Adsence750x90

Monday, July 5, 2010

Get Value of Dynamically generated TextBox

how to get value of dynamically generated TextBox using javascript


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class placeholderissue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (TextBox1.Text != string.Empty)
            {
                int count = 0;
                int.TryParse(TextBox1.Text, out count);
                
                for (int i = 0; i < count; i++)
                {
                    TextBox txt = new TextBox();
                    txt.ID = "dynamicText" + i.ToString();
                    if (PlaceHolder1.FindControl(txt.ID) != null)
                    {
                        PlaceHolder1.Controls.Add(txt);
                    }
                }
            }
        }
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != string.Empty)
        {
            int count=0;
            int.TryParse(TextBox1.Text,out count);
            for (int i = 0; i < count; i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "dynamicText" + i.ToString();
                PlaceHolder1.Controls.Add(txt);
            }
        }
    }
}






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: