如何得到gridview中radiobutton选中行的一个值

我选用自定义模版在gridview添加了一列radiobutton,现在的问题是当选中一个radiobutton时希望将该选中行的一个值传递给一个textbox,不知道该用gridview里的什么事件,也不知道在事件里怎么写获取的代码

相信很多初学者在使用GridView时遇到这样的问题,就是取不到在模板列中的RadioButton或者CheckBox得Checked属性

好的先来看了例子

新建一个网站,然后添加一个GridView,添加一个模板列,在itemTemplate中添加一个RadioButton1,数

据绑定:Eval(“customerid”)(使用北风数据库),就可以得到下面的效果了

看代码

protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}

protected void Button1_Click(object sender, EventArgs e)
{
if (((RadioButton)GridView1.Rows[0].FindControl("RadioButton2")).Checked)
{
Response.Write("RadioButton2被选中拉");
}
int i = 1;
foreach (GridViewRow dr in GridView1.Rows)
{
if (((RadioButton)dr.FindControl("RadioButton2")).Checked)
{
Response.Write("<br>"+(i++)+"被选中了!");
}
}
}
public void BindGrid()
{
string connectionString = "server=.\\sqlexpress;database=northwind;uid=sa;pwd=123";
SqlConnection conn=new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select top(10) customerid from customers", conn);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}

运行
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-06-12
protected void Gridview1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Result")
{
Panel p = (Panel)e.Item.FindControl("Panel1");
if (Session["P"] == null)
{
p.Visible = true;
Session["P"] = 1;
}
else
{
p.Visible = false;
Session["P"] = null;
}
}
if (e.CommandName == "Voting")
{
CheckBoxList chk = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
Label lbl = (Label)e.Item.FindControl("Label_Topic");
int TotalCount = 0,Count=0;
if (chk.SelectedIndex == -1)
{
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdateSucceed", "alert('尊敬的用户,请至少选择一项!')", true);
return;
}
else
{
DataSet ds_Topic = CoSt.Read("select * from OnlineSurvey where Topic='"+lbl.Text+"'");
if (ds_Topic.Tables.Count > 0 && ds_Topic.Tables[0].Rows.Count > 0)
{
TotalCount = Convert.ToInt32(ds_Topic.Tables[0].Rows[0]["TotalCount"].ToString());
}
TotalCount = TotalCount + 1;
int flag= CoSt.UpdateTotalCount(lbl.Text, TotalCount);
for (int i = 0; i < chk.Items.Count; i++)
{
if (chk.Items[i].Selected)
{
DataSet ds_Item = CoSt.Read("select * from Items where ItemID='"+chk.Items[i].Value+"'");
if (ds_Item.Tables.Count > 0 && ds_Item.Tables[0].Rows.Count > 0)
{
Count =Convert.ToInt32( ds_Item.Tables[0].Rows[0]["Count"].ToString());
}
Count = Count + 1;
CoSt.UpdateItemCount(Convert.ToInt32( chk.Items[i].Value), Count);
}
}
if (flag > 0)
{
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdateSucceed", "alert('投票成功,谢谢您的参与!')", true);
DataB();
}
else
{
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdateSucceed", "alert('投票失败,请重新后再试!')", true);
}
}
}
}
相似回答
大家正在搜