Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 02, 2013 01:55 AM by artvindustries
Member
14 Points
52 Posts
Feb 02, 2013 12:10 AM|LINK
Hello,
Its the one issue that keeps coming up. My current Holy Grail of pain. Why won't this simply work?
protected void lbAll_Click(object sender, EventArgs e) { CheckBox chkbx = ((CheckBox)FindControl("CheckBox1")); GridView grd1 = ((GridView)FindControl("GridView1")); foreach (GridViewRow gvrow in GridView1.Rows) { if (!chkbx.Checked) { chkbx.Checked = true; } } }
It throws an "Object Reference not set to an instance of an Object" error.
This should simply iterate through the rows and check all the unchecked boxes. Grrr.
All-Star
31373 Points
6412 Posts
Feb 02, 2013 12:37 AM|LINK
Can you explaint whre you place the cb?
foreach (GridViewRow gvrow in GridView1.Rows) { CheckBox chkbx = ((CheckBox)gvrrow.FindControl("CheckBox1")); if (!chkbx.Checked) { chkbx.Checked = true; } }
do you mean?
GridView grd1 = ((GridView)Page.FindControl("GridView1"));
Feb 02, 2013 12:45 AM|LINK
Sure thing!
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="Both" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"> <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> <Columns> <asp:TemplateField HeaderText="Approve?"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" text="Approve"/><br /> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle CssClass="pgr"></PagerStyle> </asp:GridView> </div>
Feb 02, 2013 01:55 AM|LINK
Found the problem!
First stating the problem. This will return all checkboxes as "Its not checked"
foreach (GridViewRow gr in GridView1.Rows) { CheckBox cb = (CheckBox)gr.Cells[0].FindControl("CheckBox1"); if (!cb.Checked) { Response.Write("Its not checked"); } else { if (cb.Checked) { Response.Write("Its Checked"); } } }
The solution is the .IsPostBack method:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = result; GridView1.DataBind(); } }
The page load rebinds your gridview so the data doesn't stick. I've slain my White Buffalo. lol
artvindustri...
Member
14 Points
52 Posts
The GridView FindControl Mystery
Feb 02, 2013 12:10 AM|LINK
Hello,
Its the one issue that keeps coming up. My current Holy Grail of pain. Why won't this simply work?
protected void lbAll_Click(object sender, EventArgs e) { CheckBox chkbx = ((CheckBox)FindControl("CheckBox1")); GridView grd1 = ((GridView)FindControl("GridView1")); foreach (GridViewRow gvrow in GridView1.Rows) { if (!chkbx.Checked) { chkbx.Checked = true; } } }It throws an "Object Reference not set to an instance of an Object" error.
This should simply iterate through the rows and check all the unchecked boxes. Grrr.
oned_gk
All-Star
31373 Points
6412 Posts
Re: The GridView FindControl Mystery
Feb 02, 2013 12:37 AM|LINK
Can you explaint whre you place the cb?
foreach (GridViewRow gvrow in GridView1.Rows) { CheckBox chkbx = ((CheckBox)gvrrow.FindControl("CheckBox1")); if (!chkbx.Checked) { chkbx.Checked = true; } }do you mean?
GridView grd1 = ((GridView)Page.FindControl("GridView1"));artvindustri...
Member
14 Points
52 Posts
Re: The GridView FindControl Mystery
Feb 02, 2013 12:45 AM|LINK
Sure thing!
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="Both" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"> <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> <Columns> <asp:TemplateField HeaderText="Approve?"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" text="Approve"/><br /> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle CssClass="pgr"></PagerStyle> </asp:GridView> </div>artvindustri...
Member
14 Points
52 Posts
Re: The GridView FindControl Mystery
Feb 02, 2013 01:55 AM|LINK
Found the problem!
First stating the problem. This will return all checkboxes as "Its not checked"
foreach (GridViewRow gr in GridView1.Rows) { CheckBox cb = (CheckBox)gr.Cells[0].FindControl("CheckBox1"); if (!cb.Checked) { Response.Write("Its not checked"); } else { if (cb.Checked) { Response.Write("Its Checked"); } } }The solution is the .IsPostBack method:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = result; GridView1.DataBind(); } }The page load rebinds your gridview so the data doesn't stick. I've slain my White Buffalo. lol