2014-03-27 47 views
0

我有一个gridview,其中使用单选按钮来选择特定的数据行。现在我需要将单选按钮保存到数据库中。我如何实现这一目标?我没有使用JQuery或阿贾克斯,我的整个编码是在ASP.NET将选定的网格视图值存储到数据库中

+1

发表您的作品,你已经做了到现在为止 – Burusothman

+1

@Yamini你应该在的地方单选按钮的使用chekbox和u可以做这项工作无论是在点击按钮或GridView中的任何情况下.. – Shirish

+0

http://msdn.microsoft。 COM/EN-US /库/ system.web.ui.webcontrols.checkbox.checked(v = vs.110)的.aspx – Burusothman

回答

1

我没有得到你的问题正确,但理想的方法是使用复选框代替单选按钮的会更好的编码目的。

下面的代码是一个演示,用于从网格视图将值插入到数据库中,其中将插入已检查的特定行。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionstring"].ToString()); 

    SqlCommand cmd = new SqlCommand(); 
    string active = "N"; 
    for (int i = 0; i <= GridView1.Rows.Count - 1; i++) 
    { 
     string A = GridView1.Rows[i].Cells[0].Text; 
     string B = GridView1.Rows[i].Cells[1].Text; 
     GridViewRow row = GridView1.Rows[i]; 
     CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1"); 
     if (Ckbox.Checked == true) 
     { 
      cn.Open(); 
      cmd.CommandText = "Insert into table_name(A,B) values (@A,@B)"; 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = cn; 

      cmd.Parameters.Clear(); 
      cmd.Parameters.AddWithValue("@A", A); 
      cmd.Parameters.AddWithValue("@B", B); 
      cmd.ExecuteNonQuery(); 
      cn.Close(); 



    } 


} 

希望这会对你有所帮助。

0

  1. 搜索通过GridRows每行
  2. 的FindControl单选按钮。
  3. 检查该按钮是否已选中。

现在试试吧。

相关问题