Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C ASP.NET
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 8-Jan-12 19:44pm
Comments
Al Moje at 9-Jan-12 0:56am
   
If possible, I suggest you to allot field on your data table for such check filtering...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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");
    
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
        }
    }
  Permalink  
v5
Comments
Vivek Shankar at 9-Jan-12 0:56am
   
Yes,please help me with detail code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
dinidusoft123 at 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)

  Print Answers RSS
0 DamithSL 495
1 Maciej Los 258
2 OriginalGriff 229
3 BillWoodruff 170
4 Zoltán Zörgő 165
0 OriginalGriff 7,934
1 DamithSL 6,084
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,174
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 9 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100