Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to create dynamic text box when user click on Add more link button.
For this I am using this code. And I have to mention that I am using master page.


protected void lnkAddMore_Click(object sender, EventArgs e)
{
if (Request.Cookies["value"] != null)
{
i = Convert.ToInt32(Request.Cookies["value"].Value) + 1 ;
}
for (int k = 1; k <= i; k++)
{
LiteralControl literal = new LiteralControl();
literal.Text = "<br /><br />";
Label newLabel = new Label();
newLabel.Text = "Choice" + " " + k.ToString();
newLabel.ID = "lblChoice_" + k.ToString();
newLabel.Attributes.Add("runat", "Server");
this.panelLabel.Controls.Add(newLabel);
this.panelLabel.Controls.Add(literal);

LiteralControl literal1 = new LiteralControl();
literal1.Text = "<br /><br />";
TextBox nexText = new TextBox();
nexText.ID = "txtChoice_" + k.ToString();
nexText.Attributes.Add("TextMode", "MultiLine");
nexText.Attributes.Add("runat", "Server");
panelTextbox.Controls.Add(nexText);
this.panelTextbox.Controls.Add(literal1);

Response.Cookies["value"].Value = i.ToString();
Session["Panel"] = panelTextbox;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content=new ContentPlaceHolder();
content.Controls.Add(Session["Panel"] as Panel);
}
}
}

Now I am facing trouble how to retrieve the data of the these text boxes after the clicking on the submit button so that I can store the values of there text boxes to database.

What will be code written for the click event of btnSave
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content_new = new ContentPlaceHolder();
for (int i = 1; i <= count; i++)
{
strControlName = "txtChoice_" + i.ToString();

TextBox objTextBox = (TextBox)content_new.FindControl(strControlName);

strTextBoxValues[i] = objTextBox.Text;
string str3 = strTextBoxValues[2];
}
}
}


This code is showing error for objTextBox
The error is NullReferenceException

And How to write stored procedure for saving data of above code.

The main problem is handling the parameter declaration

how to declare dynamic parameter for passing values so that value is saved for dynamic textbox



Thanks in advance

Deepak Pandey
Posted
Updated 5-Oct-11 20:15pm
v9
Comments
André Kraak 5-Oct-11 2:40am    
Edited question:
Added pre tags
Spelling/Grammar

Use following:

C#
protected void btnSave_Click(object sender, EventArgs e)
           {
               foreach( Control c in panelTextbox.Controls )
               {
                    if(c is TextBox)
                    {
                       // Write code for saving data in Database.
                    }
               }
           }
 
Share this answer
 
After your postback, you need to use the Page.FindControl Method to find them.
Page.FindControl Method (String)[^]

Note: The Control.FindControl Method would narrow it down if you want to use panelTextbox as a base. Probably a little more efficient.
 
Share this answer
 
v2
Comments
aryan2010 21-Sep-11 16:19pm    
I am already tried this but it showing error
aryan2010 21-Sep-11 16:19pm    
can you provide complete code for this
When adding dynamic controls you have to add them again on the postback and then retireve the data from them. You will need to know how many text boxes or controls you have added and add them all again if you plan to get data out of any of them. Again every post back you must add the controls that you dynamically added before referencig them on your postback.

Try this link for some help
http://support.microsoft.com/kb/317515[^]
 
Share this answer
 
Comments
aryan2010 21-Sep-11 16:24pm    
now check my updated question now you can fully understand my requrement
one solution can be :
1. save the whole panel (in which dynamic controls are added) in a session variable like :
C#
session["Panel"] = "YourPanel";
2. restore it on the postback event :
C#
if(PostBack)
{
   if(session["Panel"]!=null)
      Form1.Controls.Add(Session["Panel"] as Panel);
}
 
Share this answer
 
v2

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