Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i click a image then add one new text box dynamically?
please answer me
bye
Posted
Comments
JF2015 29-Oct-10 7:40am    
What have you tried? Have you already created an event handler?
Sunasara Imdadhusen 29-Oct-10 7:50am    
You want to create it by Javascript or Server side code?
Sunasara Imdadhusen 29-Oct-10 7:51am    
You said good-byes?

A simple google search gives you number of results.
//Image Button onclick event
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
 TextBox TextBox1=new TextBox();
 TextBox1.ID="TextBox1";
 //..
}
 
Share this answer
 
Comments
Hiren solanki 29-Oct-10 23:01pm    
madhukk answer + then assign textbox to some parent container to have look of textbox in page. :)
I think with thease codes you must specify the parent control
//Image Button onclick event
protected void ImageButton1_Click(object sender, ImageClickEventArgs e){ 
TextBox TextBox1=new TextBox(); 
TextBox1.ID="TextBox1"; 
 // this line is 4 specifying Parent of TextBox1
TextBox1.Parent = SomeContainerControl;
}
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
how-to-create-textbox-control.html

In Technique when creating control dynamically in a Page Load event
handler these controls will retain their ViewState after the
PostBack, but what if controls created dynamically at runtime in a
Button Click event handler instead of Page Load event then on
PostBack these controls ViewState are lost.To persists the
ViewState of these child controls we will recreate these controls
again on a PostBack in overrideable CreateChildControls() Method
which is called whenever
ASP.NET needs to create these WebControl in a Control Tree.

// Add TextBoxes Control to Placeholder


private void CreateTextBoxes()
{

for (int counter = 0; counter <= NumberOfControls; counter++)
{
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 18;
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
tb.Text = "Enter Title " + counter;
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br/>"));

}

}
In CreateTextBoxes method I loop through ‘n’ numbers of controls that we wants to create dynamically in phTextBoxes placeholder.
// Create TextBoxes on PostBack.
protected override void CreateChildControls()
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
NumberOfControls += 1;
CreateTextBoxes();
}
else
{
CreateTextBoxes();
// Increase the control value to 1
NumberOfControls = 0;
}

}
CreateChildControls method, here we are recreating control on every
PostBack. If the page is created the first time we just create these
controls and save 1 in the ViewState so we know that we have created
these controls and assigned the controls id to 1.
// Increase the counter when button is clicked and add to view
state
protected void btnAddTitle_Click(object sender, EventArgs e)
{


NumberOfControls += 1;


}
In button event handler we just increase the counter by 1, and save its value to ViewState
for later retrieval.Once we have created these controls on ASP.NET page, retrieving
data from these dynamically created controls is easy by using FindControl method.

// Read TextBoxes Data
public int NumberOfControls
{
get
{
if (ViewState["Count"] == null)
{
return 0;
}
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value++;
}
}

private void ReadTextBoxes()
{
strValue = string.Empty;
int n = NumberOfControls;

for (int i = 0; i <= NumberOfControls; i++)
{

string boxName = "TextBoxID" + (i + 1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
strValue += tb.Text + "\n";




}
Response.Write(strValue);


}

link below:
http://aspdotnetcodebook.blogspot.com/2008/03/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900