Dynamically Adding Text Box In A page

Last post 01-09-2009 2:05 PM by NC01. 32 replies.

Sort Posts:

  • Dynamically Adding Text Box In A page

    01-07-2009, 6:47 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

     Hi,All

    I want to implement this

    I have A Aspx Page

    I have A Asp.net text Box control 
    and A Asp.net submit button also

    Now In code behind on button click event  I am Processing The data of that text box


    Now I want that there should be a link button near to that Text Box WIth NAme Add More

    I want that When Uesr clicks that link  a new Text Box Shuld Appear Just Below That first Text Box

    Now how cna i do That .

    Also When User now clicks the submit button  now I can fetch the data  of that second button Also.


    How can I do That .

    Please Help me.
     

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-07-2009, 7:22 AM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    link button

    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Add More</asp:LinkButton>

     

    protected void Page_Load(object sender, EventArgs e)

    {

    //To get its value on postback

    if (Request.Params["txt2"] != null)

    {

    string val = Request.Params["txt2"];

    }

    }

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

    TextBox txt = new TextBox();

    txt.ID = "txt2";

    this.form1.Controls.Add(txt);

    }

  • Re: Dynamically Adding Text Box In A page

    01-07-2009, 7:27 AM
    • Member
      32 point Member
    • lecanu
    • Member since 01-06-2009, 9:56 AM
    • Posts 11

    You have to do that on client side ? If not, use visible attributes of controls.
    If you have to do that on client side, you can use the DOM functions (document.createElement), but it might be complicated to fetch data of the new control on server side (It will not be registered on server side).

    Tell me if you have to do that on client side

  • Re: Dynamically Adding Text Box In A page

    01-07-2009, 7:38 AM
    • All-Star
      76,225 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,204
    • TrustedFriends-MVPs

    Like this? (change "h ref" to "href").

    aspx file:

    <form id="form1" runat="server">
     <div id="dynamicControlDiv">
      <a h ref="#" onclick="createTextBox(this);">Add</a>
     </div>
     <asp:button id="submitButton" runat="server" text="Submit"></asp:button>
    </form>

    <script type="text/javascript">
    <!--
    function createTextBox(elementRef)
    {
     // Hide the link button so that only one TextBox can be added...
     elementRef.style.display = 'none';

     var controlRef = document.createElement('input');
     controlRef.type = 'text';
     controlRef.name = 'dynamicTextBox';
     controlRef.id = 'dynamicTextBox';
     controlRef.size = 20;
     document.getElementById('dynamicControlDiv').appendChild(controlRef);
    }
    // -->
    </script>

    aspx.cs file:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
         string dynamicTextBoxValue = (this.Request["dynamicTextBox"] == null) ? string.Empty : this.Request["dynamicTextBox"];

         this.Response.Write("dynamicTextBoxValue: ->" + dynamicTextBoxValue + "<-<br>");
        }
    }

    NC...

  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 12:52 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

     Hi,NC01

     FIrst I want to ask that what is the affect of changing href to h ref in abobe hyperlink

    Now My case Is that I tried Ur code that on clicking that link A new Text Box Is Created NOw when user Clicks The button its value u r saying is available at 

    Page load event I want that value in button click eventhandler ,

    Second There is other issue also that suppose If I am making And validation check in button click handler (Suppose I am comparing that second text box value with one Seesion value )

    And suupse that it does not matches then I have to return back to the  page with two text boxes with their values remain as it is 

     

    BUt in ur case I tried That Second text box disappear after returning back from button Clicks event

    How Could I maintain that as it is Confused

     

     

     

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 12:57 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

     Hi,mudassar

    I tried ur code also 

    U have Written  this.form1.Controls.Add(txt);

    I want to tell u that I have this page as child page And my master  page coding is seprate

    So  this.form1.Controls.Add(txt); is not working

     

    And The Same As I specified Above I want the value at button click event and in ur case aslo after validation check that second text box disappears after button click event I want to restore thatSad

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:06 AM
    Answer
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    This way

    protected void Page_Load(object sender, EventArgs e)

    {

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

    TextBox txt = new TextBox();

    txt.ID = "TextBox1";

    content.Controls.Add(txt);

    }

     

  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:13 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

     Hi,mudassar

    Ok I have made that 

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
            TextBox txt = new TextBox();

            txt.ID = "txt2";
            content.Controls.Add(txt);
        }

     

    But now Request.Params["txt2"] is always null in the page load event how can i fetch that

            if (Request.Params["txt2"] != null)
            {
                string val = Request.Params["txt2"];

            }

     

     

    Mudassar The problem is still the same After button clicks event that text box dynamiclay added has vanished

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:21 AM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    On each postback you will have to Re Create the dynamic controls on page load other wise they will vanish

    Even if you use Javascript to do that it will be the same case

  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:24 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

    Hi,

    ACtually I have this code

    <table style="width: 100%;" cellpadding="0" cellspacing="0">

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field1: *

    </td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox3" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 30px; width: 142px; text-align: left;" class="lbl_text">

    Field2.: *</td>

    <td style="height: 30px; width: 125px; vertical-align: middle;">

    <asp:TextBox ID="TextBox1" runat="server" Width="165px" class="textboxAd"></asp:TextBox>

    </td>

    <td style="height: 30px; width: 158px;" class="lbl_text" colspan="2">

    Field6&nbsp;&nbsp;

    <asp:TextBox ID="TextBox4" runat="server" Width="63px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field3:*</td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox6" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field4:

    </td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox5" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 33px; width: 142px; text-align: left; vertical-align:top ;padding-top:18px; " class="lbl_text">

    Field5&nbsp;

    </td>

    <td style="height: 33px; width: 366px; vertical-align: middle;" class="lbl_text"

    colspan="3">

    <br />

    &nbsp;<asp:RadioButton ID="RadioButton1" runat="server" Text="Days"/>

    &nbsp;&nbsp;&nbsp;

    &nbsp; &nbsp; &nbsp; <asp:RadioButton ID="RadioButton2" runat="server" Text="Months"/>

    <br />

    <br />

    <br />

    <asp:DropDownList ID="DropDownList2" runat="server" Width="176px">

    <asp:ListItem Text="Please Select" Value="-1"></asp:ListItem>

    <asp:ListItem Text="0" Value="0"></asp:ListItem>

    <asp:ListItem Text="1" Value="0"></asp:ListItem>

    <asp:ListItem Text="2" Value="0"></asp:ListItem>

    <asp:ListItem Text="3" Value="0"></asp:ListItem>

    <asp:ListItem Text="4" Value="0"></asp:ListItem>

    <asp:ListItem Text="5" Value="0"></asp:ListItem>

    <asp:ListItem Text="6" Value="0"></asp:ListItem>

    <asp:ListItem Text="7" Value="0"></asp:ListItem>

    <asp:ListItem Text="8" Value="0"></asp:ListItem>

    <asp:ListItem Text="9" Value="0"></asp:ListItem>

    <asp:ListItem Text="10" Value="0"></asp:ListItem>

    <asp:ListItem Text="11" Value="0"></asp:ListItem>

    <asp:ListItem Text="12" Value="0"></asp:ListItem>

    <asp:ListItem Text="13" Value="0"></asp:ListItem>

    <asp:ListItem Text="14" Value="0"></asp:ListItem>

    <asp:ListItem Text="15" Value="0"></asp:ListItem>

    <asp:ListItem Text="16" Value="0"></asp:ListItem>

    <asp:ListItem Text="17" Value="0"></asp:ListItem>

    <asp:ListItem Text="18" Value="0"></asp:ListItem>

    <asp:ListItem Text="19" Value="0"></asp:ListItem>

    <asp:ListItem Text="20" Value="0"></asp:ListItem>

    <asp:ListItem Text="21" Value="0"></asp:ListItem>

    <asp:ListItem Text="22" Value="0"></asp:ListItem>

    <asp:ListItem Text="23" Value="0"></asp:ListItem>

    <asp:ListItem Text="24" Value="0"></asp:ListItem>

    <asp:ListItem Text="25" Value="0"></asp:ListItem>

    <asp:ListItem Text="26" Value="0"></asp:ListItem>

    <asp:ListItem Text="27" Value="0"></asp:ListItem>

    <asp:ListItem Text="28" Value="0"></asp:ListItem>

    <asp:ListItem Text="29" Value="0"></asp:ListItem>

    <asp:ListItem Text="30" Value="0"></asp:ListItem>

    </asp:DropDownList>

    &nbsp; &nbsp; &nbsp;</td>

    </tr>

    </table>

     

     

    Ok I wnat that whole section to dynamicaly add on click of Add more link button

    Do u think It iosok

    Do u think i sholud implement that whole code in Html Page and on the top of form tag after writing that post onext page

    And next Page Would Be my aspx On that page shuld i do All that controls Processing

     

    Please Help Me.How Could I do That Is that is efficien to do dynamically control adding in aspx page or  shuld i go for html page and do add that all dynamic controls throgh java script

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:33 AM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    If you want the whole sectio as it is

    I will suggest put it in a user control and add the user control dynamically on click of link button

     

    Second Way

    create a html string server side

    Dim strResults As New StringBuilder

    strResults.Append("<table>")

    For i As Integer = 0 To ds.tables(0).rows.Count

    strResults.Append("<tr>")
    strResults.Append("<td>")

    strResults.Append(ds.tables(0).rows(i)("ColumnName"))

    strResults.Append("</td>")

    strResults.Append("<tr>")

    Next

    strResults.Append("</table>")

    divResults.innerHTML = strResults.ToString()

     

    and create a dymamic panel/div and assign this HTML to the panel each time on click

  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 1:41 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

     Hi,mudassar

    Thanks For Replying

    I like this To more proficient

    mudassarkhan:
    I will suggest put it in a user control and add the user control dynamically on click of link button
     

    Could U please Let Me How to do that 

    Just That source code u can give me example 

    and also Please let me know that how would I get suppose I have added three dynamic whole that controls How Wuld I will fetch tha values in code behind of my contenet page

    Confused 

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 2:02 AM
    Answer
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs
  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 2:46 AM
    • All-Star
      18,856 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 8:25 AM
    • Delhi, India
    • Posts 3,449

    Hi,mudassar

    Ok I have Created A usercontrol And Added Dynamically In My Page

    Here it is Usercontrol code

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductDetails.ascx.cs" Inherits="ProductDetails" %>

    <table style="width: 100%;" cellpadding="0" cellspacing="0">

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field1: *

    </td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox3" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 30px; width: 142px; text-align: left;" class="lbl_text">

    Field2.: *</td>

    <td style="height: 30px; width: 125px; vertical-align: middle;">

    <asp:TextBox ID="TextBox1" runat="server" Width="165px" class="textboxAd"></asp:TextBox>

    </td>

    <td style="height: 30px; width: 158px;" class="lbl_text" colspan="2">

    Field6&nbsp;&nbsp;

    <asp:TextBox ID="TextBox4" runat="server" Width="63px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field3:*</td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox6" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 35px; width: 142px; text-align: left;" class="lbl_text">

    Field4:

    </td>

    <td style="height: 35px; width: 130px; vertical-align: middle;" colspan="3">

    <asp:TextBox ID="TextBox5" runat="server" Width="166px" class="textboxAd"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td style="height: 33px; width: 142px; text-align: left; vertical-align:top ;padding-top:18px; " class="lbl_text">

    Field5&nbsp;

    </td>

    <td style="height: 33px; width: 366px; vertical-align: middle;" class="lbl_text"

    colspan="3">

    <br />

    &nbsp;<asp:RadioButton ID="RadioButton1" runat="server" Text="Days"/>

    &nbsp;&nbsp;&nbsp;

    &nbsp; &nbsp; &nbsp; <asp:RadioButton ID="RadioButton2" runat="server" Text="Months"/>

    <br />

    <br />

    <br />

    <asp:DropDownList ID="DropDownList2" runat="server" Width="176px">

    <asp:ListItem Text="Please Select" Value="-1"></asp:ListItem>

    <asp:ListItem Text="0" Value="0"></asp:ListItem>

    <asp:ListItem Text="1" Value="0"></asp:ListItem>

    <asp:ListItem Text="2" Value="0"></asp:ListItem>

    <asp:ListItem Text="3" Value="0"></asp:ListItem>

    <asp:ListItem Text="4" Value="0"></asp:ListItem>

    <asp:ListItem Text="5" Value="0"></asp:ListItem>

    <asp:ListItem Text="6" Value="0"></asp:ListItem>

    <asp:ListItem Text="7" Value="0"></asp:ListItem>

    <asp:ListItem Text="8" Value="0"></asp:ListItem>

    <asp:ListItem Text="9" Value="0"></asp:ListItem>

    <asp:ListItem Text="10" Value="0"></asp:ListItem>

    <asp:ListItem Text="11" Value="0"></asp:ListItem>

    <asp:ListItem Text="12" Value="0"></asp:ListItem>

    <asp:ListItem Text="13" Value="0"></asp:ListItem>

    <asp:ListItem Text="14" Value="0"></asp:ListItem>

    <asp:ListItem Text="15" Value="0"></asp:ListItem>

    <asp:ListItem Text="16" Value="0"></asp:ListItem>

    <asp:ListItem Text="17" Value="0"></asp:ListItem>

    <asp:ListItem Text="18" Value="0"></asp:ListItem>

    <asp:ListItem Text="19" Value="0"></asp:ListItem>

    <asp:ListItem Text="20" Value="0"></asp:ListItem>

    <asp:ListItem Text="21" Value="0"></asp:ListItem>

    <asp:ListItem Text="22" Value="0"></asp:ListItem>

    <asp:ListItem Text="23" Value="0"></asp:ListItem>

    <asp:ListItem Text="24" Value="0"></asp:ListItem>

    <asp:ListItem Text="25" Value="0"></asp:ListItem>

    <asp:ListItem Text="26" Value="0"></asp:ListItem>

    <asp:ListItem Text="27" Value="0"></asp:ListItem>

    <asp:ListItem Text="28" Value="0"></asp:ListItem>

    <asp:ListItem Text="29" Value="0"></asp:ListItem>

    <asp:ListItem Text="30" Value="0"></asp:ListItem>

    </asp:DropDownList>

    &nbsp; &nbsp; &nbsp;</td>

    </tr>

    </table>

     

     

    Now Coming back to My content page

    Aspx coding

    <%@ Register TagPrefix="CP" TagName="ProductDetails" Src="~/ProductDetails.ascx" %>

    <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="System.Web.UI" TagPrefix="asp" %>

    <asp:Content runat="server" ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1">

    <div>

    How many Items Do u Have<asp:DropDownList ID="DropDownList1" runat="server">

    <asp:ListItem Text="1" Value="1"></asp:ListItem>

    <asp:ListItem Text="2" Value="2"></asp:ListItem>

    <asp:ListItem Text="3" Value="3"></asp:ListItem>

    <asp:ListItem Text="4" Value="4"></asp:ListItem>

    <asp:ListItem Text="5" Value="5"></asp:ListItem>

    </asp:DropDownList><asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Add Sections</asp:LinkButton>

    <br />

    </div>

    <div>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    </div>

    </asp:Content>

     

    And In aspx.cs

     

    using System;

    using System.Data;

    using System.Data.SqlClient ;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    public partial class TestNew123 : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

     

    }

     

     

    protected void Button1_Click(object sender, EventArgs e)

    {

    }

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

    ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");for (int i = 0; i < Convert.ToInt32(DropDownList1.SelectedValue); i++)

    {

    Control cs = LoadControl("~/ProductDetails.ascx");

    cs.ID = "contr" + i.ToString() ;

    content.Controls.Add(cs);

    }

    }

    }

     

     

     

     

    Now My question Is How to fetch that dynamically Added Varios Text boxes and radiobuttons value in butoon Click EventConfused

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Dynamically Adding Text Box In A page

    01-08-2009, 3:26 AM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    For that you will have to avoid asp controls since theier id changes you eill nedd to use html controls in UserControl

     <input id="Text1" type="text" name = "txt" />

    name tag is most important

    if (Request.Form ["txt"] != null)

    {

    string str = Request.Form["txt"];

    }

     

    Suppose you add 3 usercontols

    then

    string str = Request.Form["txt"];

    will give you

    all 3 textbox values separated by comma

Page 1 of 3 (33 items) 1 2 3 Next >