0

I am using stringbuilder to create a radiobuttonlist dynamically for a survey I am working on. The code validates fine but will not render in HTML.

Is this because I am not supposed to use stringbuilder to generate asp web controls?

code here:

sb.Append(string.Format("<asp:RadioButtonList ID='" + qnumber + "' runat='server' RepeatDirection='Horizontal' RepeatLayout='flow' CssClass='answers'>"));
sb.Append(string.Format("<asp:ListItem Text='Strongly Agree' Value='1'></asp:ListItem>"));
sb.Append(string.Format("<asp:ListItem Text='Agree' Value = '2' ></ asp:ListItem>"));
sb.Append(string.Format("<asp:ListItem Text='Disagree' Value='3'></asp:ListItem>"));
sb.Append(string.Format("<asp:ListItem Text='Strongly Disagree' Value='4'></asp:ListItem>"));
sb.Append(string.Format("<asp:ListItem Text='Not Applicable' Value='5'></asp:ListItem></asp:RadioButtonList><asp:RequiredFieldValidator runat='server' ID='" + qnumber + "v"+ "' ControlToValidate='ID='" + qnumber + "' ErrorMessage='please answer the question' CssClass='warning' />"));
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Neil Norris
  • 411
  • 6
  • 16
  • Are you writing some sort of template generation tool like [T4](https://blogs.msdn.microsoft.com/webdev/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers/)? Otherwise not clear how you expect ASP.Net to compile resulting string produced by your code. – Alexei Levenkov Oct 10 '16 at 16:26
  • Well, what are you doing with the stringbuilder results? –  Oct 10 '16 at 16:26
  • nothing basically. the rest of the stringbuilder html renders. – Neil Norris Oct 10 '16 at 16:27
  • Completely irrelevant to this question, but `StringBuilder` has a method [`AppendFormat`](https://msdn.microsoft.com/en-us/library/system.text.stringbuilder.appendformat(v=vs.110).aspx) that does exactly what `Append(string.Format(` does. Also, format is supposed to be used so that you don't have to concatenate strings like your first line... – Heretic Monkey Oct 10 '16 at 16:42
  • Cheers Mike. I'm new to asp, always learning! – Neil Norris Oct 10 '16 at 16:42

1 Answers1

1

Problem is, you can't force your code behind to generate asp.Net code, those must be placed inside your aspx page. Asp.Net transform asp tags in html tags and listen to them on code behind, that's why you can't send asp tags to your view, the view is already processed by asp.net when code behind code is running.

Your best bet is to send pure HTML content to your page and use another way to read its contents. If you're in a hurry, I suggest you use Javascript to fill a HiddenField just before postbacking your form.

Here's an example on how your StringBuilder should look like:

sb.Append(string.Format("<input type="radio" name="answer" value="1" />Strongly Agree"));
sb.Append(string.Format("<input type="radio" name="answer" value="2" />Agree"));
sb.Append(string.Format("<input type="radio" name="answer" value="3" />Disagree"));
sb.Append(string.Format("<input type="radio" name="answer" value="4" />Strongly Disagree"));
sb.Append(string.Format("<input type="radio" name="answer" value="5" />Not Applicable"));

Then you just use below code on javascript to set the value to a HiddenField:

$('#<%= youHiddenFieldId.ClientID %>').val($("input:radio[name='answer']:checked").val());

Now you just need to call youHiddenFieldId.Value on code behind to get the value of the selected checkbox.

  • Thanks lucas. Basically a data table populates the questions for the survey and the string builder creates the answers using for (....) The questions render fine just need a way to generate the answers dynamically. – Neil Norris Oct 10 '16 at 16:41
  • I just provided some code example on my answer. See if it helps. – Lucas Sant'Anna Oct 10 '16 at 17:01
  • Yes. Place `sb.Append(string.Format("Strongly Agree"));` inside the desired loop. Be sure to change the name of the radios to something like `name="answer_" + qnumber`. – Lucas Sant'Anna Oct 10 '16 at 19:05