asp.net下拉列表未更新选定的值

时间:2014-01-02 12:00:28

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

我有一个带有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));

1 个答案:

答案 0 :(得分:0)

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

我建议您稍后尝试在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)
{
...
}