Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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

ASP.NET
<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 dont know how to bind radiobuttonlist and group the answers .
note : the common column between Question table and Answer table is Question_id
Posted

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

}
 
Share this answer
 
You can bind then in gridview rowdatabound event like this.

C#
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;
           }
}


I hope you got my point.
for more info.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900