【问题标题】:asp.net dropdownlist not updating selected valueasp.net 下拉列表不更新所选值
【发布时间】:2014-01-02 12:00:28
【问题描述】:

我有一个带有 gridview 的页面和一个下拉列表,用于控制 gridview 每页显示多少项目。

gridview 的 pageSize 值由该下拉列表控制,并保存到 cookie 中。 当用户加载网站时,会读取 cookie,以便它记住用户选择的页面大小。

我有一个问题,如果我在下拉列表中选择另一个值,它不会更新 cookie 或下拉列表。它恢复为保存的值。

这是在gridview pager模板中创建的下拉列表:

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

【问题讨论】:

    标签: c# asp.net gridview postback html-select


    【解决方案1】:

    Page_Load 事件在DropDownList SelectedIndexChanged 事件之前执行。您正在将 cookie 的值加载到 PageLoad 事件上的 DropDownList

    我建议您之后尝试加载 cookie,例如在 OnPreRender 事件上。

    或者在你的Page_Load逻辑中添加一个条件,验证回发是否是由DropDownList引起的:

    DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;      
    bool isDDLPostingBack = Request["__EVENTTARGET"] == ddlPageSize.UniqueID;
    
    if (Request.Cookies["PageSize"]["Value"] != null && !isDDLPostingBack)
    {
    ...
    }
    

    【讨论】:

    • 如果我将 cookie 读取放在 Page_PreRenderComplete 中,我几乎可以让它工作,除了我必须更改下拉列表两次才能更改值。似乎当它读取 cookie 时,它​​找到了两个 PageSize 副本,我还可以看到在 Cookies 文件夹中每次都会创建一个新副本。这不可能。
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2016-12-10
    相关资源
    最近更新 更多