genrate textboxes wid ID on every click of button [Resolved]

Posted by Rickeybglr under ASP.NET on 11/28/2012 | Points: 10 | Views : 1867 | Status : [Member] | Replies : 14
i need to generate three text boxes on single click of button. every time i click on button 3 boxes shoud get added on aspx page. plz suggest as of now i am able to generate 1 textbox on every click




Responses

Posted by: Sandhyab on: 11/28/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Once try like this
public static int count = 3;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void AddTextboxes(object sender, EventArgs e)
{
panelId.Controls.Clear();
for (int i = 0; i < count; i++)

{
Label l1 = new Label();
l1.Text = "Label" + i;
l1.ID = "lbl" +i;
TextBox t1 = new TextBox();
t1.ID = "txt" + i;
Literal lt = new Literal();
lt.Text = "<br />";
panelId.Controls.Add(lt);
panelId.Controls.Add(l1);
panelId.Controls.Add(t1);

}

count += 3;

}


Thanks & Regards

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Funda on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
You can make a for loop and reuse the code with different ID....If you send the code of creating one textbox....i will check that...

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
here is my code ..the othproblm is tht..ths code works for single click only ..wht i want on every click 3 pair of txtbox shud get generate
TextBox textbox_foradd1 = new TextBox();
textbox_foradd1.ID = "textadd1" + count.ToString();

TextBox textbox_foradd2 = new TextBox();
textbox_foradd2.ID = "textadd2" + count.ToString();

TextBox textbox_foradd3 = new TextBox();
textbox_foradd3.ID = "textadd3" + count.ToString();

PlaceHolder1.Controls.Add(textbox_foradd1);
Response.Write("<br />");
PlaceHolder1.Controls.Add(textbox_foradd2);
Response.Write("<br />");
PlaceHolder1.Controls.Add(textbox_foradd3);
Response.Write("<br />");


Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Somu475 on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Rickeybglr,

Whatever the code u have written in button click event for generating Textbox, use that same code 3 times to generate 3 Textboxes..
For that u can write a separate method inside which u can write the code for generating Textbox.
And then call that method in button click event for 3 times with different ID's.

-Somu

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pratyupalati on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi........

I hope the below code will help you........

In aspx page:

<body>

<form id="form1" runat="server">
<div>
<asp:Panel ID="panelId" runat="server"></asp:Panel>
<asp:Button ID="btnClick" runat="server" Text="ClickMe" OnClick="AddTextboxes" />
</div>
</form>
</body>


In aspx.cs page:


public static int count = 3;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void AddTextboxes(object sender, EventArgs e)
{
for (int i = 0; i < count; i++)
{
TextBox t1 = new TextBox();
t1.ID = "txt" + i;
panelId.Controls.Add(t1);
}
count += 3;
}



Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Funda on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
@Rickeybglr is right....
You can also add the Id and All HTML Attributes uisng

 t1.Attributes.Add("key", "vlaue");


Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Thnks every1 specialy pratya ... atleast i got idea.. now i can modify ths acc to my need

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ..
i have modified ths code acc. to my need but still facing 1 problm, on every click 3 pair of txtboxes r gettn generated .but labels once.. why plz luk into ths.

protected void AddTextboxes(object sender, EventArgs e)
{

panelId.Controls.Clear();
for (int i = 0; i < count; i++)
{
Label l1= new Label();

if (i == 0)
{
l1.Text = "first";

}
else if (i == 1)
{
l1.Text = "second";
}
else if (i == 2)
{
l1.Text = "third";
}

TextBox t1 = new TextBox();
t1.ID = "txtbox" + i.ToString();

Literal lt = new Literal();
lt.Text = "<br />";
panelId.Controls.Add(lt);
panelId.Controls.Add(l1);
panelId.Controls.Add(t1);

}
count += 3;
}

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Funda on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
give id to label and try....

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
no still not working.....

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
superb thnks ..

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
You are welcome..

Thanks & Regards

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
i need some more help on ths. with runtime addition of textbox i need to add delete button, on click of tht button textbox with label shud get delete.
i have added a button on runtime ..but how can i delete the runtime textbozx?? plz luk into ths
here is my code:

public static int count = 3;
public int i = 0;
protected void btnDelete_click(object sender, EventArgs e)
{
panelId.Controls.Clear();

for (i = 0; i < count; i++)
{
Label l1 = new Label();

if (i % 3 == 0)
{
l1.Text = "first";
Literal lt1 = new Literal();
lt1.Text = "<br />";
}

if (i % 3 == 1)
{
l1.Text = "second";
}

if (i % 3 == 2)
{
l1.Text = "third";
}

l1.ID = "lbl" + i;
TextBox t1 = new TextBox();
t1.ID = "txt" + i;

Literal lt = new Literal();
lt.Text = "<br />";

panelId.Controls.Add(lt);
panelId.Controls.Add(l1);
panelId.Controls.Add(t1);

Response.Write(panelId.Controls.IndexOf(t1));
lt.Text = "<br />";
Response.Write(panelId.Controls.IndexOf(l1));
}
count += 3;

Button btn1 = new Button();
btn1.ID = "btn" + i;
btn1.Text = "Delete";

btn1.Click += new EventHandler(btnDelete_click);
panelId.Controls.Add(btn1);
}

protected void btnDelete_click(object sender, EventArgs e)
{
// code to delte the last set of 3 textbox wid thr label }

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Funda on: 11/30/2012 [Member] Starter | Points: 25

Up
0
Down
Button btn1 = new Button();
btn1.ID = "b
tn" + i;
btn1.Text = "Delete";

y u r giving id with auto increment [ i ] to the Delete Button.

U need to wirte the EventHandler(btnDelete_click) in page load after creating the Delete Button

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response