[英]How to add ASP.net and HTML Controls to the page at runtime?
I have an ASP.net WebForm. 我有一个ASP.net WebForm。 The markup is like:
标记类似于:
<div>
<input type="text" id="input" runat="server" value=" " />
<asp:Button Text="send" OnClick="btnsend_Click" ID="btnsend" runat="server" />
</div>
This HTML is generated at runtime . 该HTML在运行时生成。 The events are defined in the code behind file.
这些事件在文件后面的代码中定义。 I need to add these controls at runtime.
我需要在运行时添加这些控件。 I tried to use the Literal-Control but the controls are working just like HTML Controls and not like ASP.net Controls.
我尝试使用Literal-Control,但是这些控件就像HTML控件一样工作,而不像ASP.net控件那样工作。
EDIT: 编辑:
Note: The Project type should be Website, not a web Application. 注意:项目类型应该是网站,而不是Web应用程序。 Web application won't support on demand compilation where website yes, it is.
Web应用程序不支持按需编译,而网站是。
If I understood currectly, you want to take the Markup from User which contains even asp.net controls and scriplets too. 如果我的理解正确,您想从User中获取标记,该标记甚至还包含asp.net控件和Scriplets。
if this is the case, Follow below steps: 如果是这种情况,请执行以下步骤:
<%@ Register src="~/DynamicMarkup.ascx" tagname="DynamicMarkup" tagprefix="MyASP" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:PlaceHolder runat="server" ID="DynamicMarkupContainer" ></asp:PlaceHolder> </div> </form> </body> </html>
DynamicMarkup dynamicMarkup = LoadControl("~/DynamicMarkup.ascx") as DynamicMarkup;
DynamicMarkup dynamicMarkup = LoadControl(“〜/ DynamicMarkup.ascx”)作为DynamicMarkup;
DynamicMarkupContainer.Controls.Add(ucSimpleControl);
DynamicMarkupContainer.Controls.Add(ucSimpleControl);
I have not tested this approach, Just give a thought, With this you may get some session overwriting issue which you need handle. 我还没有测试这种方法,请考虑一下,这样您可能会遇到一些会话覆盖问题,需要解决。
Hope this will help!! 希望这会有所帮助!
OLD: is this the one that you are expecting? 老:这是您所期望的吗? TextBox, and Button controls are available in System.Web.UI.WebControls namespace.
System.Web.UI.WebControls命名空间中提供了TextBox和Button控件。
void Page_Load(Object sender, EventArgs e)
{
TextBox input = new TextBox();
input.Id ="input";
this.PlaceHolder.Controls.Add(input);
Button btnSend=new Button();
btnSend.Id ="btnSend";
btnSend.Text="Send";
btnSend.Click += new EventHandler(btnSend_Click);
this.PlaceHolder.Controls.Add(btnSend);
}
void btnSend_Click(object sender, EventArgs e)
{
// throw new NotImplementedException();
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="phHolder" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
code behind : 后面的代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Init()
{
GenerateContorls();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void GenerateContorls()
{
TextBox newTxt = new TextBox() { ID = "txtsend" };
Button newBtn = new Button() { Text = "Send", ID = "btnsend" };
newBtn.Click += btnsend_Click;
phHolder.Controls.Add(newTxt);
phHolder.Controls.Add(newBtn);
}
protected void btnsend_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox)this.FindControl("txtsend");
//your code
}
}
hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.