Creating dynamic textbox

Posted by Vijayar under C# on 6/21/2011 | Points: 10 | Views : 144681 | Status : [Member] | Replies : 9
Hi
i want r\to create a dynamic textbox on clicking on button
.please help me to do this with ex

vijaya


Responses

Posted by: Ndebata on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down
On click event of button create a TextBox and add to the control collection
TextBox tb=new TextBox();

Panel1.Controls.Add(tb);


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

Posted by: Naimishforu on: 6/21/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
private System.Windows.Forms.TextBox[] textboxes;


textboxes = new System.Windows.Forms.TextBox[2];
int cnt=0;
foreach (System.Windows.Forms.TextBox textbox in textboxes)
{
// the location on the form. you may need to play with this
// and it should change for each textbox
textbox.Location = new System.Drawing.Point(88, 50);
textbox.Name = "textbox"+cnt.ToString();
textbox.Size = new System.Drawing.Size(173, 20);
textbox.TabIndex = cnt;
textbox.Visible = true;
cnt++;
}


Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

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

Posted by: Vijayar on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

i am not getting textbox by using theis code.i tried already
<tr>
<td>
Program Interested
</td>
<td>
<asp:TextBox ID="txtprogram" runat="server" CssClass="txtbox"></asp:TextBox>
</td>
<asp:Panel ID="p1" runat="server">
<td> <asp:ImageButton ID="img1" runat="server" ImageUrl="~/images/btn_add.gif"
onclick="img1_Click" /></td></asp:Panel>

protected void img1_Click(object sender, ImageClickEventArgs e)
{
TextBox tb = new TextBox();

p1.Controls.Add(tb);


}

where is my mistake


vijaya

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

Posted by: Ndebata on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down
Your ASPX is not properly defined.
<table>

<tr>
<td>
Program Interested
</td>
<td>
<asp:TextBox ID="txtprogram" runat="server" CssClass="txtbox"></asp:TextBox>
</td>
<td> <asp:ImageButton ID="img1" runat="server" ImageUrl="~/images/btn_add.gif"
onclick="img1_Click" />
<asp:Panel ID="p1" runat="server"></asp:Panel></td></tr>
</table>


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

Posted by: Lakn2 on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down
first place literal control or placeholder control or panel control in asp.net page
in button1 click event write
textbox tb=new textbox();

literal1.controls.add(tb);
or
placeholder1.controls.add(tb);


Thanks&Regards
LakshmiNarayana Nalluri.

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

Posted by: Vijayar on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
After clicking on Add button for first time iam getting a textbox,but when i click again i am not getting the textbox for second time,How to get it for second,third....time


vijaya

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

Posted by: Vijayar on: 6/21/2011 [Member] Starter | Points: 25

Up
0
Down

Hi
After clicking on Add button for first time iam getting a textbox,but when i click again i am not getting the textbox for second time,How to get it for second,third....time and i should get the textbos below txtprogram only

vijaya

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

Posted by: Susanthampy on: 6/21/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
ASPX


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Textbox" />
<asp:Panel ID="Panel1" runat="server" >

</asp:Panel>
</div>
</form>
</body>
</html>


C#

using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private List textboxes;

protected void Page_Load(object sender, EventArgs e)
{

PreRender += new EventHandler(_Default_PreRender);


textboxes = new List();

if(IsPostBack)
{
//recreate Textboxes
int count = Int32.Parse(ViewState["tbCount"].ToString());

for (int i = 0; i < count; i++)
{
TextBox tb = new TextBox();
tb.ID = "tb" + i;

Panel1.Controls.Add(tb);

textboxes.Add(tb);

tb.Text = Request.Form[tb.ClientID];
}
}
}

protected void Button1_Click(object sender, EventArgs e)
{
//create new textbox
TextBox tb = new TextBox();
tb.ID = "tb" + textboxes.Count;

Panel1.Controls.Add(tb);

textboxes.Add(tb);
}

void _Default_PreRender(object sender, EventArgs e)
{
//remember how many textboxes we had
ViewState["tbCount"] = textboxes.Count;
}
}
}


Regards,
Susan

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

Posted by: Susanthampy on: 6/21/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi ,


http://www.aspsnippets.com/Articles/Creating-Dynamic-TextBox-Controls-in-ASP.Net.aspx

Regards,
Susan

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

Login to post response