简体   繁体   中英

How to add ASP.net and HTML Controls to the page at runtime?

I have an 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 . 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.

EDIT:

Note: The Project type should be Website, not a web Application. Web application won't support on demand compilation where website yes, it is.

If I understood currectly, you want to take the Markup from User which contains even asp.net controls and scriplets too.

if this is the case, Follow below steps:

  1. Create a dummy .ascx control file, like DynamicMarkup.ascx with empty content
  2. Add this user control to the page (xxxx.aspx) where you want to show this control statically so it registered to the page
  <%@ 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> 
  1. Write user input markup (may be get from database based on your criteria) to the DynamicMarkup.ascx file in page OnInit of the page (xxxx.aspx) And the create object of this DynamicMarkup

DynamicMarkup dynamicMarkup = LoadControl("~/DynamicMarkup.ascx") as DynamicMarkup;

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.

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM