<asp:DataList ID="DataList1" runat="server" DataKeyField="testitemid">
<ItemTemplate>
<asp:Label ID="itemnameLabel" runat="server" Text='<%# Eval("itemname") %>'></asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName='<%# Eval("itemname") %>' Text='<%# Eval("select1") %>' />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName='<%# Eval("itemname") %>' Text='<%# Eval("select2") %>' />
<asp:RadioButton ID="RadioButton3" runat="server" GroupName='<%# Eval("itemname") %>' Text='<%# Eval("select3") %>' />
<asp:RadioButton ID="RadioButton4" runat="server" GroupName='<%# Eval("itemname") %>' Text='<%# Eval("select4") %>' />
<asp:RadioButton ID="RadioButton5" runat="server" GroupName='<%# Eval("itemname") %>' Text='<%# Eval("select5") %>' />
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:DataList>
</div>
<asp:Label ID="statement" runat="server"></asp:Label><br />
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
}
protected void submit_Click(object sender, EventArgs e)
{
RadioButton rbtn1 = (RadioButton)DataList1.Items[0].FindControl("RadioButton1");
RadioButton rbtn2 = (RadioButton)DataList1.Items[0].FindControl("RadioButton2");
RadioButton rbtn3 = (RadioButton)DataList1.Items[0].FindControl("RadioButton3");
RadioButton rbtn4 = (RadioButton)DataList1.Items[0].FindControl("RadioButton4");
RadioButton rbtn5 = (RadioButton)DataList1.Items[0].FindControl("RadioButton5");
if (rbtn1.Checked == true)
{
statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString();
}
}我选择RadioButton1提交后,提示上面红色代码地方“未将对象引用设置到对象的实例。”
{
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
this.DataList1.DataSource = ds.Tables[0].DefaultView;改为this.DataList1.DataSource = ds.Tables["test_temp2"].DefaultView试试
{
statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString();
}
这里出错
提示“statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString(); ”这里“未将对象引用设置到对象的实例。”
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
}
protected void submit_Click(object sender, EventArgs e)
{
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
RadioButton rbtn1 = (RadioButton)DataList1.Items[0].FindControl("RadioButton1");
RadioButton rbtn2 = (RadioButton)DataList1.Items[0].FindControl("RadioButton2");
RadioButton rbtn3 = (RadioButton)DataList1.Items[0].FindControl("RadioButton3");
RadioButton rbtn4 = (RadioButton)DataList1.Items[0].FindControl("RadioButton4");
RadioButton rbtn5 = (RadioButton)DataList1.Items[0].FindControl("RadioButton5");
if (rbtn1.Checked == true)
{
statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString();
}
}
这样看看
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
写在方法:
Page_Init(object sender, EventArgs e)
中去 Page_Init(object sender, EventArgs e)
{
testid = System.Convert.ToInt32(Request.QueryString["testid"]);
SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
SqlCommand cmd = new SqlCommand("select * from test_temp2 where testid=" + testid, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "test_temp2");
this.DataList1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
这里Items[0]的index参数错误
9楼的方法方法可行
多谢了!