Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I'm 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 addíng 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:
ASP.NET
<%@ 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:
C#
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

    }
}
Posted

1 solution

HI,

Please check the below sample.

]]>



<title>Untitled Page



<asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown">





<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
<contenttemplate>
<asp:placeholder runat="server" id="myPlaceHolder">

<triggers> <asp:asyncpostbacktrigger controlid="btnAddTextBox" eventname="Click">






<asp:button id="btnAddTextBox" runat="server" text="Add TextBox" onclick="btnAddTextBox_Click" xmlns:asp="#unknown">



<asp:updatepanel id="UpdatePanel2" runat="server" xmlns:asp="#unknown">
<contenttemplate>
<asp:button runat="server" id="MyButton" text="Get Values." onclick="MyButton_Click">



<asp:label runat="server" id="MyLabel">









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

public partial class _Default : System.Web.UI.Page
{
static int myCount = 5;
private TextBox[] dynamicTextBoxes;

protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if (myControl != null)
{
if (myControl.ClientID.ToString() == "btnAddTextBox")
{
myCount = myCount + 1;
}
}

}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dynamicTextBoxes = new TextBox[myCount];
int i;
for (i = 0; i < myCount; i += 1)
{
TextBox textBox = new TextBox();
textBox.ID = "myTextBox" + i.ToString();
myPlaceHolder.Controls.Add(textBox);
dynamicTextBoxes[i] = textBox;
LiteralControl literalBreak = new LiteralControl("
");
myPlaceHolder.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 btnAddTextBox_Click(object sender, EventArgs e)
{
// Handled in preInit due to event sequencing.
}
protected void MyButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "";
foreach (TextBox tb in dynamicTextBoxes)
{
MyLabel.Text += tb.Text + " :: ";
}
}
}
 
Share this answer
 
Comments
Kasper Simonsen 19-Dec-13 8:21am    
Hi
I have tried your sample, but my visual studio is complaning alot over ASP part.

and what is a <asp:asyncpostbacktrigger ? :S can you explain a bit :)
prabhugi2it 20-Dec-13 22:03pm    
Hi kasper,

Please use AJAX in your project then it will work..


AsyncPostBackTrigger is used for the controls which are outside the updatepanel control. AsyncPostBackTrigger allows these controls to trigger an asynchronus post back.

http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger.aspx
http://ramanisandeep.wordpress.com/2009/04/07/asyncpostbacktrigger-vs-postbacktrigger/


If you want to Update/Refresh an ajax update panel by any control,which reside out side of that panel, specify the control ID in <Triggers> property.

you will get information from this link

http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers
http://asp-net-example.blogspot.com/2009/11/ajax-asyncpostbacktrigger-how-to-use.html


if you are facing problem,please give me more details. I will help you
Thanks
Prabhu

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