渲染.NET控件串并获得事件触发控件、事件、NET

2023-09-06 05:53:26 作者:我愿为你选择回到、孤单

我有这样的问题,即我试图生成HTML来显示从数据库中有不同的花括号是被替换为在运行时动态内容模板的表单来在页面上。例如花括号{}的内容被替换几个checkboxlists,radiobuttonlists,文本框和一个提交按钮。我这样做的方式是通过动态创建控件和渲染它们作为字符串,然后输出该字符串到浏览器。但是这样做是,它不能识别分配给该控件时生成他们的事件。我可能会采取错误的做法对这个问题,但我实在不明白怎么回事,这是可以实现的。我都忍了下面的一些示例code,以显示我想要实现。请告知如何解决这个问题。

I have this issue where I'm trying to generate HTML to display on the page that is coming from the database in form of a template that has different curly brackets that get replaced with dynamic content at run time. For example curly bracket {Content} gets replaced with a few checkboxlists, radiobuttonlists, textboxes and a submit button. The way I'm doing this is by creating controls dynamically and rendering them as string and then outputting that string to the browser. But what this does is, it doesn't recognise the events assigned to the controls when they are generated. I may be taking a wrong approach to the issue, but I don't really see how else this can be achieved. I have put up some sample code below to show what I'm trying to achieve. Please advise how to resolve this.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        Dim PageContent As StringBuilder = New StringBuilder()
        PageContent.Append("A lot of content from database laid out using HTML templates stored in database including the below mentioned button code.")

        '# To illustrate how pagecontent is built using lots of such controls rendered to string
        Dim SubmitButton As Button = New Button()
        SubmitButton.ID = "MySubmitButton"
        SubmitButton.Text = "Submit"
        SubmitButton.CssClass = "SubmitButton"
        'Page.Controls.Add(SubmitButton)        '# This line I thought would resolve the issue, but it doesn't as the .net form is not built at the time of page load
        AddHandler SubmitButton.Click, AddressOf ClickHandler

        PageContent.Append(RenderControlToString(SubmitButton))

        Dim Output As Literal = New Literal()
        Output.Text = PageContent.ToString()
        Page.Controls.Add(Output)

    End If

End Sub

''' <summary>
''' Button Click Hander
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)

    '# Control never gets here!!!!!
    Response.Write("<br/>Button Clicked!")
    Response.End()

End Sub

''' <summary>
''' Render control to string
''' </summary>
''' <param name="control"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function RenderControlToString(control As Control) As String

    Dim sb = New StringBuilder()
    Using sw = New StringWriter(sb)
        Using textWriter = New HtmlTextWriter(sw)
            control.RenderControl(textWriter)
        End Using
    End Using

    Return sb.ToString()

End Function

感谢您的建议!

Thanks for advising!

推荐答案

动态加载的.NET控件是有点混乱。我看到四个问题,你在做什么。

Dynamically loading controls in .Net is a little confusing. I see four problems with what you're doing.

您正试图动态地仅在初始HTTP GET(而不是回发)添加控件。您需要每次添加控件。你不具约束力的数据在这里,你将控件添加到页面控件树。这些控件需要每次页面运行时无以复加。

You're trying to dynamically add the controls only on the initial HTTP GET (not on postbacks). You need to add the controls every time. You're not binding data here, you're adding controls to the Page control tree. Those controls need to be added every time the page runs.

您正在绘制控件的字符串。不这样做,只是把它们添加到页面控件树和页面渲染期间,他们将呈现自己。

You're rendering controls to string. Don't do that, just add them to the page control tree and they'll render on their own during the page Render.

使用Page.LoadControl()创建控件时。这将创建的控制,迫使它的事件(页面事件生命周期),以赶上页面的其余部分。

Use Page.LoadControl() when creating controls. This will create the control and force it's events (page event lifecycle) to catch up with the rest of the page.

您要添加控件晚了一点,在页面事件生命周期。尝试在Page_Init加入你的控制。

You're adding controls a little late in the page event lifecycle. Try adding your controls in Page_Init.

下面是拉它一起的样本。它动态地创建一个按钮,然后将其添加到一个占位符控件。

Here's a sample that pulls it all together. It dynamically creates a button and then adds it to a placeholder control.

protected void Page_Init(object sender, System.EventArgs e)
{
  var button = LoadControl(typeof(Button), null) as Button;
  button.Click += clickHandler;
  myPlaceholder.Controls.Add(button);
}
 
精彩推荐