动态添加 ASP 控件 [英] Dynamically Add ASP Controls

查看:24
本文介绍了动态添加 ASP 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在后面的asp.net C# 代码中将asp 标记控件 动态添加到表单中?我正在尝试执行与以下类似的操作,但具体而言,如果需要,字符串可以从 Button 更改为 Textbox.

string control1 = "asp:button";HtmlGenericControl xControl = new HtmlGenericControl(control1);body1.Controls.Add(xControl);

有没有办法在 ASP.Net 网络表单上做到这一点?我已经尝试过 HtmlGenericControl 但这显然行不通.有什么建议吗?

解决方案

可以通过添加div runat serverc#代码

 Button btn = new Button();btn.Text = "测试";maindiv.Controls.Add(btn);

html

<form id="form1" runat="server"><div runat="server" id="maindiv">

</表单>

https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

How do you dynamically add an asp taged control to a form on asp.net C# code behind? I am trying to do something similar to the following, but specifically where the string can change from Button to Textbox if needed.

string control1 = "asp:button";
HtmlGenericControl xControl = new HtmlGenericControl(control1);
body1.Controls.Add(xControl);

Is there a way to do this on a ASP.Net webform? I have tried the HtmlGenericControl but this is obviously not going to work. Any suggestions?

解决方案

You can by adding div runat server c# code

 Button btn = new Button();
 btn.Text = "test";
 maindiv.Controls.Add(btn);

html

<body>
    <form id="form1" runat="server">
    <div runat="server" id="maindiv">

    </div>
    </form>
</body>

https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

这篇关于动态添加 ASP 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆