I have a page where The Dropdownbox (selUsers) is populated from a database on pageload.
On LoadComplete I am setting the value based on a value from the maser page.
selUsers.SelectedValue = master.intUserID
This appears to work properly, as the correct text is displayed in the dropdownbox.
I have a button that calls a sub. In that sub, hen I call selUsers.SelectedItem.Value or selUsers.SelectedValue I get the value of the first item in the dropdownbox not the selected one.
I have been reading and I am seeing that the code to set the value has to be in ispostback, but I am not posting the page back, nor do I want to. Is it truly the case that even if you set the value of the dropdown, it isn't really set until you post the page back? If that isnt the case, why am I not getting the correct value.
Before you ask, I have scoured the code, and at no place else is the dropdown being rebound, or the value being reset.
the whole page life cycle can be a tad confusing.
I'd advise you to read this (article: ASP.NET Page Life Cycle Overview) https://msdn.microsoft.com/en-us/library/ms178472.aspx
But to answer your question:
If you click that button , the following takes place in this specific order (relevant to your problem):
1) Page load event gets raised: this is where you get your data and databind, thus 'resetting' the selected value on the dropdownlist as well.
2) The button click event gets raised (this is an item event): You read out the value you want to read out
BUT since you haven't reached LoadComplete yet (where you'll set the new selected value) you'll see the first value in the list.
3) Load complete event gets raised: here you set the correct selected value.
So yes, if you click on an asp web form's button (by default), it will cause a postback and go through the whole page life cycle to build the 'response' to your action. You'll have to take this into account while building your pages.
You can see this in action while debugging: place breakpoints on those 3 points in your code behind file and then click the button.
To solve your problem, you'll need to set the selected value after you databind your dropdownlist AND before you read out the selected value.
Hope this helps.
Related
I have a page that if IsPostBack is true, calls a javascript function which gets the size of the screen, passes the width and height to hidden fields and clicks a button to cause a PostBack. So I can retrieve the size of the user's screen and then load the page with default data with tables the right size etc.
Invariably, when the page (having posted back once) displays the data, a gridview is populated. Each row has a 'delete' Link Button in it. Each Link Button is set OnClientClick to call a javascript function to confirm you want to delete.
Above the grid is a row of text boxes / buttons / dropdownlists which allow you to search for, or filter the data showing. The Gridview is in an update panel. When the page first loads its data, and shows the list of projects, the delete Link Buttons all work okay. In every row, no problem. The Confirm box is displayed and, if Okay is selected, the asynchronous postback occurs.
If, after the initial data is displayed (which is already after one postback) you then use the search box and button, or use a dropdownlist to filter the data (the Search button and filter DropDownList are async triggers for the UpdatePanel) the data refreshes okay (always) but, then, sometimes, the 'delete' LinkButton in each row of the GridView does nothing. It doesn't even fire the ClientSide function. It's as if it is dead.
I had a similar problem a while ago with ImageButtons and the received wisdom seems to be 'change them to LinkButtons'. But I have dozens of grids with 'delete' LinkButtons that work okay, but this one has decided to stop working - sometimes.
I have to say, since moving to Framework 4.0, I'm thinking of giving up on UpdatePanels. Seem to have nothing but problems with sites that worked reliably for years in Framework 2.0
Any ideas please? Is this anything to do with the order in which controls are loaded on the second postbacks?
Edit: There is a twist to this. When this situation occurs, i.e. clicking on the Delete link does not even trigger the Client Side event - if you click on an area of the page outside the Gridview (i.e. outside the UpdatePanel) - and then click back on the Delete link - it works!
Regarding ImageButtons: There is a bug with IE10: ASP.NET fails to detect IE10 causing _doPostBack is undefined JavaScript error or maintain FF5 scrollbar position ( http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx ). Just upgrade to .NET 4.5.
Regarding LinkButtons: Do you have "ID" property? I remember I had a similar issue because my linkButton didn't have an ID.
Could you post some code? It would help.
In a program I have 1 multiview with several views into it (where views are menu options).
The problem is my error message already shows up from the moment I click on the menu option (a certain view).
After input it disapears and when I leave it empty for the next input, the error message comes back.
So in other words it works fine, but when I click on the menu that error message should not be there (when the page loads/shows for the 1st time).
EDIT: a (IsPostBack) within a function seemed to have solved the issue.
IsPostBack can be so confusing at time :>
I would recommend wrapping all of your controls with a unique ValidationGroup per view. The problem is that all validation is firing on postback, so any validation control (hidden or not) that is invalidated will show the message as long as it is within the current view. You will also need to add the same ValidationGroup to the buttons that are navigating between the Views. I would also bet that you are not checking if Page.IsValid() is true before navigating to the next view. If you do, you will notice that you will not be able to navigate away from the current view because the validators in the other views are invalidated.
You can also set CausesValidation on any button to False to prevent any validation from firing.
This project has gone from being a simple '99 Ford F-150 to the Homer.
I've got controls with a gridview with textboxes for data entry.
All the user controls on the pages are in AJAX updatepanels.
User types in a database column or budget entity or some other financial thing they want to include in the report.
The textboxes in the gridview have autopostback = true set.
overly long background info
When the user leaves the textbox, during the postback (triggered by onTextChanged) I do some validation back on the server on their entry - regexs, do they have rights to that column, is that column locked, etc. If it fails, I put a error message next to the textbox. If it passes, I wipe out any title or error that used to be next to the code.
Focus is getting lost from the postback if they're tabbing out of the box, rather than going to the next textbox in the gridview.
So to fix that I need, if their leaving the tb via the tab key, to also figure out what textbox or gridviewrow they're on, if they're not on the last row, and after the validation and labeling, put the focus on the textbox in the next row.
I can't figure out how, in ontextchanged, to find what caused me to leave the textbox, so I'm thinking use javascript onkeyup to test the key pressed and then find the next box etc, but the ontextchanged fires first and then the js never does, and also, since the control is all AJAXed, the javascript can't find the textboxes because when you enter the page everything is collapsed (the requirements people loooove to collapse and expand things), and so when it's expanded, all the 'new' textboxes are up in the viewstate stuff in the page source, and not down where javascript can see them.
The questions
So I'm wondering if I can have an onblur in the javascript that can trigger a postback where I can do my validation and such, and either 1) include the keypressed or pick it out of sender in the event or 2) followup the onblur with onkeyup and somehow figure out what textbox is next on the grid and throw focus there.
Or, is there another .NET based approach that could work for this? In terms of tearing the whole thing down and starting from scratch, I couldn't sell that to the bosses, I'm past the point of no return as far as that goes.
Does changing the one textbox value change anything else on the screen besides the error message/title associated with the textbox?
If not, my first thought would be to stop using the AutoPostBack and change to a Page Method hooked up to the onblur event. This should fix your tab issue and also reduce the back and forth between client and server.
Here's a blog post that talks about update panels and also Page Methods; The blog also includes a lot of useful posts about using javascript with .net, ajax, etc.
I have a DropDownList on an ASP.NET master page and I want to change some values and refresh the page when I select a different item from the list. I enabled the post back property in the DropDownList but it still gets back always to the first value whenever I select. Any advice?
Please post some more code to show where you are setting the value of the dropdown (both on load and anywhere else).
Usually, this is just a case of not understanding the event model. Try setting breakpoints on all of those points where you set the value and step through the code.
The most obvious case would be if you're setting the value in the page_load event handler and not wrapping it in a check for !Page.IsPostback
How should I get a value displayed in the DropDownList when redirected through a cancel buttton. The value in the page where cancel button is there in a TextBox should be caught in the DropDownList.
This DropDownList has SelectedIndexChanged event also fired for which on selection of a country in the list we get a ListView displayed in the same page. In that ListView we have an add button which will redirect us to another page called addcountry in which we have few controls. In those controls one TextBox I am getting value through QueryString in an enabled False state. Now again I need that value displayed in the DropDownList when I click cancel button.
How can I solve this problem?
Using $_POST and $_GET Variables you should be able to retreive almost any value from a form and put it in the right place on a new page, this right place can be one of your drop down list element...
To make absolutly sure your Cancel Button will send the good value to the right page put it in an independant form that has your new page (where you have yout dropdown list) as its action, and in this seperate form put an hidden field that holds the specific value you wanna see in this dd list ...
Hope I am clear but starting with your very "fuzzy" and blur question It is hard to help you.