Powered By Blogger

Sunday, December 20, 2009

Dynamically create control and event handler in ASP.net

Hey Friends, It is my First post. In this Post i solve some problem about DYNAMIC creation of control and event handling in ASP.net with C#.net. Two days i search lot of website. They have only told how to create Dynamic control and Creating Event Handling. and also they are specified what are problems? first one is Page_load(that is autopostback). I solved it.

EXAMPLE:

Problem definition:

                              when enter particular value in textbox control.Dynamically create given number of textbox within panel.Enter some data in dynamically created textbox. Then select different value in dropdownlist(that is select index changed or item changed). Display text of Dynamically created TextBoxes.
Lostly clear all dynamic controls in panel.

Solution:

Static control:


                    One TextBox control.
                    One DropDownList.
                    One Panel Control.
                    One Label Control(visible=false).

Code:

Default.aspx

using System;

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 _Default : System.Web.UI.Page
{
      TextBox[] t = new TextBox[40];
      protected void Page_Load(object sender, EventArgs e)
     {
            if (Label1.Text=="1")
           {
               create_text();
           }
      }
      private void create_text()
     {
             Label1.Text = "1";
             for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)
             {
                  t[i] = new TextBox();
                  t[i].ID = "text" + i;
                  Panel1.Controls.Add(t[i]);
              }
      }
      protected void TextBox1_TextChanged(object sender, EventArgs e)
     {
                    create_text();
      }
      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
             for(int i=0;i
                              Response.Write(t[i].Text );
             Panel1.Controls.Clear();
             Label1.Text = "0";
        }
}

********************* ENJOY FRIEND. EVERY TIME I AM FOR YOU********************









     

No comments:

Post a Comment