<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提交后,提示上面红色代码地方“未将对象引用设置到对象的实例。”

解决方案 »

  1.   

    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();
            }
      

  2.   

    da.Fill(ds, "test_temp2");
    this.DataList1.DataSource = ds.Tables[0].DefaultView;改为this.DataList1.DataSource = ds.Tables["test_temp2"].DefaultView试试
      

  3.   

    写错了。应该是 if (rbtn1.Checked == true)
            {
                statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString();
            }
    这里出错
      

  4.   

    结果还是一样
    提示“statement.Text = ds.Tables["test_temp2"].DefaultView[0].Row[4].ToString(); ”这里“未将对象引用设置到对象的实例。”
      

  5.   

    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)
        {
                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();
            }
        }
    这样看看
      

  6.   

    你把Page_Load方法中的内容
                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();
    }
      

  7.   

    7楼的方法提示RadioButton rbtn1 = (RadioButton)DataList1.Items[0].FindControl("RadioButton1");
    这里Items[0]的index参数错误
    9楼的方法方法可行
    多谢了!
      

  8.   

    因为这个DataSet ds = new DataSet();写在protected void Page_Load(object sender, EventArgs e){}外面了,你提交一次它就重新new一次,所以你下面在用的话就是新的ds,所以会报这个错误。