td><asp:datagrid id="question" Runat="server" AutoGenerateColumns="False" Width="635">
<Columns>
<asp:BoundColumn Visible="False" DataField="q_id" HeaderText="id"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体"></FONT><FONT face="宋体"></FONT>
<BR>
<FONT style="FONT-WEIGHT: bold" color="#003333">第<%# DataBinder.Eval(Container.DataItem,"q_id") %>题</FONT>
<BR>
<%# DataBinder.Eval(Container.DataItem,"q_content")%> <BR>
<asp:RadioButtonList id="answers" Runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></td>
在我的dataitembound 中
函数是
private void question_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
        RadioButtonList radio=new RadioButtonList();
radio=(RadioButtonList)e.Item.FindControl("answers");
radio.Items[0].Text=DataBinder.Eval(e.Item.DataItem, "q_answer1").ToString() ; radio.Items[1].Text=DataBinder.Eval(e.Item.DataItem, "q_answer2").ToString() ;
radio.Items[2].Text=DataBinder.Eval(e.Item.DataItem, "q_answer3").ToString() ;       
     } 
}

解决方案 »

  1.   

    但是我在提取里面的数据时 我使用的函数是这样的
    public void correct()
    {
               int number=0;
    string sanswer="";
    string ranswer="";
    int qid;

    for(int i=0;i<question.Items.Count;i++)
    {
    qid=Int32.Parse(question.Items[i].Cells[0].Text.ToString());
    //string x="4";
       
    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    sanswer=radio.SelectedItem.Value;


    string strConn=ConfigurationSettings.AppSettings["datasource"].ToString();
    string strCmd="select  q_ranswer from question where q_id=1 ";
    OleDbConnection objConnection=new OleDbConnection(strConn);
    //try
    //{
    objConnection.Open();
    OleDbCommand objCmd=new OleDbCommand(strCmd,objConnection);
    OleDbDataReader objReader=objCmd.ExecuteReader();
    if(objReader.Read())
    {
    ranswer=objReader["q_ranswer"].ToString();
    } //}
    //catch
    //{}
    //finally
    //{
    objConnection.Close();
    // }
    if(sanswer.Equals(ranswer))
    {
    number+=1;
    }
    int score=number*5;
    string strUpdate="Update student set s_='"+score+"' where s_snumber='"+Session["snumber"]+"' ";
    try
    {
    objConnection.Open();
    OleDbCommand objUpdate=new OleDbCommand(strUpdate,objConnection);
    objUpdate.ExecuteNonQuery();
    }
    catch
    {}
    finally
    {
    objConnection.Close();
    }
    }  }
      

  2.   

    上面那代码 有点难看
    不好意思 我把主要的 截取下来了
      
    public void correct()
    {
            int number=0;
    string sanswer="";
    string ranswer="";
    int qid;

    for(int i=0;i<question.Items.Count;i++)
    {
    qid=Int32.Parse(question.Items[i].Cells[0].Text.ToString());
    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    sanswer=radio.SelectedItem.Value;////此处就是 错误出现的地方 !!!! 说什么
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
    string strConn=ConfigurationSettings.AppSettings["datasource"].ToString();
    string strCmd="select  q_ranswer from question where q_id=1 ";
    OleDbConnection objConnection=new OleDbConnection(strConn);
    objConnection.Open();
    OleDbCommand objCmd=new OleDbCommand(strCmd,objConnection);
    OleDbDataReader objReader=objCmd.ExecuteReader();
    if(objReader.Read())
    {
    ranswer=objReader["q_ranswer"].ToString();

    objConnection.Close();
      

  3.   

    在 dataitembound中使用
    RadioButtonList radio=new RadioButtonList();
    radio=(RadioButtonList)e.Item.FindControl("answers");
    radio.Items[0].Text=DataBinder.Eval(e.Item.DataItem, "q_answer1").ToString() ; radio.Items[1].Text=DataBinder.Eval(e.Item.DataItem, "q_answer2").ToString() ;
    radio.Items[2].Text=DataBinder.Eval(e.Item.DataItem, "q_answer3").ToString() ;       

    那个if 语句很重要
    没有那个 if 语句 同样会出现
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
      但是在我的correct()函数里
    没法用
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
    ...
    这个语句  因为 那个 e 就根本有定义
    强行使用也会出错
       但是我不知道怎么才能不出现 那样的错误
    哪为高手 指点 以下啊   我很急啊!!!!!!!!!
      

  4.   

    是不是没有选择数据呀
    RadioButtonList.SelectedItem.Value 
    如果没有选择的话,就没有值了
      

  5.   

    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    改为:
    RadioButtonList radio=new (RadioButtonList)question.Items[i].FindControl("answers");
    试一下~少了个new吧~
      

  6.   

    还是不行啊
      我原来那个itemdatabound 都是加个
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
    ...
    后才能运行的 
      但是在后面那个函数 我就不知道了!!
      

  7.   

    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    你调试一下看看这一句里的radio找到没,如果没找到那就会出现问题
      

  8.   

    public void correct()是在什么时候调用的呢?是在绑定前还是后
      

  9.   

    是在  帮定后
    也就是在 做完题目 后
    我要把 考生 的选择的题目 的答案 提取 出来啊
    而这些 a b  c 都是在 radiobuttonlist 里面
      

  10.   

    qid=Int32.Parse(question.Items[i].Cells[0].Text.ToString());
    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    sanswer=radio.SelectedItem.Value;
    ------------------------
    修改如下:
    qid=Int32.Parse(question.Items[i].Cells[0].Text.ToString());
    RadioButtonList radio=(RadioButtonList)question.Items[i].FindControl("answers");
    if(radio != null)
    {
    sanswer=radio.SelectedValue;
    }主要是你有没有取到对象的问题。
      

  11.   

    http://singlepine.cnblogs.com/articles/266538.html
    用客户端radio