Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am binding the data to the HTML table using c# stringbuilder. I am getting data but i have two problems:
1) If i have the data 10 values then the 10 values binding to the HTML table but repeating 10 times
2) i am binding these values to the input type radio button in c# asp.net web form application. This binding has done perfectly but my problem with this radio button is when i check this radio button it's OK but if i click on second data's radio button first data's radio button is cleared this is my problem can any body solve it for me

What I have tried:

DataSet ds = new DataSet();
StringBuilder htmlTable = new StringBuilder();
string query = "SELECT DISTINCT * FROM Tablename ORDER BY Number ASC";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.Fill(ds);
htmlTable.Append("<table border='0'>");
if (!object.Equals(ds.Tables[0], null))
{
    if (ds.Tables[0].Rows.Count > 0)
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlTable.Append("<tr style='color:black;'>");
            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Number"] + ')' + ' ' + ds.Tables[0].Rows[i]["Question"] + "</td>");
            htmlTable.Append("</tr>");
            htmlTable.Append("<tr style='color:black;'>");
            htmlTable.Append("<td>" + "<input type=\"radio\" id=\"rb1\" name=\"options\" />" + " A) " + "<label for='rb1'>" + ds.Tables[0].Rows[i]["ChoiceA"] + "</label>" + "</td>");
            htmlTable.Append("</tr>");
            htmlTable.Append("<tr style='color:black;'>");
            htmlTable.Append("<td>" + "<input type=\"radio\" id=\"rb2\" name=\"options\" />" + " B) " + "<label for='rb2'>" + ds.Tables[0].Rows[i]["ChoiceB"] + "</label>" + "</td>");
            htmlTable.Append("</tr>");
            htmlTable.Append("<tr style='color:black;'>");
            htmlTable.Append("<td>" + "<input type=\"radio\" id=\"rb3\" name=\"options\" />" + " C) " + "<label for='rb3'>" + ds.Tables[0].Rows[i]["ChoiceC"] + "</label>" + "</td>");
            htmlTable.Append("</tr>");
            htmlTable.Append("<tr style='color:black;'>");
            htmlTable.Append("<td>" + "<input type=\"radio\" id=\"rb4\" name=\"options\" />" + " D) " + "<label for='rb4'>" + ds.Tables[0].Rows[i]["ChoiceD"] + "</label>" + "</td>");
            htmlTable.Append("</tr>");
        }
        htmlTable.Append("</table>");
    }
    else
    {
        htmlTable.Append("<tr>");
        htmlTable.Append("<td style='text-align:center;' colspan='5'>There is No Records.</td>");
        htmlTable.Append("</tr>");
        htmlTable.Append("</table>");
    }
}
Posted
Updated 22-Nov-18 4:25am
Comments
F-ES Sitecore 22-Nov-18 6:42am    
My main advice would be to not do things this way at all, use a repeater to generate rows of controls.
Member 8583441 22-Nov-18 6:52am    
yes sir i have used repeater for this but this binding is done at different method and that is assigned to repeater itemtemplate and binds the data to the repeater

1 solution

Your code is working perfectly fine; you are just using the wrong markup.
Have you looked at the HTML that is rendered from this? It would be helpful so that you can see what is going on.

I see 3 issues with the HTML that would be rendered:
1- All radio buttons have the same name="options". This is giving you the visible problem that you check a button on "Question 4" and your choice for all other questions are erased.
2- You have 4 options and they have IDs of rb1..rb4. These IDs are used by the label element and will select the corresponding button when clicked. These same IDs are used in subsequent rows. The DOM (Document Object Model) only allows for an ID to be assigned to 1 element. The net result is that clicking on "Choice 3" for Question 1 could check "Choice 3" on Question 4.
3- The radio buttons have no values assigned to them, so whatever is going to process this form ain't going to know what was checked.

An easy fix to problems 1 & 2 is to use your row iterator int i as part of the name/id/for attributes of these elements.
Problem 3 would be fixed by adding the value attribute to the input element

How I would do this it actually utilize another StringBuilder to build a temple before you go through your iterations, and to utilize the String.Format() method within the loop to have a cleaner appearance.

Other things changed:
1- I also replaced the DataSet with a DataTable; as you would only need a DataSet if there was more than 1 result set, further cleaning up the code.
2- I used string literals (@) to avoid single quotes.
3- Your original code has each option in it's own TD. Which gives you five rows 1 of 1 column. This is fine, however; it does not align with the "no questions" result of being 1 row of 5 columns. So I removed the TR wrapper on the question and answers

You still have some room for improvement.
1- I stripped out the color=black on every element. You would only need to do this once, at the table level. You could assign this in the STYLE block on the page.
2- You could close the table after both IF blocks and remove a line of code
C#
DataTable dt = new DataSet();
StringBuilder htmlTable = new StringBuilder();
string query = "SELECT DISTINCT * FROM Tablename ORDER BY Number ASC";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.Fill(dt);
htmlTable.Append("<table border='0'>");
if (!object.Equals(dt, null))
{
    StringBuilder sbQA = new StringBuilder();
    sbQA.AppendLine(@"<tr>");
    sbQA.AppendLine(@"<td>{0} Question</td></tr>");
    sbQA.AppendLine(@"<td><input type=""radio"" id=""rb1_{2}"" name=""options{2}"" value=""A""> A) <label for=""rb1_{2}"">{3}</label></td>");
    sbQA.AppendLine(@"<td><input type=""radio"" id=""rb2_{2}"" name=""options{2}"" value=""B""> B) <label for=""rb2_{2}"">{4}</label></td>");
    sbQA.AppendLine(@"<td><input type=""radio"" id=""rb3_{2}"" name=""options{2}"" value=""C""> C) <label for=""rb3_{2}"">{5}</label></td>");
    sbQA.AppendLine(@"<td><input type=""radio"" id=""rb4_{2}"" name=""options{2}"" value=""D""> D) <label for=""rb4_{2}"">{6}</label></td>");
    sbQA.AppendLine(@"</tr>");
    string aQuestion = sbQA.ToString();
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            htmlTable.AppendFormat(aQuestion, dt.Rows[i]["Number"], i, dt.Rows[i]["ChoiceA"], dt.Rows[i]["ChoiceB"], dt.Rows[i]["ChoiceC"], dt.Rows[i]["ChoiceD"] );
        }
        htmlTable.Append("</table>");
    }
    else
    {
        htmlTable.Append(@"<tr><td style=""text-align:center;"" colspan=""5"">There are No Records.</td></tr>");
        htmlTable.Append("</table>");
    }
}
 
Share this answer
 
Comments
Member 8583441 22-Nov-18 23:27pm    
same errors coming when i executed your code also like repeating the questions with options and radio buttons is checked with one question. The remaining questions radio buttons are not checked if i check that question previous questions is unchecked this was the problem. Actually my code is from the method calling to bind to the repeater concept. I will post that part also. This part is after the method call.

Repeater RptrQuestionsAndOptions = new Repeater();
TemplateBuilder header = new TemplateBuilder();
header.AppendLiteralString("");
TemplateBuilder itemTemplate = new TemplateBuilder();
itemTemplate.AppendLiteralString(GetQuestionNumberQuestionChoices());
TemplateBuilder footer = new TemplateBuilder();
footer.AppendLiteralString("Questions");

RptrQuestionsAndOptions.HeaderTemplate = header;
RptrQuestionsAndOptions.ItemTemplate = itemTemplate;
RptrQuestionsAndOptions.FooterTemplate = footer;

string query = "SELECT DISTINCT * FROM TableName ORDER BY Number ASC";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sda.Fill(ds);

RptrQuestionsAndOptions.DataSource = ds;
RptrQuestionsAndOptions.DataBind();
divTabContent.Controls.Add(RptrQuestionsAndOptions);
Member 8583441 23-Nov-18 0:16am    
sorry footer.AppendLiteralString("Questions"); is wrongly typed.The correct one is footer.AppendLiteralString("Closing Table"); HTML is not printing here so writing in text format and also header.AppendLiteralString("opening of table with thead tag inside tr and td");
Member 8583441 22-Nov-18 23:28pm    
GetQuestionNumberQuestionChoices() in this method i have written the piece of code that is posted in the question
Member 8583441 22-Nov-18 23:33pm    
This solution helped me a lot to learn the concept of stringbuilder i am very much thankful to you sir for making me understand what is stringbuilder and how to append the HTML tags and also appendingline finally the appendformat in this solution. Thank you sir once again to make me learn new concept how to use this stringbuilder
MadMyche 23-Nov-18 0:21am    
Using a Repeater Control and Templates would have been important things to know when you made the original post; and ALL of the code should have been included.

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