繁体   English   中英

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

[英]Dropdownlist binding data again on postback even with Page.IsPostback

我需要列出所有列出的商店。 当点击需要获取所选值但发生回发时,即使我检查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的值。

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

谢谢大家。

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

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

其次,尝试使用IsInAsyncPostBack

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

启用您的页面和DropDownlist viewState然后尝试

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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