Albert
Albert

Reputation: 141

Get string from textbox in C# asp.net

I have some problems getting the string from some textbox that I added using a linkbutton. This code can add textboxes by clicking on linkbutton and these textboxes are added on a panel, but I don't know how to get the string that the user writes in these textboxes that were added, and also I can't keep the string that were written in the textbox when I click on the linkbutton to add one more textbox. Can you help me please? Here is my code:

public partial class _Default : Page
{
    Label myLabel1;
    Label myLabel2;
    protected void Page_Load(object sender, EventArgs e)
    {
        myLabel1 = new Label();
        myLabel2 = new Label();
        Panel1.Controls.Add(myLabel1);
        Panel2.Controls.Add(myLabel2);
        if (!Page.IsPostBack)
        {
            //Remove the session when first time page loads.
            Session.Remove("clicks");
            Session.Remove("clicks2");
        }

    }
    private void BuildTextBoxes(int rowCount1, int rowCount2)
    {
        for (int i = 0; i < rowCount1; i++)
        {
            TextBox TxtBoxU = new TextBox();
            TxtBoxU.ID = "TextBoxU" + i.ToString();
            //Add the labels and textboxes to the Panel.
            Panel1.Controls.Add(TxtBoxU);
        }

        myLabel1.Text = rowCount1 + "";

        for (int i = 0; i < rowCount2; i++)
        {
            TextBox TxtBoxU = new TextBox();
            TxtBoxU.ID = "TextBoxU" + i.ToString();
            //Add the labels and textboxes to the Panel.
            Panel2.Controls.Add(TxtBoxU);
        }

        myLabel2.Text = rowCount2 + "";
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int rowCount1 = 0;
        //initialize a session.
        rowCount1 = Convert.ToInt32(Session["clicks"]);
        rowCount1++;
        //In each button clic save the numbers into the session.
        Session["clicks"] = rowCount1;
        BuildTextBoxes(rowCount1, Convert.ToInt32(Session["clicks2"]));

    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        int rowCount2 = 0;
        //initialize a session.
        rowCount2 = Convert.ToInt32(Session["clicks2"]);
        rowCount2++;
        //In each button clic save the numbers into the session.
        Session["clicks2"] = rowCount2;
        BuildTextBoxes(Convert.ToInt32(Session["clicks"]), rowCount2);
    }

}

Thank you so much.

Upvotes: 0

Views: 2268

Answers (3)

SBurris
SBurris

Reputation: 7448

There are several things that you need to do. Since you are dynamically generating these textboxes, you need to generate them every time the page is being processed (even the postbacks). They also need to be generated before the viewstate is loaded.

On each time the page is loaded you need to recreate the textboxes in the panel, this should happen in the page_init function (needs to happen before the viewstate is loaded).

For more information on the ASP.Net Page Life Cycle: ASP.NET Page Life Cycle Overview

The code below is what I think you are looking for:

Label myLabel1;
Label myLabel2;

/// <summary>
/// Add the dynamic controls to the page before the viewstate is 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void page_init(object sender, EventArgs e)
{      
  myLabel1 = new Label();
  myLabel2 = new Label();
  Panel1.Controls.Add(myLabel1);
  Panel2.Controls.Add(myLabel2);

  var box1Count = 0;
  box1Count = Convert.ToInt32(Session["clicks"]);

  var box2Count = 0;
  box2Count = Convert.ToInt32(Session["clicks2"]);

  BuildTextBoxes(box1Count, box2Count);
}

/// <summary>
/// Ensure first time loads properly setup the page.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{ 
  if (!Page.IsPostBack)
  { 
    //Remove the session when first time page loads.
    Session.Remove("clicks");
    Session.Remove("clicks2");

    //Set the Text Boxes and lables to zero.
    BuildTextBoxes(0, 0);
  }
}

/// <summary>
/// Add any new text boxes to the screen.
/// </summary>
/// <param name="rowCount1">The total number of text boxes in the first group.</param>
/// <param name="rowCount2">The total number of text boxes in the second group.</param>
private void BuildTextBoxes(int rowCount1, int rowCount2)
{

  var panel1Count = Panel1.Controls.Count;    //Current number of text boxes
  panel1Count--;                              //Remove the Label control from the count
  for (int i = panel1Count; i < rowCount1; i++)
  {
    TextBox TxtBoxU = new TextBox();
    TxtBoxU.ID = "TextBox1U" + i.ToString();  //Ensure a globally unique name.
    Panel1.Controls.Add(TxtBoxU);             //Add the labels and textboxes to the Panel.
  }
  myLabel1.Text = rowCount1.ToString();

  var panel2Count = Panel2.Controls.Count;    //Current number of text boxes
  panel2Count--;                              //Remove the Label control from the count
  for (int i = panel2Count; i < rowCount2; i++)
  {
    TextBox TxtBoxU = new TextBox();          
    TxtBoxU.ID = "TextBox2U" + i.ToString();  //Ensure a globally unique name;
    Panel2.Controls.Add(TxtBoxU);             //Add the labels and textboxes to the Panel.
  }
  myLabel2.Text = rowCount2 + "";
}

/// <summary>
/// Add another textbox to the first group.
/// </summary>
protected void LinkButton1_Click(object sender, EventArgs e)
{
  int rowCount1 = 0;
  //initialize a session.
  rowCount1 = Convert.ToInt32(Session["clicks"]);
  rowCount1++;
  //In each button click save the numbers into the session.
  Session["clicks"] = rowCount1;
  BuildTextBoxes(rowCount1, Convert.ToInt32(Session["clicks2"]));
}

/// <summary>
/// Add another textbox to the second group.
/// </summary>
protected void LinkButton2_Click(object sender, EventArgs e)
{
  int rowCount2 = 0;
  //initialize a session.
  rowCount2 = Convert.ToInt32(Session["clicks2"]);
  rowCount2++;
  //In each button clic save the numbers into the session.
  Session["clicks2"] = rowCount2;
  BuildTextBoxes(Convert.ToInt32(Session["clicks"]), rowCount2);
}

Upvotes: 3

sh1rts
sh1rts

Reputation: 1874

+1 for explaining that all this needs to happen before viewstate is loaded. If anyone hasn't already read the Dynamic Controls and Viewstate articles on Infinities Loop, I strongly suggest they do: -

Dynamic Controls

Viewstate

Both excellent and explain things down to the last detail.

Upvotes: 0

dav_i
dav_i

Reputation: 28097

To read your text boxes you need to do the following on postback:

var result = Request.Form["textboxId"]

Upvotes: 3

Related Questions