即使使用Page.IsPostback,Dropdownlist也会在回发时再次绑定数据

时间:2012-12-03 11:02:33

标签: c# asp.net

我需要列出所有列出的商店。当点击时需要获取所选值但发生回发,即使我检查Page.IsPostback它也总是为null,因为没有数据。

这是我的代码。

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

这是我的aspx文件

<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>

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

这是我的代码

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();
            }

我正在做的是我在点击时隐藏了一个div并显示了所有调查问题的另一个div,并且在提交时我需要获得前一个具有下拉列表的div的值。

请帮助我,因为我发现它非常困难。

谢谢大家。

2 个答案:

答案 0 :(得分:1)

首先,在ASPX中的DropDownList中指定AsyncPostBackTrigger

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

其次,请尝试使用IsInAsyncPostBack

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

答案 1 :(得分:1)

启用您的网页&amp; DropDownlist viewState&amp;然后尝试

视图状态使服务器控件能够跨HTTP请求维护其状态。如果满足以下所有条件,则启用控件的视图状态:

  • 页面的EnableViewState属性设置为true。
  • 控件的EnableViewState属性设置为true。
  • 控件的ViewStateMode属性设置为Enabled或 继承Enabled设置。

或 在control / page init中加载数据(不要检查IsPostBack标志)而不是pageLoad