如何使用Datarow和DataTable来填充radiobuttonlist?

时间:2013-12-10 11:25:42

标签: c# asp.net datatable datarow

我创建了一个测验示例代码,其中有一个label和一个RadioButtonList用于提问和答案,

 public DataSet GetDataSet(string query)
  {
      DataSet QuestionSet = new DataSet();
      DataTable QTable = new DataTable();
      QTable.Columns.Add("ROW_NUMBER");
      QTable.Columns.Add("QuestionNo");
      QTable.Columns.Add("Tname");
      QTable.Columns.Add("Tnumber");
      QTable.Columns.Add("question");
      QTable.Columns.Add("ans1");
      QTable.Columns.Add("ans2");
      QTable.Columns.Add("ans3");
      QTable.Columns.Add("ans4");

     DataRow dr;

    for (int i = 0; i < 10; i++)
      {
         dr = QTable.NewRow();
         dr[0] =  i;
         dr[1] = "Qno" + i;
         dr[2] = "TST001";
         dr[3] = "TST001";
       //dr[4] = "Is n't so nice to have this test project in place ?";
         dr[4] = "Question";
         dr[5] = "Option1";
         dr[6] = "Option2";
         dr[7] = "Option3";
         dr[8] = "Option45";

            QTable.Rows.Add(dr);
      }

    QuestionSet.Tables.Add(QTable);
   return QuestionSet;

    }

开启下一步按钮单击

 protected void Button1_Click(object sender, EventArgs e) 
  {
     LoadQuestion();
  } 
 protected void LoadQuestion()
  { 
    if (Questions.Tables[0].Rows.Count>0) 
        {
          DataRow DR = Questions.Tables[0].Rows[0]; Question.Text=DR[0].ToString()+" of  "+totalQs; sno = DR[1].ToString();
         Questionlbl.Text = DR[4].ToString();        
         rbtnAns.Items.Clear();rbtnAns.Items.Add(DR[5].ToString());  
         rbtnAns.Items.Add(DR[6].ToString());  
         rbtnAns.Items.Add(DR[7].ToString());
         rbtnAns.Items.Add(DR[8].ToString());
         Question‌​s.Tables[0].Rows.Remove(DR);    
    }
  }

我想要的是,当用户点击下一个按钮时,应显示带有四个选项的下一个问题,我应该如何使用datarow来获取此信息。

1 个答案:

答案 0 :(得分:0)

是的,请参阅下面我添加了一些示例代码。你可以尝试一下。 将此代码放在 aspx 页面中,并根据需要配置datasource

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        AllowPaging="true"
        AutoGenerateRows="false" 
        DataSourceID="DetailsViewSource">
        <Fields>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblQUestion" runat="server" Text='<%#Eval("question") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <input type="radio" name="anwser" value='<%#Eval("ans1") %>'>
                    <label>
                        <%#Eval("ans1") %></label><br />
                    <input type="radio" name="anwser" value='<%#Eval("ans2") %>' />
                    <label>
                        <%#Eval("ans2") %></label><br />
                    <input type="radio" name="anwser" value='<%#Eval("ans3") %>' />
                    <label>
                        <%#Eval("ans3") %></label><br />
                    <input type="radio" name="anwser" value='<%#Eval("ans4") %>' />
                    <label>
                        <%#Eval("ans4") %></label><br />
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
        <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last"
            NextPageText="Next" PreviousPageText="Previous" />
        <PagerStyle BackColor="AliceBlue" ForeColor="HotPink" />
    </asp:DetailsView>
    <asp:SqlDataSource ID="DetailsViewSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringKeyHere%>"
        InsertCommand="Your Insert Query" 
        SelectCommand="Your Select Query">
     </asp:SqlDataSource>