We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

There are different ways in which you can create dynamic control in ASP.NET.
Here i have given 5 different ways for creating Dynamic controls.
 
1 : [By using JavaScript]
------------------------------
 
By using javascript you can create Dynamic controls.
Code :
        window.load = createDynamicControl;
        function createDynamicControl()
        {
 
            document.write("<input type='text' name='txtJS' value='JavaScript Textbox' />");
        }
2 : [By using ASP.NET Literal Control]
--------------------------------------
 
By using Literal control in ASP.NET you can create Dynamic Control.
 
 
<asp:Literal ID="ltrDynamicControl" runat="server" Text="I am Literal control" />
 
 
The output of the above code at the client end will be - I am Literal Control,
 
without having any additional tag.
 
So you can create dynamic controls using this control.
 
Code :
 
        protected void Page_Load(object sender, EventArgs e)
 
        {
 
            if (!IsPostBack)
 
            {
 
                ltrDynamicControl.Text = "<input type='text' id='txtDynamicControl' runat='server' value='Literal Textbox' />";
 
            }
 
        }
3 : [By using ASP.NET Label Control]
------------------------------------
 
By using Label control in ASP.NET you can create Dynamic Control.
 
 
<asp:Label ID="lblDynamicControl" runat="server" Text="I am Label control" />
 
 
The output of the above code at the client end will be - <span>I am Label Control</span>, 
means text will be enclosed with in <span></span> tag.
 
So you can create dynamic controls using this control.
 
Code :
 
        protected void Page_Load(object sender, EventArgs e)
 
        {
 
            if (!IsPostBack)
 
            {
 
                lblDynamicControl.Text = "<input type='text' id='txtDynamicControl' runat='server' value='Label Textbox' />";
 
            }
 
        }
 
 
4 : [By using Response object in ASP.NET]
 
-----------------------------------------
 
 
 
By using Response object in ASP.NET you can create dynamic Control.
 
Code :
 
        Response.Write("<input type='text' id='txtDynamicControl' runat='server' value='Response Textbox' />");
 
 
 
5 : [By using Control classes in ASP.NET]
 
-----------------------------------------
 
By using Control classes you can create dynamic controls.
 
Like for textbox you can use TextBox class to create dynamic control for textbox.
 
Code :
 
        TextBox txtDynamicControl = new TextBox();
 
        txtDynamicControl.Text = "Textbox Class Textbox";
 
        this.Form.Controls.Add(txtDynamicControl);


Related Tags:

ASP.NET, Javascript

Author: Devi Das

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login