Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top