Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a function setlocation(); which bind my dropdownlist on pageload.
and if user select another location then on selectedindex i use following code:
C#
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            string check = "";
                        check = ddlState.SelectedItem.Text;
            ViewState["statename"] = check;
        }

ON page load i use:
C#
if(ViewState["statename"]!=null)
               {
                   ddlState.SelectedItem.Text = ViewState["statename"].ToString();
               }
                else
                   {
                Setlocation();
                   }

in aspx:
XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                       <ContentTemplate><asp:DropDownList ID="ddlState" AutoPostBack="true"   runat="server" style="height:28px; width:176px"
                               onselectedindexchanged="ddlState_SelectedIndexChanged">
                       </asp:DropDownList>
                     </ContentTemplate>
                           <Triggers>
                               <asp:AsyncPostBackTrigger ControlID="imgBtnCancel" EventName="Click" />
                           </Triggers>
                     </asp:UpdatePanel>

The problem is that onpage load the complier fall alwayz on else condition.why where i am wrong
Posted
Updated 12-Dec-10 21:55pm
v4
Comments
Abdul Quader Mamun 13-Dec-10 3:16am    
Spelling check.

1 solution

Well, 2 things:
1. Sounds like you forgot to use the page 'IsPostback' property. Make sure that you are not rebinding the dropdown on every postback - if so, it will reset the selection.

2. Once IsPostback is in place, the else part would hit from 3rd time onwards. First time, fresh pageload, nothing in viewstate. 2nd time, again nothing as selectedindex change event is not yet hit. Once hit, it will set the viewstate now.After this, if you have any postback it will go to that part.

Now, not sure what you are trying to do but it does not look like the intended bevaiour or the way to do it.
 
Share this answer
 
Comments
balongi 13-Dec-10 3:40am    
I dont want 'ispostback' property, is their any alternative
balongi 13-Dec-10 3:40am    
because setlocation function is called on button click which i wriiten on updatepanel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900