简体   繁体   English

asp.net dropdownlist不更新所选值

[英]asp.net dropdownlist not updating selected value

I have a page with a gridview on it and a dropdown list which controls how many items per page the gridview will display. 我有一个带有gridview的页面和一个下拉列表,该列表控制gridview每页显示多少个项目。

The pageSize value of the gridview is controlled by this dropdown list and it gets saved to a cookie. gridview的pageSize值由此下拉列表控制,并将其保存到Cookie中。 When the user loads the site the cookie is read so that it remembers what page size the user picked. 当用户加载站点时,将读取cookie,以便它记住用户选择的页面大小。

I have one problem and that is, if I pick another value on the dropdown list it does not update either cookie or dropdown list. 我有一个问题,那就是,如果我在下拉列表中选择另一个值,它不会更新cookie或下拉列表。 It reverts back to the saved value. 它恢复为保存的值。

This is the dropdown list created in the gridview pager template: 这是在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>

and this is where I try to load the value of the dropdown list from cookie: 这是我尝试从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();
            }
        }

The dropdown list index changed code: 下拉列表索引已更改代码:

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

When I step through the code of the SelectedIndexChanged method, I can see that ddlPageSize.SelectedValue always contains the value from the cookie, 50, even though I select another value. 当我单步执行SelectedIndexChanged方法的代码时,可以看到ddlPageSize.SelectedValue始终包含来自cookie的值50,即使我选择了另一个值也是如此。

I guess the question is, where do I set the index of the dropdown list? 我想问题是,在哪里设置下拉列表的索引?

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

The Page_Load event executes before the DropDownList SelectedIndexChanged event. Page_Load事件在DropDownList SelectedIndexChanged事件之前执行。 And you are loading the cookie's value to the DropDownList on the PageLoad event. 并且您正在将Cookie的值加载到PageLoad事件上的DropDownList

I suggest you to try loading the cookie afterwards, on the OnPreRender event for example. 我建议您尝试稍后在OnPreRender事件上加载cookie。

Or add a condition to your Page_Load logic, verifying if the PostBack is caused by the DropDownList : 或向您的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