简体   繁体   中英

Dropdownlist binding data again on postback even with Page.IsPostback

I need to have a dropdown with all the stores listed. and when clicked need to get the selected value but postback happens and even when i check Page.IsPostback it is always null because there are no data.

here is my code.

=============================================

Here is my aspx file

<div id="SurveyCompetitionContainer">
    <div class="surveyCompetitionContents">
        <ajax:UpdatePanel ID="WinSurveyAjax" runat="server" UpdateMode="conditional" ChildrenAsTriggers="true"
            RenderMode="block">
            <ContentTemplate>
                <div id="competitionInfo" runat="server" visible="true">
                    <div class="headerTitle">
                        Enter your email for the chance to win a week long holiday in a luxury apartment
                        in the Lake District.
                    </div>
                    <div class="emailBox">
                        <div class="emailDiv">
                            <asp:Label ID="emailLabel" CssClass="emailLabel" Text="Email:" runat="server"></asp:Label>
                            <asp:TextBox ID="emailTextbox" runat="server" CssClass="water" Text="" ClientIDMode="Static"
                                ValidationGroup="EmailGroup"> </asp:TextBox>
                        </div>
                        <div class="storesDiv">
                            <asp:Label ID="LabelStores" CssClass="StoreLabel" Text="Your favourite Mountain Warehouse store:"
                                runat="server"></asp:Label>
                            <asp:DropDownList ID="Stores" CssClass="storesDD" runat="server" ToolTip="Stores"
                                EnableViewState="true" AppendDataBoundItems="True" OnSelectedIndexChanged="Stores_SelectedIndexChanged">
                            </asp:DropDownList>
                        </div>
                    </div>
                    <div class="enterbtn">
                        <asp:Button ID="emailJoinButton" CssClass="emailJoinButton" runat="server" OnClick="emailJoinButton_Click"
                            Text="ENTER" EnableViewState="False" ValidationGroup="EmailGroup" />
                    </div>
                </div>
                <div id="competitionSurvey" runat="server">
                    <div class="SCHeaderInfo">
                        <h2>
                            Thank you! We will notify the lucky winner by email.
                        </h2>
                        <h3>
                            We'd love to send you tailored special offers - just take a few moments to fill
                            out the following optional details.
                        </h3>
                    </div>
                    <div class="SCquestionsDetails">
                        <p>
                            <b>1. What are you interested in?</b><span class="small"> (Select all that apply)</span></p>
                        <asp:CheckBoxList ID="Interests" runat="server" CssClass="interests" Width="400">
                            <asp:ListItem>Camping</asp:ListItem>
                            <asp:ListItem>Hiking</asp:ListItem>
                            <asp:ListItem>Skiing</asp:ListItem>
                            <asp:ListItem>Travel</asp:ListItem>
                            <asp:ListItem>Walking</asp:ListItem>
                            <asp:ListItem>Everything</asp:ListItem>
                            <asp:ListItem>Mens</asp:ListItem>
                            <asp:ListItem>Womens</asp:ListItem>
                            <asp:ListItem>Kids</asp:ListItem>
                        </asp:CheckBoxList>
                    </div>
                </div>
            </ContentTemplate>
        </ajax:UpdatePanel>
    </div>
</div>

================================

Here is my Code

protected void Page_Load(object sender, EventArgs e)
            {
                        if (!Page.IsPostBack)
                                    BindAllStores();
            }




  protected void emailJoinButton_Click(object sender, EventArgs e)
            {
                        Session["FavStore"] = Stores.SelectedItem.ToString(); --> this is where i am storing the selected value.

                        if (Page.IsValid)
                        {
                                    if (competitionInfo.Visible == true)
                                    {
                                                LoadSurvey();
                                                competitionInfo.Visible = false;
                                                competitionSurvey.Visible = true;
                                    }
                        }
            }


public void  BindAllStores()
            {
                        Stores.AppendDataBoundItems = true;    
                        Stores.Items.Add(new ListItem("--- Choose one of our stroes --- ", "0"));


                        Stores.DataSource = AllStores;
                        Stores.DataTextField = "DisplayName";
                        Stores.DataValueField = "NAME";
                        Stores.DataBind();
            }

what i am doing is i am hiding one div when clicked and showing another div with all the survey questions and when submit i need to get the value of previous div which has the dropdownlist.

please help me out as i am finding it very difficult.

thanks all.

First, specify DropDownList in the AsyncPostBackTrigger in ASPX

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="dropdownlist" EventName="SelectedIndexChanged" />
</Triggers>

Second, try using IsInAsyncPostBack instead:

protected void Page_Load(object sender, EventArgs e) 
{
    if (!ScriptManager.GetCurrent(Page).IsInAsyncPostBack) 
    {
        BindAllStores();
    }
}

enable your page & DropDownlist viewState & then try

View state enables a server control to maintain its state across HTTP requests. View state for a control is enabled if all of the following conditions are met:

  • The EnableViewState property for the page is set to true.
  • The EnableViewState property for the control is set to true.
  • The ViewStateMode property for the control is set to Enabled or inherits the Enabled setting.

or Load data in control/page init (do not check the IsPostBack flag) instead of pageLoad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM