我正在研究一个项目,我在gridview中有一个gridview和一个radiobutton,我想要的是,我想在点击按钮时将选定的行值发送到数据库但我得到一个错误作为对象参考未设置为对象的实例。 就像我的代码一样 单击按钮时的cs代码
protected void btn_selectgridview_Click(object sender, EventArgs e)
{
int k = 0;
//Checkther whether atleast one check box is selected or not
for (int i = 0; i <=gvrepair_details.Rows.Count-1; i++)
{
GridViewRow row = gvrepair_details.Rows[i];
RadioButton rb = (RadioButton)row.FindControl("CheckBox1");
if (rb.Checked == true)
{
k++;
}
}
if (k == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),Guid.NewGuid().ToString(), "<script language=JavaScript>alert('select the value in grid');</script>");
return;
}
for (int i = 0; i <=gvrepair_details.Rows.Count-1; i++)
{
string bookname = gvrepair_details.Rows[i].Cells[1].Text;
string categoryname = gvrepair_details.Rows[i].Cells[2].Text;
string subcategory =gvrepair_details.Rows[i].Cells[3].Text;
string shelf_no = gvrepair_details.Rows[i].Cells[4].Text;
string isbn = gvrepair_details.Rows[i].Cells[5].Text;
string edition = gvrepair_details.Rows[i].Cells[6].Text;
string status = gvrepair_details.Rows[i].Cells[7].Text;
GridViewRow row = gvrepair_details.Rows[i];
RadioButton rb = (RadioButton)row.FindControl("CheckBox1");
if (rb.Checked == true)
{
InsertData(bookname, categoryname, subcategory, shelf_no, isbn, edition, status);
}
}
}
void InsertData(String bookname, String categoryname, String subcategory,String shelf_no,String isbn,String edition,String status)
{
try
{
sql = "insert into library_repair(bookname, categoryname, subcategoryname, shelf_no, isbn, edition, status)values('" +bookname+ "','" +categoryname+ "','" +subcategory+ "','"+shelf_no+"','"+isbn+"','"+edition+"','"+status+"')";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
答案 0 :(得分:0)
试试这个
if (((RadioButton)gvrepair_details.Rows[i].FindControl("CheckBox1")).Checked == true)
{
k++;
}
而不是
if (rb.Checked == true)
{
k++;
}
答案 1 :(得分:0)
我重新构造了你的第一个函数(代码可能包含语法错误,因为还没有运行它):
protected void btn_selectgridview_Click(object sender, EventArgs e)
{
int k = 0;
int checkBoxColumnIndex = 2; //Here you should specify the cell containing the checkBox
foreach (GridViewRow item in gvrepair_details.Rows)
{
RadioButton rb = (RadioButton)item.Cells[checkBoxColumnIndex].FindControl("CheckBox1");
if (rb != null && rb.Checked) //Check if rb is null
{
k++;
string bookname = item.Cells[1].Text;
string categoryname = item.Cells[2].Text;
string subcategory = item.Cells[3].Text;
string shelf_no = item.Cells[4].Text;
string isbn = item.Cells[5].Text;
string edition = item.Cells[6].Text;
string status = item.Cells[7].Text;
InsertData(bookname, categoryname, subcategory, shelf_no, isbn, edition, status);
}
}
//Checkther whether atleast one check box is selected or not
if (k == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>alert('select the value in grid');</script>");
return;
}
}
请注意checkBoxColumnIndex
。应提供CheckBox1索引。