绑定单选按钮列表里面的DataList [英] Bind RadiobuttonList inside DataList

查看:119
本文介绍了绑定单选按钮列表里面的DataList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好。
我在这里发帖前,搜索了很多次。
我工作的一个项目像一个调查[问答]
我能得到DataList控件中的所有问题,现在我正在寻找一种方式来显示每一个问题里面单选按钮列表中的答案。

Good morning . I search many times before posting here . I am working on a project like a survey [Questions and Answers] I am able to get all questions in datalist , now i am searching a way to display the answers in Radio button list inside each question .

下面是页面加载

protected void Page_Load(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["TMConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(CS);

        //Getting All Questions

        SqlDataAdapter dr = new SqlDataAdapter("select * from Question ", con);
        DataSet ds = new DataSet();
        dr.Fill(ds, "Qs");
        OuterDataList.DataSource = ds.Tables["Qs"];
        OuterDataList.DataBind();
    }

下面是页面主体

<body>
<form id="form1" runat="server">
<h1>Test Page</h1>
    <asp:DataList ID="OuterDataList" RunAt="server">
    <ItemTemplate>
      <h4><%# DataBinder.Eval (Container.DataItem, "Question") %></h4>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
    </ItemTemplate>
  </asp:DataList>
 </form>

我不知道如何绑定单选按钮列表和群组的答案。
注意:问题表,并回答表之间的共同列Question_id

i dont know how to bind radiobuttonlist and group the answers . note : the common column between Question table and Answer table is Question_id

推荐答案

Firt将类似下面的模板。

Firt will make a template like below.

<asp:DataList runat="server" ID="DataList1" RepeatDirection="Vertical" 
DataKeyField="QuestionID" onitemdatabound="DataList1_ItemDataBound">
<ItemTemplate>
    <table>
        <tr>
            <td>
                <%# Eval("Question") %>
            </td>
        </tr>
        <tr>
            <td>
                <asp:RadioButtonList runat="server" ID="RadioButtonList1">
                </asp:RadioButtonList>
            </td>
        </tr>
    </table>
</ItemTemplate>

这是使用DataList1_ItemDataBound可以绑定你的答案后

After that using DataList1_ItemDataBound you can bind your answers.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
            //Get questionID here
            int QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID"));
            //pass Question ID to your DB and get all available options for the question
            //Bind the RadiobUttonList here
        }

    }

这篇关于绑定单选按钮列表里面的DataList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆