坚持寻呼的DropDownList [英] persist dropdownlist on paging

查看:213
本文介绍了坚持寻呼的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView和我通过列表填充它。它的一个栏目是一个DropDownList和AllowPaging设置为true。我的问题是,当我对DDL和寻呼选择的值丢失后选择一个值。有什么办法/想法坚持所选值?
感谢您的帮助。

I have a GridView and I populate it via a List . One of its columns is a DropDownList and AllowPaging is set to true. My problem is when I choose a value on the ddl and after a paging the selected value is lost. Is there any way/idea to persist the selected values? Thanks for your help.

推荐答案

您可以使用视图状态中Dictionary对象保存多个值,即

You could use a Dictionary object within view state to save multiple values i.e.

Dictionary<int, string> ddlValues = new Dictionary<int, string>()

其中int是行索引和字符串是DDL选择的值。当然,这可能是一个int / GUID或任何根据存储在DDL或一个int,如果你想使用的selectedIndex工作的实际价值来代替。

where int is the row index and string is the ddl selected value. Of course this could be an int/guid or whatever depending on the actual value stored in the ddl or an int if you want to work with selectedIndex instead.

页面上的事件中,你需要做的。

on the page event you would need to do

protected void MyGridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
   for(int rowIndex = 0; rowIndex < myGridView.Rows.Length; rowIndex++)
   {
        DropdownList ddl = myGridView.Rows[rowIndex].FindControl("ddlId") as DropDownList

    if(ddl != null)
        {
           if(ddl.SelectedIndex > 0) //.. or sensible check appropriate to you
           {
               int ddlIndex = rowIndex * e.NewPageIndex + 1;

               //.. add pageIndex and selectedValue to dictionary
               ddlValues.Add(ddlIndex, ddl.SelectedValue);

            }

        }
    }
}

不要担心当前页面DDL值。这将与正常的方式来视图状态持续。这是我们占隐藏的网页。因此,我们将重新填充字典当电网页面。

Don't worry about the current page ddl values. These will be persisted with viewstate in the normal way. It is the 'hidden' pages that we are accounting for. Hence we are repopulating the dictionary when the grid pages.

本词典可以被保存在会话/ ViewState中并用于通过执行过程反向补充水分DropDownList的。例如,当页面加载(检查!的IsPostBack ),或者当电网取决于你究竟是如何设置的东西,重新绑定

The Dictionary could then be saved in session/viewState and used to rehydrate the dropdownlist by doing the process in reverse. For instance when the page loads (checking !isPostBack) or when the grid rebinds depending on exactly how you have set things up

这篇关于坚持寻呼的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆