How to get the value of dynamically created checkbox in asp.net gridivew

Posted by Srisoft under ASP.NET on 8/14/2015 | Points: 10 | Views : 5855 | Status : [Member] | Replies : 3
Hi,

This is sridhar,i m adding checkbox controls dynamically in asp.net gridview like this
CheckBox cb = new CheckBox();
cb.EnableViewState = true;
cb.ID = "chkbox" ;
GridView1.Rows[j].Cells[mcount].Controls.Add(cb);
and i want to access that checkbox is checked or not on button click event....

Any one Help me...

With Regards
S.Sridhar




Responses

Posted by: Sheonarayan on: 8/18/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
If the button is outside the Grid (not inside one of the column of the Grid), loop through each GridViewRow of the GridView and then try to find out using this approach http://www.dotnetfunda.com/forums/show/4605/want-to-find-control-in-gridview-on-rowcommand-event-in-aspnet.



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Srisoft, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srisoft on: 8/19/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for your reply...sir..
My requirement is, create header column dynamically and add checkbox dynamically to corresponding header column. like this

for (int j = 0; j < GridView1.Rows.Count; j++)
{

GridView1.Rows[j].Cells[mcount].BackColor = System.Drawing.Color.Red;

CheckBox cb = new System.Web.UI.WebControls.CheckBox();
cb.ID = "chkChecked_" + j;
cb.EnableViewState = true;
//cb.ID = "chkbox" ;
cb.Enabled = false;
GridView1.Rows[j].Cells[mcount].Controls.Add(cb);
}

and i want to access the dynamically added checkbox value if it is checked or not like this...

for (int j = 0; j < GridView1.Rows.Count; j++)
{

CheckBox cb = (CheckBox)GridView1.Rows[j].Cells[mcount].FindControl(string.Format("chkChecked_{0}", j.ToString()));
if (cb.Checked == true)
{
Response.Write("CHECKED");

}

}

but i got error...

Object reference not set to an instance of an object.

Help me...

Srisoft, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 9/14/2015 [Member] Starter | Points: 25

Up
0
Down
Hello,
In your Save Button Click
For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
end if

If you need to create Event for your Check Box then your code might be

Dim CheckBoxes As List(Of CheckBox) = New List(Of CheckBox)

Dim i As Integer = 1
Dim x As Integer = 10
Dim y As Integer = 15
For Each row As DataRow In objdtr.Rows

Dim chk As New CheckBox
chk.Name = row("Voltage")
chk.Text = row("Voltage")

chk.Location = New Point(x, y)
chk.Width = 70
GroupBox1.Controls.Add(chk)
x = x + 70
AddHandler chk.CheckedChanged, AddressOf ChkBox_CheckedChanged

Next


Private Sub ChkBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim chkBox As CheckBox = TryCast(sender, CheckBox)

If chkBox IsNot Nothing Then

MessageBox.Show(chkBox.CheckState)

End If

End Sub

'In Save Button Click
For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
end if
Next

SAMIR
Sr. Software Engineer

Srisoft, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response