fetching radiobutton control value from gridview
-
through means of below code i add radiobutton to my gridview.
grdFeedback.DataSource = ds;
grdFeedback.DataBind();foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.Cells[3].Text == "" && row.Cells[4].Text == "" && row.Cells[5].Text == "" && row.Cells[6].Text == "" && row.Cells[7].Text == "")
{
TextBox txtAns = new TextBox();
txtAns.ID = "txtAns";
row.Cells[3].Controls.Add(txtAns);
}
else
{
for (int c = 3; c <= 7; c++)
{
if (row.Cells[c].Text != " ")
{
RadioButton rbtn = new RadioButton();
string rbt = "rbtngrp";
string opt = row.Cells[c].Text;rbtn.ID = rbt + row.RowIndex + c; rbtn.Text = opt.ToString(); rbtn.GroupName = rbt + row.RowIndex; row.Cells\[c\].Controls.Add(rbtn); } } } }
how to check it whether the button is checked or not in an button click?. The below code i used. but its error : 'grdFeedback.Rows[0].Cells[4].Controls[2]' threw an exception of 'System.ArgumentOutOfRangeException' bool {System.ArgumentOutOfRangeException}
((CheckBox)(grdFeedback.Rows[0].Cells[4].Controls[2])).Checked
-
through means of below code i add radiobutton to my gridview.
grdFeedback.DataSource = ds;
grdFeedback.DataBind();foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.Cells[3].Text == "" && row.Cells[4].Text == "" && row.Cells[5].Text == "" && row.Cells[6].Text == "" && row.Cells[7].Text == "")
{
TextBox txtAns = new TextBox();
txtAns.ID = "txtAns";
row.Cells[3].Controls.Add(txtAns);
}
else
{
for (int c = 3; c <= 7; c++)
{
if (row.Cells[c].Text != " ")
{
RadioButton rbtn = new RadioButton();
string rbt = "rbtngrp";
string opt = row.Cells[c].Text;rbtn.ID = rbt + row.RowIndex + c; rbtn.Text = opt.ToString(); rbtn.GroupName = rbt + row.RowIndex; row.Cells\[c\].Controls.Add(rbtn); } } } }
how to check it whether the button is checked or not in an button click?. The below code i used. but its error : 'grdFeedback.Rows[0].Cells[4].Controls[2]' threw an exception of 'System.ArgumentOutOfRangeException' bool {System.ArgumentOutOfRangeException}
((CheckBox)(grdFeedback.Rows[0].Cells[4].Controls[2])).Checked
First of all, you shouldn't be finding controls by indices. For one thing, it is confusing. Moreover, if you changed the order of your columns, you'll end up breaking your code. Instead, use the
row.FindControl
method. Another thing to check for the row type, since a grid may have several row types (header/footer/data/..) So while you checkbox exists in the DataRow, it doesn't exist in the header row for example, which in turn probably causes your exception. So you should do something like this:foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButton btn=row.FindControl("RadioButton_ID_Goes_Here") as RadioButton;
bool isChecked = btn.Checked;
//do stuff
}
} -
First of all, you shouldn't be finding controls by indices. For one thing, it is confusing. Moreover, if you changed the order of your columns, you'll end up breaking your code. Instead, use the
row.FindControl
method. Another thing to check for the row type, since a grid may have several row types (header/footer/data/..) So while you checkbox exists in the DataRow, it doesn't exist in the header row for example, which in turn probably causes your exception. So you should do something like this:foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButton btn=row.FindControl("RadioButton_ID_Goes_Here") as RadioButton;
bool isChecked = btn.Checked;
//do stuff
}
}thanks for ur immediate reply roman. but the this i have implemeted. but the same error.
((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked
but it returns error : ((System.Web.UI.WebControls.CheckBox)(((System.Web.UI.WebControls.RadioButton)(grdFeedback.Rows[0].Cells[3].FindControl("rbtn03"))))) is null Also i dont know y its showing "System.Web.UI.WebControls.CheckBox" in error message. plz help me Thanks in advance
-
thanks for ur immediate reply roman. but the this i have implemeted. but the same error.
((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked
but it returns error : ((System.Web.UI.WebControls.CheckBox)(((System.Web.UI.WebControls.RadioButton)(grdFeedback.Rows[0].Cells[3].FindControl("rbtn03"))))) is null Also i dont know y its showing "System.Web.UI.WebControls.CheckBox" in error message. plz help me Thanks in advance
I'm not sure how you set your controls in the datagrid, but
((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked
shouldn't be needed. Try this instead((RadioButton)grdFeedback.Rows[0].FindControl("rbtn03")).Checked
As a rule of thumb, you shouldn't rely on indexes to fetch controls. Also you need to make sure that the row is a data row, as the header row won't have the radiobutton