绑定ridobuttonlist没有问题
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int s =Convert.ToInt32(e.Row.Cells[0].Text);
qlConnection conn = Class1.DBCon();
conn.Open();
string com = string.Format("select * from [Survey_Option] where [QuestionId]={0}", s);
SqlDataAdapter da = new SqlDataAdapter(com, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
conn.Close(); RadioButtonList rb1 = (RadioButtonList)e.Row.Cells[1].FindControl("RadioButtonList1");
rb1.DataSource = dt;
rb1.DataTextField = "OptionName";
rb1.DataValueField = "OptionId";
rb1.DataBind(); }
}
protected void Button1_Click1(object sender, EventArgs e)
{
RadioButtonList rb1; for (int i = 0; i < GridView1.Rows.Count; i++)
{
rb1 = GridView1.Rows[i].FindControl("RadioButtonList1") as RadioButtonList;
SqlConnection con = Class1.DBCon();
con.Open();
int optionid =Convert.ToInt32(rb1.SelectedValue ;//出现问题,字符串格式不正确。 string sql1 = string.Format("insert into [Survey_Vote](OptionId) values('{0}')", optionid);
SqlCommand com1 = new SqlCommand(sql1, con);
com1.ExecuteNonQuery();
}
}
补充一点 rb1 = (RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList1")也会出现上述问题 ;
不求原因,只希望能给出正确的代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int s =Convert.ToInt32(e.Row.Cells[0].Text);
qlConnection conn = Class1.DBCon();
conn.Open();
string com = string.Format("select * from [Survey_Option] where [QuestionId]={0}", s);
SqlDataAdapter da = new SqlDataAdapter(com, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
conn.Close(); RadioButtonList rb1 = (RadioButtonList)e.Row.Cells[1].FindControl("RadioButtonList1");
rb1.DataSource = dt;
rb1.DataTextField = "OptionName";
rb1.DataValueField = "OptionId";
rb1.DataBind(); }
}
protected void Button1_Click1(object sender, EventArgs e)
{
RadioButtonList rb1; for (int i = 0; i < GridView1.Rows.Count; i++)
{
rb1 = GridView1.Rows[i].FindControl("RadioButtonList1") as RadioButtonList;
SqlConnection con = Class1.DBCon();
con.Open();
int optionid =Convert.ToInt32(rb1.SelectedValue ;//出现问题,字符串格式不正确。 string sql1 = string.Format("insert into [Survey_Vote](OptionId) values('{0}')", optionid);
SqlCommand com1 = new SqlCommand(sql1, con);
com1.ExecuteNonQuery();
}
}
补充一点 rb1 = (RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList1")也会出现上述问题 ;
不求原因,只希望能给出正确的代码
{
string strid= ((RadioButtonList)gvr.FindControl("RadioButtonList1")).SelectedValue;
}
试试