0

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.

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.

4 Answers 4

1

I have already answered it here.

Lost dynamically created text box values

You can try this.

private string GetValue(string ControlID)
{
   string[] keys = Request.Form.AllKeys;
   string value = string.Empty;
   foreach (string key in keys)
   {
     if (key.IndexOf(ControlID) >= 0)
     {
         value = Request.Form[key].ToString();
         break;
     }
   }

   return value;
}

Then to get the value

string txtChoice1value = GetValue("txtChoice1");
Sign up to request clarification or add additional context in comments.

Comments

0

First of all when you dynamically create a control it doesn't need to be set "runat = sever". Problem is in this line `ContentPlaceHolder content_new = new ContentPlaceHolder();` you make a new ContentPlaceHolder, this mean it doesn't have any control to be found.

Check this page. How To Create TextBox Control Dynamically at Runtime

Comments

0

You need to find the reference of your already created ContentPlaceHolder like-

ContentPlaceHolder cnt =(ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");

and then add the dynamically created Control in that ContentPlaceHolder as-

cnt.Controls.Add(Session["Panel"] as Panel);

Why you creating a new ContentPlaceHolder each time even when you have mentioned that you are using masterPage, so there must exists a ContentPlaceHolder..

Comments

0

Controls wont persist on postback have a look at http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.