Adsence750x90

Monday, July 5, 2010

Get Value of Dynamically generated TextBox

how to get value of dynamically generated TextBox using javascript


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. public partial class placeholderissue : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (IsPostBack)  
  13.         {  
  14.             if (TextBox1.Text != string.Empty)  
  15.             {  
  16.                 int count = 0;  
  17.                 int.TryParse(TextBox1.Text, out count);  
  18.                   
  19.                 for (int i = 0; i < count; i++)  
  20.                 {  
  21.                     TextBox txt = new TextBox();  
  22.                     txt.ID = "dynamicText" + i.ToString();  
  23.                     if (PlaceHolder1.FindControl(txt.ID) != null)  
  24.                     {  
  25.                         PlaceHolder1.Controls.Add(txt);  
  26.                     }  
  27.                 }  
  28.             }  
  29.         }  
  30.         
  31.     }  
  32.     protected void Button1_Click(object sender, EventArgs e)  
  33.     {  
  34.         if (TextBox1.Text != string.Empty)  
  35.         {  
  36.             int count=0;  
  37.             int.TryParse(TextBox1.Text,out count);  
  38.             for (int i = 0; i < count; i++)  
  39.             {  
  40.                 TextBox txt = new TextBox();  
  41.                 txt.ID = "dynamicText" + i.ToString();  
  42.                 PlaceHolder1.Controls.Add(txt);  
  43.             }  
  44.         }  
  45.     }  
  46. }  



HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="placeholderissue.aspx.cs" Inherits="placeholderissue" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1"
            runat="server" Text="Button" onclick="Button1_Click" />
        <br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <input id="Button2" type="button" value="button" onclick="GetTextBoxValue();" />
        <script language="javascript">
            function GetTextBoxValue() {
                var count = document.getElementById("TextBox1").value;
                if (count > 0) {
                    for (i = 0; i < count; i++) {
                        alert(document.getElementById("dynamicText" + i).value);
                    }
                }
            }
        </script>
    <div>
    </form>
</body>
</html>




Source Code

No comments: