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