Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using one label and one checkbox but it showing single label and checkbox ,but label and checkbox should generate dynamically depending upon the question's option.

What I have tried:

private void SetAnswer()
        {
            StrQuery = "SELECT AnswerID,Answer FROM TBL_Answer where QuestionID='" + QID + "'";
            oDt = ClsConnection.ExecuteSelectQry(StrQuery);
            for (int i = 0;i< oDt.Rows.Count; i++)
            {
                //if (oDt.Rows.Count > 0)
                {
                    Label lbl = new Label();
                    LblAnswer.Text = oDt.Rows[i]["Answer"].ToString();
                  
                    chckbox.Visible = true;
                }
            }
        }
Posted
Updated 18-May-17 3:33am
v2
Comments
F-ES Sitecore 18-May-17 8:59am    
Your code is simply updating the label and checkbox you have on the page, so each iteration of the loop overwrites the last. There is nothing in your code that is creating new controls for each row. The easiest way to do this is to use a Repeater and bind your data to the repeater, then in the ItemTemplate of the Repeater you put your label and checkbox and bind them to the data for that row. If you google asp:Repeater you'll find lots of examples.

I'd suggest to search CP Knowledge Base[^]. There you'll find tons of examples.
 
Share this answer
 
Comments
Vanaja Dasi 19-May-17 2:15am    
thanks
Maciej Los 19-May-17 2:16am    
You're very welcome.
Use checkbox list  
<asp:CheckBoxList ID="chkAns" runat="server">
</asp:CheckBoxList>

and code behind

ListItem item = new ListItem();
item.Text = oDt.Rows[i]["Answer"].ToString();
 item.Value = oDt.Rows[i]["AnswerID"].ToString();
 chkAns.Items.Add(item);
 
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