C# 生成的动态控件不显示

发布于 2024-11-29 00:45:09 字数 1344 浏览 2 评论 0原文

我的一个页面上有以下 C# 代码:

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>");
    }

目的是创建一个动态设置属性的复选框列表。

运行时不会引发任何错误,但页面上不会出现任何控件。

当我查看页面的源代码时,我得到以下 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>

出于兴趣,我将其发布到另一个应用程序中,并且它运行良好,显示了所有控件。

这是事件调用顺序的问题吗?我对下一步要尝试什么感到有点茫然,所以任何建议都会很好。

谢谢,

奥利弗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-12-06 00:45:09

基本上你不能这样做。

渲染事件在页面生命周期中发生得很晚。您不能只输出 ASPX 标记,因为解析标记、实例化控件等的事件已经运行。

您应该做的是将 PlaceHolder 控件添加到标记中的页面,然后在较早的事件(例如 Init 或 Load)中将所需的控件添加到该占位符。同样,您不能只写出 ASPX 标记,您需要实例化控件,大致如下:

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

实现您想要的效果的一种方法是使用 VirtualPathProvider 为 .aspx 请求生成标记,因为它们是框架要求。或者您可以查看您实际想要生成的 HTML 输出(即带有一些关联 JavaScript 的输入元素列表)并直接呈现它们。然而,这两种行为可能都应该被归类为令人讨厌的黑客行为。

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.

猛虎独行 2024-12-06 00:45:09

您正在渲染服务器端代码 - 浏览器无法理解。

您必须在页面呈现之前将 CheckBoxList 及其 ListItem 添加到表单中。

服务器端控件为浏览器呈现 html - 通常是通过 Asp.Net 解析服务器端标记来创建的。

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.

败给现实 2024-12-06 00:45:09

您直接将 html 内容写入浏览器,因此您应该仅使用 html 标签。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文