I have two very similar .aspx pages. Both of them contain a DropDownList control. Both DropDownList controls' EnableViewState is set to False:
<asp:DropDownList ID="ddl" runat="server" EnableViewState="False" />
There is a LinkButton on both pages. In the btn_Click handler, if I try to access ddl.SelectedItem property (which, as far as I know, should be null because of the EnableViewState=False, am I correct?), it is null on one page, but not null (it has a correct value) on the other page. Can you give me any pointers how can that be possible?
string txt = ddl.SelectedItem.Text; // SelectedItem sometimes null, other times not
The difference between those two pages's dropdowns is, that in one case, the dropdown is filled using AjaxToolkit's CascadingDropDown control (in that case, SelectedItem is not null, despite having EnableViewState property set to false), in the other case, dropdown's items are being filled in the Page_Load property, within and if-block, checking whether IsPostBack is false (items are only filled on the first request, I won't need them after the postback).
Thank you.
CascadingDropDown doesn't honor the enable viewstate setting. If you need it null, you should reset it to null on page load or a similar event.
If you have something populating the drop-down through JavaScript then ASP.NET will see that value, irrespective of how it got there.
Looks like the selected index gets posted back to the server when you change the value; the selected index property gets applied once items are applied in the list, so if you have no items, I don't believe it gets applied until some items get added to the list. So, if you bind the list again, and a value got selected from the list, and you rebind the drop down, it would apply the selected index. That could be it possibly.
HTH
Related
there's a table and some ddl in updatepanel.
like this:
why this happens???
I think that you forget to reset your drop down list and you place them again and again data.
So two thinks you can do. Use the IsPostBack for populate your dropdownlist and not populate them again if there is a post back, or on every post back, first clear the dropdown items them add again the list.
For fast check if the dropdownlist is the case, set enableviewstate="false" on them
This is probably something fundamental and stupid that I've missed, but the various workarounds are causing me such huge headaches that really, I need to understand the basic problem here in order to find the best solution.
I have a usercontrol which contains a drop-down list.
<asp:DropDownList ID="ddlOrderStatus" AutoPostBack="true" runat="server" CssClass="textbox">
<asp:ListItem value="8">Pending</asp:ListItem>
<asp:ListItem value="9">On Hold</asp:ListItem>
<asp:ListItem Value="11">Complete</asp:ListItem>
<asp:ListItem Value="12">Cancelled</asp:ListItem>
</asp:DropDownList>
It does nothing in its Page_Load event.
This is in turn contained in a page which, in it's Page_Load event does some databinding on some repeater controls on the page but doesn't touch the control containing the ddl. There is no ajax on the page.
The dropdownlist is - clearly - set to autopostback = true. The postback code looks like this:
Private Sub ddlOrderStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrderStatus.SelectedIndexChanged
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim currentsorting As String = Request.QueryString("sort")
Dim currentpaging As String = Request.QueryString("page")
Response.Redirect(String.Format("~/admin/orders/Manage/0?sort={0}&page={1}&status={2}", currentsorting, currentpaging, ddl.SelectedValue))
End Sub
So when the ddl is changed, the page actually does a double postback. I see no way to avoid this as I need to do a postback to get the value in the ddl and then another postback to get some new data from the server based on that value.
Anyway, the long and the short of it is that when you do this the dropdownlist fails to maintain its state. So you select a value, the code does the job it's supposed to - gets the selected value then posts back again returning the new expected set of data - and then on loading again the dropdownlist is back in its default form.
This is a pernicious problem for me because of that double-postback: if I try and set the value of the ddl to equal what's in the querystring I have, effecitvely, set it to that value permanently because Page_Load - where the value is set - occurs before events are processed and so the value stays unchanged. I tried moving the code which changed the selectedvalue of the ddl into Render, but it seemed that although the selected value had changed as far as the user could see, the selected value was still the default value during processing and was treated as such.
I also tried setting the selectedValue in session and then clearing it as soon as it was populated. That worked for some scenarios, but unfortunately this page also contains some javascript that can cause it to post back - and you can't set session from client-side javascript. So that idea had to go out the window.
I'm at a loss here - this apparently simple problem has eaten a whole day of my time. Can anyone either tell me why state isn't being maintained on this control and/or suggest a way I get it to show the right value after postback without turning the selection into a permanent one?
Cheers,
Matt
In your page load:
// Only set the selected order status when the page first loads, but not on postbacks.
if( !Page.IsPostback )
{
ddlOrderStatus.SelectedValue = Request.QueryString("status");
}
Footnote:
I see no way to avoid this as I need
to do a postback to get the value in
the ddl and then another postback to
get some new data from the server
based on that value.
You could do it from javascript
I'll preface this by saying that I do my ASP.NET programming in C# and use GridViews, but the following should apply to your situation regardless:
Is it necessary for you to use the query string to hold this data? If you've declared your paging and sorting controls on your ASPX page, you can access them and their values directly in your codebehind (as Greg demonstrated in the above code by not performing a FindControl call). Has the user control setup prevented you from being able to u this? Are you instantiating the user control containing this drop-down list on a page, and then trying to access the value of the drop-down list from the page's codebehind itself, rather than the user control's code behind?
I don't get this. So I disable viewstate on my .aspx page. I then select options from 3 dropdown menus and then on submit check the values and they are all -1. I put ViewState back on and do the same, select values and now in the submit event they're all set to a valid value for the SelectedIndex.
I don't get this. I don't see how viewstate has anything to do with selecting values in a dropdown and then calling a server-side handler to pick up those selected values in order to do something with it and in order to get the actual valid SelectedIndex values that the user selected. I don't see why it would give me a -1 if I disable viewstate for the SelectedIndex for each of those 3 dropdownlist controls.
Either move your code that binds the dropdowns into Page_Init or surround them with
if (!IsPostPack) { ... }
Am I doing this correctly?
I have two dropdowns. The first is populated on form load, and it is a trigger for an updatepanel that contains the 2nd dropdown.
When I change the value in the first dropdown box, it triggers the 2nd one's updatepanel, which populates it.
However, when I select a value in the 2nd dropdownbox, it triggers itself and repopulates... so if I select the 2nd or 3rd item in the dropdown, it repopulates and selects the first item again.
Am I doing something wrong, or is this normal? Should I put the first dropdown into an updatepanel instead and have it trigger itself, and in its own trigger, populate the 2nd listbox?
It appears that changing the value in the second dropdown is causing a postback, which it then sets the value to the default or first value. Try setting the dropdown's AutoPostBack property to false and EnableViewState to true.
Use the ASP.NET Ajax Control Toolkit CascadingDropDown instead.
EDIT: You need to set your UpdateMode=Conditional and control when your UpdatePanel's should update, by calling UpdatePanel2.Update().
I had this problem once.
This is a problem for update panel plus dropdown autopostback
By default ViewState for page is stored,so you do not need to change ViewState for control.
First,Change your UpdatePanel's UpdateMode property to Conditional UpdateMode="Conditional".This ensures that you can control postback since your dropdown always postback when you set AutoPostBack to true.
Second is in your method which you bind datasource into dropdown,after databinding is done.Update your panel by calling Update method of UpdatePanel updPanel.Update(); by
dropdown.DataSource = source;//I presume
dropdown.DataBind();
updPanel.Update();
Hope this helps.
I am trying to put a listbox control on my ASP.net page, and when I click it, the selectedindex value is always -1. Why is it doing that? I set AutoPostBack to true. Why is it always returning -1?
Please let me know.
Thanks
There could be many reasons why but I am guessing that you are loading the contents of the ListBox on every load of the page.
Wrap your data binding code in an if statement like this to allow the control to retain what index you selected:
if (!this.IsPostBack)
{
// data binding code here
}
It does depend on what your doing, but -1 normally means nothing is selected when a PostBack is occurring, or that the list of items is empty that the control is being databound to.