使用分页在GridView中维护检查值 [英] maintaining checked values in a gridview with paging

查看:73
本文介绍了使用分页在GridView中维护检查值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发一个网站时,我遇到了一个问题,我要维护检查值.用户搜索项目时,有很多项目可供选择,因此所有项目都显示在Grid视图中,我在Gridview中有一个复选框.现在当用户选择多个项目并保存时,我需要维护所有分页检查的数据.请帮助我. br/>
问候

Vivek

I gott one Problem which exists with me when I am developing one web site during developing this site I come on point to maintain checked value. User search for items there are lot’s of item to select, So all items display in Grid view, I have checkbox in Gridview.Now when user select multiple item and save I need to maintain all paging checked Data.please help me out friends.

regards

Vivek

推荐答案



您必须为此目的维护隐藏字段List和属性(get,set).将选中的项目ID绑定到字符串列表或整数列表.

从列表中添加或删除ID.发生此复选框的checkchange事件,您可以在事件旁边捕获选中或未选中的项目ID,并从列表中添加或删除该列表,并将其设置为在隐藏字段中保留值的属性.如果您需要更多编码级别的帮助,请告诉我.
Hi,

You have to maintain hidden field List and property (get,set) for this pupose. Bind checked item id''s in to string list or int list.

add or remove id from list. this happen checkbox''s checkchange event and you can catch the checked or unchecked item id in side the event and add or remove from the list and set to the property which maintain the values in side the hidden field. If you want more coding level help, please let me know.
List<string> list = new List<string>();
    list.Add("1");


让我用您需要改进的基本代码来更新解决方案.

我的aspx页面


let me update solution with basic codes that you need to improve.

my aspx page

<form id="form1" runat="server">
    <div>
  <asp:GridView ID="GridView1" runat="server"

AutoGenerateColumns="False"

>

    <Columns>

        <asp:TemplateField HeaderText="Name"

SortExpression="FirstName">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server"

Text='<%# Bind("FirstName") %>'></asp:Label>

                <asp:CheckBox ID="CheckBoxDone" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxDone_CheckedChanged"  />
            <asp:Label ID="Label2" runat="server"

Text='<%# Bind("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:HiddenField ID="hf1" runat="server"></asp:HiddenField>
    </div>
    </form>


而且我已经将数据表绑定到页面加载事件内的网格​​视图中


and i have bind the data table in to the grid view inside the page load event

protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              GridView1.DataSource = FillDataTable();
              GridView1.DataBind();
              List<string> strList = new List<string>();
          }
      }


这是填充数据方法


this is fill data method

private static DataTable FillDataTable()
        {
            DataTable dt = new DataTable("dttbl");
            DataRow dr;

            dt.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
            dt.Columns.Add(new System.Data.DataColumn("FirstName", typeof(string)));


            dr = dt.NewRow();
            dr["ID"] = 1;
            dr["FirstName"] = "Dinidu";
            dt.Rows.Add(dr);

            return dt;

        }


这是财产


this is property

public List<string> MyProperty {
           get{

               if (ViewState["val"] == null)
            {
                ViewState["val"] = new List<string>();
            }               return (List<string>)ViewState["val"];
                          }

           set
           { ViewState.Add("val", value); }
       }


这是已检查的更改事件


this is checked change event

protected void CheckBoxDone_CheckedChanged(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
        {

            string x = ((Label)((CheckBox)sender).FindControl("Label2")).Text;
            // you should insert below two lines  which match to the property for recover the exception
           // List<string> strList = new List<string>();
            //strList.Add(x);
            this.MyProperty.Add(x)
             // = strList;
        }
        else
        {
           // In here remove the value from the property
        }
    }


朋友,

您需要将分页保留存储到ViewState或Session变量中.

关于复选框值
在下面的代码中,我使用Datalist控件的方式与使用Grid控件的方式相同.
我添加了 chkInquier 属性来设置主键值.

Hi friend,

you need to store your paging detain to ViewState or in Session variable.

Regarding Checkbox value
in below code i was use Datalist control same way u can use Grid control.
and chkInquier attribute i add for set primary key value.

public void GetSelectCatalogId()
    {
        foreach (DataListItem dlt in dtProductList.Items)
        {
            if (dlt.FindControl("chkInquier") != null)
            {
                CheckBox chk = (CheckBox)dlt.FindControl("chkInquier");
                if (chk.Checked)
                {
                    string strcatalogid = chk.Attributes["catalogid"].ToString();

                    if (StrCataLogIdList != string.Empty)
                    {
                        if (!StrCataLogIdList.Contains(strcatalogid))
                        {
                            StrCataLogIdList += ", " + strcatalogid.ToString();
                        }
                    }
                    else
                    {
                        StrCataLogIdList += strcatalogid.ToString();
                    }
                }
            }
        }
    }



您可以使用相同的方式编写代码来设置ckeckbox.

希望这会对您有所帮助:)



same way you can write code for set the ckeckbox.

Hope this will help you :)


http://www.highoncoding.com/Articles/697_Persisting_CheckBox_State_While_Paging_in_GridView_Control.aspx [^ ]

希望对您有帮助!
http://www.highoncoding.com/Articles/697_Persisting_CheckBox_State_While_Paging_in_GridView_Control.aspx[^]

hope tis ll help u !!!


这篇关于使用分页在GridView中维护检查值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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