Friday, September 10, 2010

dynamic creation of textboxes

by Narendra Sammeta, .Net Developer

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UPanel" runat="server">
<ContentTemplate>
<asp:Button ID="btn_CreateTxtBox" runat="server" ForeColor="blue"
Font-Bold="true" BackColor="Yellow"
Text="Click here to CreateTextBoxes dynamically" onclick="btn_CreateTxtBox_Click"
/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
===


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

namespace examples_Post
{
public partial class exp8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn_CreateTxtBox_Click(object sender, EventArgs e)
{
int count = 0;

if (ViewState["Textboxes"] != null)
count = (int )ViewState["Textboxes"];
count = count + 1;
ViewState["Textboxes"] = count;

for (int i = 0; i < count; i++)
{
TextBox tbox = new TextBox();
tbox.Text = "";
tbox.ID = "TextBox" + count;
UPanel.ContentTemplateContainer.Controls.Add(tbox);
}
}
}
}

0 comments:

Post a Comment

Related Posts with Thumbnails