Aslam
Aslam

Reputation: 47

how to get multiple value from dynamic generated multiple textbox in asp.net c#

I want to get value from textbox which is dynamically generated. Given below is my code, But Every time a was get null value.

Default.aspx:

 <asp:Panel ID="Panel1" runat="server" style="width:460px">
 <asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder>
  </asp:Panel>


<asp:Button ID="Button1" runat="server" Text="Button" 
   onclick="Button1_Click1" />
<asp:Button ID="Button2" runat="server" Text="save" onclick="Button2_Click" />

Default.aspx.cs

 protected void Button1_Click1(object sender, EventArgs e)
 {
    CreateTextBox();

 }
 public void CreateTextBox()
 {

    int rowCount = 0;
    //initialize a session.
    rowCount = Convert.ToInt32(Session["clicks"]);

    rowCount++;

    //In each button clic save the numbers into the session.
    Session["clicks"] = rowCount;


    //Create the textboxes and labels each time the button is clicked.
    for (int i = 0; i < rowCount; i++)
    {

        TextBox TxtBoxU = new TextBox();

        TextBox TxtBoxE = new TextBox();

        Label lblU = new Label();
        Label lblE = new Label();

        TxtBoxU.ID = "TextBoxU" + i.ToString();
        TxtBoxE.ID = "TextBoxE" + i.ToString();

        lblU.ID = "LabelU" + i.ToString();
        lblE.ID = "LabelE" + i.ToString();


        lblU.Text = "Header " + (i + 1).ToString() + " : ";
        lblE.Text = "Value " + (i + 1).ToString() + " : ";

        //Add the labels and textboxes to the Panel.
        Area1.Controls.Add(lblU);
        Area1.Controls.Add(TxtBoxU);

        Area1.Controls.Add(lblE);
        Area1.Controls.Add(TxtBoxE);
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
    int count=Convert.ToInt32( Session["clicks"]);
    for(int j=0;j<count;j++)
    {

        TextBox aa = (TextBox)Area1.FindControl("TextBoxU"+j);
        Response.Write(aa.Text);

        TextBox bb = (TextBox)Area1.FindControl("TextBoxE" + j);
        Response.Write(bb.Text);

    }
 }

SO, Please given a proper resolution of this query. Thank You.

Upvotes: 0

Views: 5666

Answers (1)

Suraj Singh
Suraj Singh

Reputation: 4069

I think you have to call CreateTextBox(); function at Page_Load event also .

Your Button2_Click cause page reload and next time when your page loads your textbox and labels are not created and when you try to access them by their Id's you get null values.

You need to check which event raised the post back and for Button2_Click you need to call CreateTextBox(); function.

On Page_Load

  protected void Page_Load(object sender, EventArgs e)
     {
       if (IsPostBack)
        {
          string eventName = getPostBackControlName();//Checks for the event that
            //Caused postBack 
          if (eventName == "Button2")
           CreateTextbox();

         }
      }

getPostbackControlName -Here

private string getPostBackControlName()
        {
            Control control = null;
            //first we will check the "__EVENTTARGET" because if post back made by       the controls
            //which used "_doPostBack" function also available in Request.Form collection.
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it
            else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                foreach (string ctl in Page.Request.Form)
                {
                    //handle ImageButton they having an additional "quasi-property" in their Id which identifies
                    //mouse x and y coordinates
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = Page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button ||
                             c is System.Web.UI.WebControls.ImageButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control.ID;

        }

protected void Button1_Click1(object sender, EventArgs e)
 {
    CreateTextBox();

 }

protected void Button2_Click(object sender, EventArgs e)
{
    int count=Convert.ToInt32( Session["clicks"]);
    for(int j=0;j<count;j++)
    {

        TextBox aa = (TextBox)Area1.FindControl("TextBoxU"+j);
        Response.Write(aa.Text);

        TextBox bb = (TextBox)Area1.FindControl("TextBoxE" + j);
        Response.Write(bb.Text);

    }
 }

Upvotes: 1

Related Questions