Adsence750x90

Thursday, July 1, 2010

Creating Dynamic Controls in ASP.Net

Server Side Code
Code snippet for creating Dynamic Controls in ASP.Net. adding dynamic contents and reading the values of dynamically generated controls in ASP.Net have little issue. because after PostBack dynamic controls are disappear from the form. to avoid this we can regenerate controls in page.

This is a simple sample to adding and reading Dynamic controls in ASP.Net.
in !IsPostBack i am create a new instance of ASP TextBox Control.then Adding it to the PlaceHolder Control

if (!IsPostBack)
        {
            //bind textBox in placeholder
            TextBox textBox = new TextBox();
            textBox.ID = "TextBox1";
            PlaceHolder1.Controls.Add(textBox);
        }


in IsPostBack Section

Issue come in this part. after PostBack controls are hidden or disappear from the Form. To avoid this am checking the controls are available in PlaceHolder. if it returns null am binding the controls in PlaceHolder.


//if postback dynamic generated controls are invisible
            //so we can bind in each post back
            TextBox textBox = new TextBox();
            if (PlaceHolder1.FindControl("TextBox1") == null)
            {
                textBox.ID = "TextBox1";
                PlaceHolder1.Controls.Add(textBox);
            }



Reading Values From Generated Control

In this section am explaining how to Read Contents from Dynamically generated TextBox.

i think code is more than effective to explain this, Check the code
I am reading TextBox Value in a Button click.
first check the controls is available in PlaceHolder. if available, display the value using Responce.Write.

TextBox text = PlaceHolder1.FindControl("TextBox1") as TextBox;
        if (text != null)
        {
            Response.Write(text.Text);
        }


Full Source Code


HTML Side

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamiccontrols.aspx.cs" Inherits="dynamiccontrols" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>




Server Side Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class dynamiccontrols : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bind textBox in placeholder
            TextBox textBox = new TextBox();
            textBox.ID = "TextBox1";
            PlaceHolder1.Controls.Add(textBox);
        }
        else
        { 
            //if postback dynamic generated controls are invisible
            //so we can bind in each post back
            TextBox textBox = new TextBox();
            if (PlaceHolder1.FindControl("TextBox1") == null)
            {
                textBox.ID = "TextBox1";
                PlaceHolder1.Controls.Add(textBox);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox text = PlaceHolder1.FindControl("TextBox1") as TextBox;
        if (text != null)
        {
            Response.Write(text.Text);
        }
    }
}

4 comments:

Atheist said...

Dude i am getting an expection saying place holder doesnot exist in the current context.what do i do?

saravanan said...

Error:The name 'PlaceHolder1' does not exist in the current context

Unknown said...

you hav to add a placeholder control in the source page

Raju.M said...

Thanks priya acharya , saravanan and Atheist please debug your code then you understand where you getting the error.

Thanks