1

I have the following C# code on one of my pages:

protected override void Render(HtmlTextWriter writer) 
    {
        //an array named fieldNames is delcared here

        writer.Write("<form id=\"Form1\" runat=\"server\" action=\"\">");
        writer.Write("<asp:checkboxlist id=\"checkBoxes\" runat=\"server\">");
        for (int count = 0; count < fieldNames.GetLength(0); count++)
        {  //iterates over the array of field names
            writer.Write("<asp:listitem text=" + fieldNames[count] + " value=" + fieldNames[count] + "/>");
        }
        writer.Write("</asp:checkboxlist>");
        writer.Write("</form>");
    }

The intent is to create a list of checkboxes which have had their attributes set dynamically.

When run this does not throw any errors but no controls appear on the page.

When I view the source of the page I get the following html:

<form id="Form1" runat="server" action="">
    <asp:checkboxlist id="checkBoxes" runat="server">
        <asp:listitem text='Spares Part No' value='Spares Part No'/>
        <asp:listitem text='Description' value='Description'/>
        <asp:listitem text='Site' value='Site'/>
        <asp:listitem text='Rack/Bin Number' value='Rack/Bin Number'/>
    </asp:checkboxlist>
</form>

Out of interest I posted this in another application and it runs fine with all the controls being displayed.

Is this a problem with the order in which events are called? I am at a bit of a loss as to what to try next so any advice would be great.

Thanks,

Oliver

3 Answers3

1

Basically you can't do this.

The Render event comes very late in the page life cycle. You can't just output ASPX markup because the events that parse the markup, instantiate controls etc have already run.

What you should do is add a PlaceHolder control to your page in the markup, and then in an earlier event (e.g. Init or Load) add the controls you want to that placeholder. Again you can't just write out the ASPX markup however, you need to instantiate the controls, along the lines of:

var checkbox = new CheckboxList { Id = "checkBoxes" };
uxPlaceHolder.Controls.Add(checkbox);
checkbox.Items.Add(new ListItem { Text = "...", Value = "..." });

One way you could achieve what you want would be to use a VirtualPathProvider to generate the markup for .aspx requests as they are requested by the framework. Or you could look at what HTML output you actually want to generate (i.e. a list of input elements with some associated javascript) and render these directly. Both of these should probably be classified as nasty hacks however.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • fair enough - is there any way to call this sort of script earlier? i thought about using the page_load event but i'm afraid i have no idea in what order events are called – Oliver Woolland Aug 12 '11 at 10:22
  • You can't add declarative ASPX markup during the page lifecycle. Why? Because the 'page' is a .Net object, it is compiled by combining the declarative markup with the code behind. By the time the code behind runs the markup has been parsed and the Page object is created. From then on you need to work the the control tree (i.e. the instantiated objects that were created by parsing the markup). This is good - it's easy and less prone to error than munging together text markup. – James Gaunt Aug 12 '11 at 10:32
  • thanks for this - the placeholder idea looks good to me. thanks for your help :) – Oliver Woolland Aug 12 '11 at 10:45
  • 1
    No problem. FYI your next step should probably be to wrap this code up into a custom server side control (i.e. create a new class which inherits from CompositeControl and instantiate the controls in the events of this class). See http://msdn.microsoft.com/en-us/library/aa479016.aspx. This way your control generation code is isolated and reusable. – James Gaunt Aug 12 '11 at 10:48
0

You're rendering server-side code - which the browser doesn't understand.

You have to add the CheckBoxList and its ListItems to the form before the page renders.

The server-side control renders the html for the browser - it is created, normally, by Asp.Net parsing the server-side markup.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
0

You are directly writing html content to browser, so you should use only html tags.

Satish G
  • 31
  • 1