today we will learn here how to create the dynamic Button as well as Textbox ,how to handle the event of dynamic Button and how to retrieve the values of Textboxs
//Put a Place Holder or panel control on your aspx page like this
//2) go to the code behind and done the following operaions
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Administrator.DataAccess;
using Administrator.BussinessObject;
public partial class UserControl_Navigration : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateButton();//Function to create the dynamic button
CreatedTextBox(); //Function to Create the Dyanmic Textbox
}
else
{
CreateButton();
CreatedTextBox();
}
}
//function to Create the dynamic button
public void CreateButton()
{
for (int i = 0; i <= 10; i++)
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "Button"+i.ToString();
btn.CssClass = "current";
btn.ForeColor = System.Drawing.Color.White;
btn.BackColor = System.Drawing.Color.Gray;
btn.Attributes.Add("style", "cursor:pointer;");
btn.Command +=new CommandEventHandler(Redirect);//Handling the Event of button
place.Controls.Add(btn);//Adding the button to Place holder control here
}
}
//this function Redirect will be fire when any user will click on button
protected void Redirect(object sender, CommandEventArgs e)
{
Button btn=(Button)sender;
Response.Write("My Name is:" + btn.ID.ToString());
btn.Text = "My name is praveen";
//using for loop we are geting the text of dynamic text box
for (int i = 0; i <= 5; i++)
{
TextBox txt =(TextBox) this.form1.FindControl("txt" + i);
Response.Write(txt.Text);
txt.Text = "Jai Shree Ram";
}
}
//Here We are creating the dynamic textboxs
public void CreatedTextBox()
{
for(int i=0; i<=5; i++)
{
TextBox txt = new TextBox();
txt.ID = "txt" + i;
this.form1.Controls.Add(txt);
}
}
}
}
}
No comments:
Post a Comment