Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Al Moje 9-Jan-12 0:56am    
If possible, I suggest you to allot field on your data table for such check filtering...

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.
C#
List<string> list = new List<string>();
    list.Add("1");

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

my aspx page
XML
<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
C#
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
C#
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
C#
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
C#
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
        }
    }
 
Share this answer
 
v5
Comments
Vivek Shankar 9-Jan-12 0:56am    
Yes,please help me with detail code.
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();
                    }
                }
            }
        }
    }


same way you can write code for set the ckeckbox.

Hope this will help you :)
 
Share this answer
 
 
Share this answer
 
Comments
dinidusoft123 9-Jan-12 4:15am    
this is good article that discrib the solution i have proposed. Please follow this artical if you are using grid paging instead of DataPaging.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900