0

我正在做一个项目,我在gridview中有一个gridview和一个单选按钮,我想要的是,我想通过单击按钮将选定的行值发送到数据库,但由于对象引用未设置为对象的实例。因为我的代码是点击按钮的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());
    }
}   
4

2 回答 2

0

尝试这个

if (((RadioButton)gvrepair_details.Rows[i].FindControl("CheckBox1")).Checked == true)
        {
           k++;
        }

代替

if (rb.Checked == true)
        {
          k++;
        } 
于 2013-05-06T11:48:32.023 回答
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 索引。

于 2013-05-06T12:05:03.490 回答