1

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.

5
  • Add Panel then use literal control Commented Nov 15, 2014 at 9:57
  • I tried but the controls still working as HTML. I guess the problem because the control is not registered for this page. Commented Nov 15, 2014 at 10:02
  • Exactly what do you mean "at runtime"? You can look at the answers below if you meant "dynamically creating controls" instead of in markup. They are all "HTML" once rendered (to client). Commented Nov 15, 2014 at 18:55
  • I mean the user will insert the html which contains an asp controls this html need to be add to the current page, so i can't know what controls are inside the user input, in this situation the answer below is not suitable. Commented Nov 17, 2014 at 1:19
  • If you want the controls to behave like ASP.NET controls (running through the ASP.NET page life cycle), you will need to add them to the page using ASP.NET. ASP.NET creates a lot of JavaScript for your controls, not only HTML. Commented Nov 17, 2014 at 9:34

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0
<%@ 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

1 Comment

the asp Html is entered by the user so i can't excpect what controls he is putting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.