.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

create and retrieve value from dynamic controls

Posted By:Pankaj Mishra       Posted Date: January 08, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

create and retrieve value from dynamic controls

In this article we are going to see how to create dynamic control in asp.net web application or asp.net website. We are also going see how to retrieve the value from newly create TextBox control and fire button click event of dynamically create Button control.

Lets first take an example of TextBox Control

Create TextBox Control Programmatically

To create TextBox using code we are going to use Page_PreInit Event Lets see the code

Using C# Code

/// 
/// Pre Init event to create dynamic controls
/// 
/// 
/// 
protected void Page_PreInit(object sender, EventArgs e)
{
    TextBox txtBox = null;

    for (int i = 0; i < 5; i++)
    {
        txtBox = new TextBox();
        txtBox.ID = "myTextBox" + i; //Assign Id
       
        //Add Newly created textbox to Panel
        pnlTextBox.Controls.Add(txtBox);


        //Add berek so the next textbox will create in next new line
        Literal lit = new Literal();
        lit.Text = "

"; pnlTextBox.Controls.Add(lit); } }

Using VB.Net Code

''' 
''' Pre Init event to create dynamic controls
''' 
''' 
''' 
Protected Sub Page_PreInit(sender As Object, e As EventArgs)
	Dim txtBox As TextBox = Nothing

	Dim i As Integer = 0
	While i < 5
		txtBox = New TextBox()
		txtBox.ID = "myTextBox" + i
		'Assign Id
		'Add Newly created textbox to Panel
		pnlTextBox.Controls.Add(txtBox)


		'Add berek so the next textbox will create in next new line
		Dim lit As New Literal()
		lit.Text = "

" pnlTextBox.Controls.Add(lit) System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1) End While End Sub

Now we have created the 5 Textbox control and added in the web page using above code. Now using below code example we are going to retrive the value from the TextBox

In C# Example

protected void btnGetValText_Click(object sender, EventArgs e)
{
    string valueFromTextBox = string.Empty;
    //Loop thru all the control available in Panel
    foreach (Control ctrl in pnlTextBox.Controls)
    {
        TextBox txtBox = ctrl as TextBox;
        if (txtBox != null)
        {
            valueFromTextBox = txtBox.Text.Trim();
            //Your rest logic goes here
        }
    }
}

VB.Net sample method

Protected Sub btnGetValText_Click(sender As Object, e As EventArgs)
	Dim valueFromTextBox As String = String.Empty
	'Loop thru all the control available in Panel
	For Each ctrl As Control In pnlTextBox.Controls
		Dim txtBox As TextBox = TryCast(ctrl, TextBox)
		If txtBox <> Nothing Then
				'Your rest logic goes here
			valueFromTextBox = txtBox.Text.Trim()
		End If
	Next
End Sub

I have written the above code in a Button click event

Create and attach button click event in web page

I have seen in so many forums asking question that dynamically created button is not firing click event. Most of the time beginner forget to attach the event of programmatically created Button control.

As above example Here also we are going to use Page_PreInit Event to create Asp.Net button class to create Button control dynamically.

Lets see the code in C#

protected void Page_PreInit(object sender, EventArgs e)
{
    Button myButton = null;

    //Create 5 button dynamically
    for (int i = 0; i < 5; i++)
    {
        //Create new instance of Button class and assign it necessary property
        myButton = new Button();
        myButton.ID = "myButton" + i; //Assign Id
        myButton.Text = "myButton" + i;

        //Attach the button click event handler 
        myButton.Click += new EventHandler(myButton_Click);

        //Add Newly created Button to Panel
        pnlButton.Controls.Add(myButton);

        //Add berek so the next Button will create in next new line
        Literal lit = new Literal();
        lit.Text = "

"; // add the Button to panel in a web page pnlButton.Controls.Add(lit); } }

and Event handler method

void myButton_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    if(btn !=null)
    {
        Response.Write("Button id " + btn.ID + "  clicked");
    }
}

You can alos donwload the sample project by clicking on below download link.

Hope this will help beginner

Cheers!!!
Pankaj

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend