c# gridview中的RadioButtonList要怎么绑定数据源啊? 在线等~
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"Width="100%"><Col...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" >
<Columns>···
<ItemTemplate>···
<asp:RadioButtonList ID="danxuan" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
Visible="False" Width="178%">
</ItemTemplate>···
是要绑定vot_op表里的op_id、op_name到RadioButtonList的DataValueField、DataTextField
我刚开始学,请大家帮忙啊~
linv2:sda.SelectCommand.Connection = con ;这句话报错了,“System.NullReferenceException: 未将对象引用设置到对象的实例。
”。我写的是:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
string strsql = null;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand.Connection = con ;
sda.SelectCommand.CommandText = strsql;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButtonList radlist = (RadioButtonList)GridView1.Rows[i].FindControl("danxuan");
DataTable tbl = new DataTable();
strsql = "SELECT * from [vot_op] where op_type='单选'and t_id='" + int.Parse(Session["t_id"].ToString()) + "'";
sda.Fill(tbl);
···
con这样写其他的控件也能连上啊 这里问题出在哪里了? 展开
<Columns>···
<ItemTemplate>···
<asp:RadioButtonList ID="danxuan" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
Visible="False" Width="178%">
</ItemTemplate>···
是要绑定vot_op表里的op_id、op_name到RadioButtonList的DataValueField、DataTextField
我刚开始学,请大家帮忙啊~
linv2:sda.SelectCommand.Connection = con ;这句话报错了,“System.NullReferenceException: 未将对象引用设置到对象的实例。
”。我写的是:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
string strsql = null;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand.Connection = con ;
sda.SelectCommand.CommandText = strsql;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButtonList radlist = (RadioButtonList)GridView1.Rows[i].FindControl("danxuan");
DataTable tbl = new DataTable();
strsql = "SELECT * from [vot_op] where op_type='单选'and t_id='" + int.Parse(Session["t_id"].ToString()) + "'";
sda.Fill(tbl);
···
con这样写其他的控件也能连上啊 这里问题出在哪里了? 展开
展开全部
RadioButtonList 在GridView1里边
你确定不是RadioButton?????
----------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection mycon = new SqlConnection("数据库连接字符串");
string strsql=null;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand.Connection = mycon;
sda.SelectCommand.CommandText = strsql;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButtonList radlist = (RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList1");
DataTable tbl = new DataTable();
strsql = "SQL语句";
sda.Fill(tbl);
radlist.DataSource = tbl;
radlist.DataBind();
radlist.DataTextField = "op_name";
radlist.DataValueField = "op_id";
}
}
你确定不是RadioButton?????
----------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection mycon = new SqlConnection("数据库连接字符串");
string strsql=null;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand.Connection = mycon;
sda.SelectCommand.CommandText = strsql;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButtonList radlist = (RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList1");
DataTable tbl = new DataTable();
strsql = "SQL语句";
sda.Fill(tbl);
radlist.DataSource = tbl;
radlist.DataBind();
radlist.DataTextField = "op_name";
radlist.DataValueField = "op_id";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询