Question

I'm new to coding, hope some can help me a bit, I got stuck at retrieving data from my Dynamically added Text boxes in ASP.NET.

I Master Site and a Content site. I have added some buttons to the content site there are adding or removing the textboxes, after what's needed by the user.

My problem is, that i'm not sure how to retrieve the data correct. hope some body can help me on the way.

My Content site:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

<div id="div_fortext" class="div_fortext">
    <p class="header2">
        Opret RMA Sag
    </p>

    <p class="text1">
        Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
    </p>
</div>

<div id="div_insert_devices"  runat="server">   

</div>
      // 3 buttons one who add, one who remove textboxes and a submit button
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
<asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />


</asp:Content>

My C# code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
    }

    private void InsertLine()
    {
        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }    
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());     
        count++;

        ViewState["DeviceCount"] = count;
        InsertLine();
    }

    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());
        count--;
        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {   
        // Submit - save the textboxes to Strings ??? Can any body help
    }
}
Was it helpful?

Solution 5

Hi I found the solution after some time here... I did not got any of examples to work that you have provided. sorry.

But I almost started from scratch and got this to work precis as I wanted :)

My Content Site:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

   <div id="div_fortext" class="div_fortext">

       <p class="header2">
           Opret RMA Sag
       </p>

       <p class="text1">
           Some TEXT
       </p>

   </div>




   </div>

   <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
   <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />--%>



   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

   <asp:PlaceHolder runat="server" id="DynamicDevices"></asp:PlaceHolder>

   <asp:Button id="btnAddTextBox" runat="server" text="Tilføj" CssClass="butten1" OnClick="btnAddTextBox_Click" />
   <asp:Button id="btnRemoveTextBox" runat="server" text="Fjern" CssClass="butten1" OnClick="btnRemoveTextBox_Click" />



   <asp:Button runat="server" id="Submit" text="Submit" CssClass="butten1" OnClick="Submit_Click" />
   <br /><asp:Label runat="server" id="MyLabel"></asp:Label>

</asp:Content>

My C# code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    static int myCount = 1;
    private TextBox[] dynamicTextBoxes;
    private TextBox[] Serial_arr;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myCount = 1;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
        {
            if (myControl.ID.ToString() == "btnAddTextBox")
            {
                myCount = myCount >= 30 ? 30 : myCount + 1;
            }

            if (myControl.ID.ToString() == "btnRemoveTextBox")
            {
                myCount = myCount <= 1 ? 1 : myCount - 1;
            }

        }
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dynamicTextBoxes = new TextBox[myCount];
        Serial_arr = new TextBox[myCount];
        int i;
        for (i = 0; i < myCount; i += 1)
        {
            LiteralControl literalBreak = new LiteralControl("<div>");
            DynamicDevices.Controls.Add(literalBreak);


            TextBox Serial = new TextBox();
            Serial.ID = "txtSerial" + i.ToString();
            Serial.CssClass = "";
            DynamicDevices.Controls.Add(Serial);
            Serial_arr[i] = Serial;

            TextBox textBox = new TextBox();
            textBox.ID = "myTextBox" + i.ToString();
            DynamicDevices.Controls.Add(textBox);
            dynamicTextBoxes[i] = textBox;


            literalBreak = new LiteralControl("</div>");
            DynamicDevices.Controls.Add(literalBreak);
        }
    }


    public static Control GetPostBackControl(Page thePage)
    {
        Control mycontrol = null;
        string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
        if (((ctrlname != null) & (ctrlname != string.Empty)))
        {
            mycontrol = thePage.FindControl(ctrlname);
        }
        else
        {
            foreach (string item in thePage.Request.Form)
            {
                Control c = thePage.FindControl(item);
                if (((c) is System.Web.UI.WebControls.Button))
                {
                    mycontrol = c;
                }
            }
        }
        return mycontrol;
    }


    protected void Submit_Click(object sender, EventArgs e)
    {

        int deviceCount = Serial_arr.Count();

        DataSet ds = new DataSet();
        ds.Tables.Add("Devices");

        ds.Tables["Devices"].Columns.Add("Serial", System.Type.GetType("System.String"));
        ds.Tables["Devices"].Columns.Add("text", System.Type.GetType("System.String"));

        for (int x = 0; x < deviceCount; x++)
        {

            DataRow dr = ds.Tables["Devices"].NewRow();

            dr["Serial"] = Serial_arr[x].Text.ToString();
            dr["text"] = dynamicTextBoxes[x].Text.ToString();

            ds.Tables["Devices"].Rows.Add(dr);

        }

        //MyLabel.Text = "der er " + deviceCount +" Devices<br />";
        //foreach (TextBox tb in Serial_arr)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}
        //MyLabel.Text += "<br />";
        //foreach (TextBox tb in dynamicTextBoxes)
        //{
        //    MyLabel.Text += tb.Text + " :: ";
        //}


    }

    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {

    }
    protected void btnRemoveTextBox_Click(object sender, EventArgs e)
    {

    }





}

Thanks For all the help anyway :) Hope somebody can use this.

Best regards Kasper :)

OTHER TIPS

((TextBox)div_insert_devices.FindControl("txtboxname")).Text

Try this one

you can use the following code

protected void btnSubmit_Click(object sender, EventArgs e)
{   
    // Submit - save the textboxes to Strings ??? Can any body help
    int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

    for (int i = 0; i < DeviceCount; i++)
    {
      TextBox txtbx= (TextBox)div_insert_devices.FindControl("serial" + i);
      if(txtbx!=null)
      {
        var value= txtbx.Text;
      }
    }
}        

The way i would attempt this is like the following:

protected void btnSubmit_Click(object sender, EventArgs e)
{
      foreach (Control control in div_insert_devices.Controls){
            if (control.GetType() == typeof(textbox)){
                   Textbox myDynTextbox = (Textbox)control;

                   Var myString = myDynTextbox.Text;

Please note this code can be simplyfied further but, i have written it this was so you understand how it would work, and my advise would be to store all the strings in a collection of Strings, making it easier to maintain.

            }
      }

}

Kush

Why don't you use javascript to save the value of textbox. just hold the value in some hidden field and bind it everytime when you need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top