how to generate dynamic controls text box , label in c# , asp.net.
in this article i have show how to generate dynamic control in asp.net and bind with existing control like Div .dynamic means
it will generate on run time .
first we will assign that attribute and then initialize new value .
To Generate Dynamic button
.cs page
Button btnaddarray = new Button();
btnaddarray.Text = "Add Array"; // Give text to respective button
btnaddarray.ID = "BtnaddArray"; // provide id
btnaddarray.Attributes.Add("Onclick", "BtnaddArray_Onclick"); // add event means when buton click then event will be raised.
Now here i have called FunArray() function to generate dynamic controls in c# .
just copy or read this code and execute it.
private void FunArray()
{
int[] myArray = new int[10];
myArray[0]=1;
myArray[2]=2;
myArray[3] = 3;
Label lblPrintarrayvalue = new Label();
lblPrintarrayvalue.Text =Convert.ToString("Array One:-" + myArray[0] +"Array Two:-"+"</Br>"+ myArray[1]+"Array
Three:-"+ myArray[2]+"<br/>");
dvlabl.Controls.Add(lblPrintarrayvalue);
lblPrintarrayvalue.ForeColor= System.Drawing.Color.Red;
Response.Output.Write(lblPrintarrayvalue.Text);
Response.Output.Write("<p>Your total is ${0:F2}</p>", lblPrintarrayvalue.Text);
Response.Write(String.Format("<p>Your total is ${0:F2}</p>", lblPrintarrayvalue.Text));
TextBox txttakeval = new TextBox();
txttakeval.ID = "txtval";
Label lblenterarray = new Label();
lblenterarray.Text = "Enter Array List";
txttakeval.Attributes.Add("Runat", "Server");
Button btnaddarray = new Button();
btnaddarray.Text = "Add Array";
btnaddarray.ID = "BtnaddArray";
btnaddarray.Attributes.Add("Onclick", "BtnaddArray_Onclick");
btnaddarray.Click += new EventHandler(this.BtnaddArray_Onclick);
dvlabl.Controls.Add(lblenterarray);
dvlabl.Controls.Add(txttakeval);
dvlabl.Controls.Add(btnaddarray);
}
private void BtnaddArray_Onclick(object sender, EventArgs e)
{
// your code
}
.aspx page
<div runat="server" id="dvlabl">
<asp:Button runat="server" ID="btnsubmit" Text="Submit" OnClick="btnsubmit_onclick" ValidationGroup="nm" />
<asp:TextBox runat=server ID=txt2ley></asp:TextBox>
</div>
in this article i have show how to generate dynamic control in asp.net and bind with existing control like Div .dynamic means
it will generate on run time .
first we will assign that attribute and then initialize new value .
To Generate Dynamic button
.cs page
Button btnaddarray = new Button();
btnaddarray.Text = "Add Array"; // Give text to respective button
btnaddarray.ID = "BtnaddArray"; // provide id
btnaddarray.Attributes.Add("Onclick", "BtnaddArray_Onclick"); // add event means when buton click then event will be raised.
Now here i have called FunArray() function to generate dynamic controls in c# .
just copy or read this code and execute it.
private void FunArray()
{
int[] myArray = new int[10];
myArray[0]=1;
myArray[2]=2;
myArray[3] = 3;
Label lblPrintarrayvalue = new Label();
lblPrintarrayvalue.Text =Convert.ToString("Array One:-" + myArray[0] +"Array Two:-"+"</Br>"+ myArray[1]+"Array
Three:-"+ myArray[2]+"<br/>");
dvlabl.Controls.Add(lblPrintarrayvalue);
lblPrintarrayvalue.ForeColor= System.Drawing.Color.Red;
Response.Output.Write(lblPrintarrayvalue.Text);
Response.Output.Write("<p>Your total is ${0:F2}</p>", lblPrintarrayvalue.Text);
Response.Write(String.Format("<p>Your total is ${0:F2}</p>", lblPrintarrayvalue.Text));
TextBox txttakeval = new TextBox();
txttakeval.ID = "txtval";
Label lblenterarray = new Label();
lblenterarray.Text = "Enter Array List";
txttakeval.Attributes.Add("Runat", "Server");
Button btnaddarray = new Button();
btnaddarray.Text = "Add Array";
btnaddarray.ID = "BtnaddArray";
btnaddarray.Attributes.Add("Onclick", "BtnaddArray_Onclick");
btnaddarray.Click += new EventHandler(this.BtnaddArray_Onclick);
dvlabl.Controls.Add(lblenterarray);
dvlabl.Controls.Add(txttakeval);
dvlabl.Controls.Add(btnaddarray);
}
private void BtnaddArray_Onclick(object sender, EventArgs e)
{
// your code
}
.aspx page
<div runat="server" id="dvlabl">
<asp:Button runat="server" ID="btnsubmit" Text="Submit" OnClick="btnsubmit_onclick" ValidationGroup="nm" />
<asp:TextBox runat=server ID=txt2ley></asp:TextBox>
</div>
0 comments: