在DataList中绑定RadiobuttonList [英] Bind RadiobuttonList inside DataList

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

问题描述



早上好。

我在发布这里之前多次搜索。

我正在开展一项类似调查的项目[问题与解答]

我能够在datalist中获得所有问题,现在我正在寻找在每个问题内的单选按钮列表中显示答案的方法。



这里是页​​面加载

Hi,
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 .

here is page load

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





这是页面正文





here is page body

<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>
</body>





i不知道如何绑定radiobuttonlist并将答案分组。

注意:Question表和Answer表之间的公共列是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

推荐答案

查看以下链接

https://humrahimcs.wordpress.com/tag/asp-net-datalist-nested-with-radiobuttonlist/ [ ^ ]



希望有帮助
Check the below link
https://humrahimcs.wordpress.com/tag/asp-net-datalist-nested-with-radiobuttonlist/[^]

Hope it helps


将隐藏字段绑定到问题ID并找到它ItemDataBound

Put a Hidden field bind the question id and find it ItemDataBound
<asp:hiddenfield id=""hnQuestionsid"" runat=""server"" value="<%# DataBinder.Eval(Container.DataItem, "Questionsid")%>" xmlns:asp="#unknown" />



代码方


in code side

protected void OuterDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataListItem drv = e.Item.DataItem as DataListItem;
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
HiddenField hnQuestionsid = (HiddenField)e.Item.FindControl("hnQuestionsid");
DataSet ds = loadQuestionOptionbyqid(hnQuestionsid.Value);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
RadioButtonList1.DataSource = ds;
RadioButtonList1.DataTextField = "questionName";// something like this
RadioButtonList1.DataValueField = "questionID";
RadioButtonList1.DataBind();
}

}


你可以在gridview rowdatabound事件中绑定,就像这样。



You can bind then in gridview rowdatabound event like this.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               int rdbValue= (int)DataBinder.Eval(e.Row.DataItem, "rdbID");
               RadioButtonList rb = (RadioButtonList)e.Row.FindControl("RadioButtonListID");
               rb.Items.FindByValue(rdbValue.ToString()).Selected = true;
           }
}





我希望你明白我的意思。

更多info。



http ://www.c-sharpcorner.com/UploadFile/54db21/gridview-with-radiobutton-binding/ [ ^ ]


这篇关于在DataList中绑定RadiobuttonList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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