0

I have a Control that is populated with a series of controls. What i was wanting to do is get the string output. I tried an example that i was working on, but it seems that since some of the controls are input textboxes that there are issues. It is telling me that it needs a form with runat="server" set. My conclusion is that it isnt reading the markup of sorts.

Is there a way to get around it? I was thinking to wrap the data backend with a form...

so instead of root -> (control1, control2), it would be somethiing like root -> form -> (control1, control2).

The attemps i have been doing:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWritertw = new HtmlTextWriter(sw);
root.RenderControl(tw);

the error occurs at the root.

In the markup i tried to do stuff like:

<form runat=server><asp:pladeholder id="root" /></form>

but that didnt work at all.

I have yet to try the root->form->(1,2) but going to attempt it now. Edit: I did this attempt, and it no longer caused the one HTMLException, but now an invalid Operation Exception: RegisterForEventValidation can only be called during Render();

I also tried overriding VeriftRenderingInServerForm(Control control){} but that wasnt working either, but maybe i was doing this part wrong?

2 Answers 2

0

I suggest you this code - based on Controls property of Place Holder

var controlsFlow = new StringBuilder() ;
foreach(var control in yourPlaceHolder.Controls)
{
  controlsFlow.Append(control.Id);
}

Nota : Add runat="server" to your PlacHolder

8
  • For some reason, the placeholder is defined serverside, it isnt on the markup Oct 31, 2012 at 16:56
  • would that work anyways, I mean, would we need recursion? I dunno how deep the controls go Oct 31, 2012 at 16:57
  • if you have just one level, you don't need , but you can control your level hen you create your control Oct 31, 2012 at 16:59
  • Your code does not work at the control.Id because it doesnt understand what var is. It seems to work if you change it to: foreach(Control control in placeHolder.Controls){controlsFlow.Appen(control.ID);} Oct 31, 2012 at 17:09
  • Doing this with your placeholder works. Seems like there is still some sort of issue when it comes to input type="text" with the form stuff Oct 31, 2012 at 17:24
0

You are trying to output ASP.net controls, and these require the runat="server" controls because they are not legitimate HTML controls.

If you don't want to access them in the code behind, then you can use regular HTML tags instead. E.g

2
  • Yea, i build the stuff serverside and just trying to putput it to the screen to manipulate. For some reason it isnt quite working Oct 31, 2012 at 16:58
  • my goal is to render the asp.net controls. Im not trying to straight up view them. Nov 2, 2012 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.