2
Answers

generate "text box" dynamically

Photo of Rajeev Kumar

Rajeev Kumar

11y
780
1
hello,

 sir i need how to generate "text box" dynamically in asp .net on the button click.

Answers (2)

0
Photo of Sanjeeb Lenka
NA 22.1k 1.3m 11y
Try this:
in aspx:

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

<!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">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />
    </form>
</body>
</html>

in .cs:

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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        TextBox txt = new TextBox();
        txt.ID = "tb";
        txt.Width = 540;
        txt.Height = 60;
        txt.TextMode = TextBoxMode.MultiLine;
        form1.Controls.Add(txt);

    }
}
Accepted
0
Photo of Abhay Shanker
NA 10.5k 2.4m 11y
Try this

on aspx page

<asp:TextBox runat="server" ID="textbox1"/>
<asp:TextBox runat="server" ID="textbox2"/>

<asp:Button runat="server" ID="btnAdd" OnClick="btnAdd_Click" />
<asp:PlaceHolder runat="server" ID="ph" />


in Code Page

protected void btnAdd_Click(object sender, EventArgs e)
{
     
TextBox tb1 = new TextBox();
     
TextBox tb2 = new TextBox();
      ph
.Controls.Add(tb1);
      ph
.Controls.Add(tb2);
}


Also check below link for better understanding

http://www.codeproject.com/Articles/502251/How-to-create-controls-dynamically-in-ASP-NET-and