如何将数据随机绑定到radiobuttonlist asp.net中

时间:2022-11-25 19:38:22

I'm working on building a form. I need to show my questions and then to show 3 answers [which they're columns in the same table] in a radiobuttonlist.

我正在建立一个表格。我需要显示我的问题,然后在radiobuttonlist中显示3个答案[它们是同一个表中的列]。

I tried binding the data automatically to the Datalist.

我尝试将数据自动绑定到Datalist。

And then to check If the correct Answer is checked ... if it's not I need to show an error

然后检查是否检查了正确的答案...如果不是,我需要显示错误

<asp:DataList ID="DataList1t" runat="server" DataSourceID="AccessDataSource1" onitemdatabound="DataList1_ItemDataBound" onselectedindexchanged="DataList1t_SelectedIndexChanged1">
    <ItemTemplate>
        <table>
            <tr>
                <td>
                    <%# Eval("TheQus") %>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButtonList runat="server" ID="RadioButtonList1">
                    </asp:RadioButtonList>
                </td>
            </tr>
        </table>
        <br />
        <br />
    </ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Databasegeometreyeh.accdb" SelectCommand="SELECT TOP 4 Qus.ID, Qus.TheQus, Qus.CorrectAns, Qus.Ans2, Qus.Ans3 FROM  EkhtbrNafsk INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE  Qus.Type= 1  order By rnd(Qus.ID)"></asp:AccessDataSource>

and the Code behind is

而背后的守则是

protected void Page_Load(object sender, EventArgs e)
{
    //DataList1t.Visible = false;

    //DataTable QusTri = DAL.SelectFromTable("SELECT TOP 4 Qus.TheQus FROM  EkhtbrNafsk INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE  Qus.Type=" + 1 + " order By rnd(Qus.ID)");
    //DataList1t.DataSource = QusTri;
    //DataList1t.DataBind();

    //DataList1r.Visible = false;

    //DataTable Qusrepoe = DAL.SelectFromTable("SELECT TOP 4 Qus.TheQus FROM  EkhtbrNafsk INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE Qus.Type= " + 2 + " order By rnd(Qus.ID)");
    //DataList1r.DataSource = Qusrepoe;
    //DataList1r.DataBind();


    if (!IsPostBack)
    {
        DataTable t1 = DAL.SelectFromTable("select * from [EkhtbrNafsk]");
        DropDownList1.DataTextField = "TypeTest";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataSource = t1;
        DropDownList1.DataBind();
        DataList1t.Visible = false;
        DataList1r.Visible = false;
    }
}
protected void Button2_Click(object sender, EventArgs e)
{

    if (DropDownList1.SelectedValue == "1")
    {
        DataList1t.Visible = true;
    }
    else if (DropDownList1.SelectedValue == "2")
        DataList1r.Visible = true;
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item)
    {
        RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");

        int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ID"));
        DataTable dt = DAL.SelectFromTable("SELECT Qus.ID, Qus.CorrectAns, Qus.Ans2, Qus.Ans3 FROM  EkhtbrNafsk INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE  Qus.ID=" + QuestionID);

        RadioButtonList1.Items.Add(dt.Rows[0][0].ToString());
        RadioButtonList1.Items.Add(dt.Rows[0][1].ToString());
        RadioButtonList1.Items.Add(dt.Rows[0][2].ToString());//Bind the RadiobUttonList here


    }
}

1 个解决方案

#1


0  

Try this:

DataTable dt = DAL.SelectFromTable("SELECT Qus.ID, Qus.CorrectAns, Qus.Ans2, Qus.Ans3 FROM EkhtbrNafsk ORDER BY rand() INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE  Qus.ID=" + QuestionID );

#1


0  

Try this:

DataTable dt = DAL.SelectFromTable("SELECT Qus.ID, Qus.CorrectAns, Qus.Ans2, Qus.Ans3 FROM EkhtbrNafsk ORDER BY rand() INNER JOIN Qus ON EkhtbrNafsk.ID = Qus.Type  WHERE  Qus.ID=" + QuestionID );