0
我有一個gridview,其中使用單選按鈕來選擇特定的數據行。現在我需要將單選按鈕保存到數據庫中。我如何實現這一目標?我沒有使用JQuery或阿賈克斯,我的整個編碼是在ASP.NET將選定的網格視圖值存儲到數據庫中
做我有一個gridview,其中使用單選按鈕來選擇特定的數據行。現在我需要將單選按鈕保存到數據庫中。我如何實現這一目標?我沒有使用JQuery或阿賈克斯,我的整個編碼是在ASP.NET將選定的網格視圖值存儲到數據庫中
做我沒有得到你的問題正確,但理想的方法是使用複選框代替單選按鈕的會更好的編碼目的。
下面的代碼是一個演示,用於從網格視圖將值插入到數據庫中,其中將插入已檢查的特定行。
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();
}
}
希望這會對你有所幫助。
易
現在試試吧。
發表您的作品,你已經做了到現在爲止 – Burusothman
@Yamini你應該在的地方單選按鈕的使用chekbox和u可以做這項工作無論是在點擊按鈕或GridView中的任何情況下.. – Shirish
http://msdn.microsoft。 COM/EN-US /庫/ system.web.ui.webcontrols.checkbox.checked(v = vs.110)的.aspx – Burusothman