Gridview asp.net 2.0中的下拉列表 [英] Dropdownlist in Gridview asp.net 2.0

查看:64
本文介绍了Gridview asp.net 2.0中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在Gridview中有dropdownlist.当我重新绑定/刷新gridview时,它将重置选定的值.

请帮我.我该如何解决这个问题
谢谢
Seema

Hi,
I have dropdownlist within Gridview. When I rebind/Refresh the gridview then it reset the selected value.

Please help me. How can I solve this issue
Thanx
Seema

推荐答案

Hello Seema,

如果刷新或重新绑定GridView,它将每次重置选定的值.如果要保留它,只需检查需要更新哪些项目,而不是刷新整个数据即可.
Hello Seema,

If you refresh or rebind the GridView, it will everytime reset the selected value. If you want to retain it, just check what are the items you need to update rather than refreshing the whole data.


hi seema,
I write some codes:

In Html Page:

<asp:gridview runat="server" id="GridView1" autogeneratecolumns="false" cellpadding="3" cellspacing="0" xmlns:asp="#unknown">
    <columns>
      <asp:templatefield headertext="Column1">
        <itemtemplate>
           <asp:dropdownlist runat="server" id="DropDownList1" width="150px/">
          <asp:hiddenfield runat="server" id="HiddenField1" value="<%#Bind("Column1") %"> />
        </asp:hiddenfield></asp:dropdownlist></itemtemplate>
      </asp:templatefield>
      <asp:boundfield datafield="Column2" headertext="Column2" />
      <asp:boundfield datafield="Column3" headertext="Column3" />
    </columns>
   </asp:gridview>

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            DataTable datatable = new DataTable();
            datatable.Columns.Add("Column1", typeof(System.String));
            datatable.Columns.Add("Column2", typeof(System.String));
            datatable.Columns.Add("Column3", typeof(System.String));

            datatable.Rows.Add("1", "2", "3");
            datatable.Rows.Add("4", "5", "6");

            this.GridView1.DataSource = datatable;
            this.GridView1.DataBind();

            foreach (GridViewRow GridViewRow1 in this.GridView1.Rows)
            {
                //find hiddenField and DropDownList by id
                HiddenField hiddenField1 = (HiddenField)GridViewRow1.FindControl("HiddenField1");
                DropDownList dropDownList1 = (DropDownList)GridViewRow1.FindControl("DropDownList1");
                if (hiddenField1 != null && dropDownList1 != null)
                {
                    dropDownList1.Items.Clear;
                    //bind dropDownList1
                    //.....
                    dropDownList1.SelectedValue = hiddenField1.Value;
                }
            }
        }


Hope make you understand.


将数据保存在ViewState中.并在DropDownList上重新绑定/刷新后将其绑定.
save data in ViewState. and bind it after rebind/Refresh on your DropDownList.


这篇关于Gridview asp.net 2.0中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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