簡體   English   中英

asp.net dropdownlist不更新所選值

[英]asp.net dropdownlist not updating selected value

我有一個帶有gridview的頁面和一個下拉列表,該列表控制gridview每頁顯示多少個項目。

gridview的pageSize值由此下拉列表控制,並將其保存到Cookie中。 當用戶加載站點時,將讀取cookie,以便它記住用戶選擇的頁面大小。

我有一個問題,那就是,如果我在下拉列表中選擇另一個值,它不會更新cookie或下拉列表。 它恢復為保存的值。

這是在gridview頁面模板中創建的下拉列表:

<PagerTemplate>
             <asp:Table ID="Table3" runat="server" Width="100%">
                 <asp:TableRow>
                     <asp:TableCell HorizontalAlign="Left">
                         <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
                     </asp:TableCell>
                     <asp:TableCell HorizontalAlign="Right" Width="10%">
                         Page Size
                            <asp:DropDownList runat="server" ID="ddlPageSize" AutoPostBack="true"
                                 OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" OnLoad="ddlPageSize_Load">
                                <asp:ListItem>5</asp:ListItem>
                                <asp:ListItem>10</asp:ListItem>
                                <asp:ListItem>20</asp:ListItem>
                                <asp:ListItem>50</asp:ListItem>
                                <asp:ListItem>100</asp:ListItem>
                            </asp:DropDownList>
                     </asp:TableCell>
                 </asp:TableRow>
             </asp:Table>
        </PagerTemplate>

這是我嘗試從Cookie加載下拉列表的值的地方:

protected void Page_Load(object sender, EventArgs e)
        {
            string pageSize = "10";
            //Try and load the PageSize cookie from the user's machine and default to 10 records if none is found.
            if (Request.Cookies["PageSize"] != null)
            {                
                if (Request.Cookies["PageSize"]["Value"] != null)
                {
                    pageSize = Request.Cookies["PageSize"]["Value"];
                    int _pageSize;
                    int.TryParse(pageSize, out _pageSize);
                    gvRecordsList.PageSize = _pageSize;
                    DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
                    ddlPageSize.SelectedIndex = ddlPageSize.Items.IndexOf(new ListItem(pageSize));
                }
            }
            else
                gvRecordsList.PageSize = 10;


            if (IsPostBack)
            {
                ApplyPaging();               
            }
            else
            {
                gvRecordsList.DataSourceID = "RecordsListSqlDataSource";
                gvRecordsList.DataBind();
            }
        }

下拉列表索引已更改代碼:

protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
            gvRecordsList.PageSize = int.Parse(ddlPageSize.SelectedValue);
            Response.Cookies["PageSize"]["Value"] = ddlPageSize.SelectedValue;
            Response.Cookies["PageSize"].Expires = DateTime.Now.AddDays(1d);
        }

當我單步執行SelectedIndexChanged方法的代碼時,可以看到ddlPageSize.SelectedValue始終包含來自cookie的值50,即使我選擇了另一個值也是如此。

我想問題是,在哪里設置下拉列表的索引?

DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
                        ddlPageSize.SelectedIndex = ddlPageSize.Items.IndexOf(new ListItem(pageSize));

Page_Load事件在DropDownList SelectedIndexChanged事件之前執行。 並且您正在將Cookie的值加載到PageLoad事件上的DropDownList

我建議您嘗試稍后在OnPreRender事件上加載cookie。

或向您的Page_Load邏輯添加條件,以驗證PostBack是否由DropDownList引起:

DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;      
bool isDDLPostingBack = Request["__EVENTTARGET"] == ddlPageSize.UniqueID;

if (Request.Cookies["PageSize"]["Value"] != null && !isDDLPostingBack)
{
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM