Skip to main content

How To Create Dynamic Control based on a selected value




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


<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<table>

<tr>

<td>

<asp:DropDownList ID="ddlist" runat="server" AutoPostBack="True"
OnSelectedIndexChanged
="ddlist_SelectedIndexChanged">

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

<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:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>

</asp:DropDownList>

</td>

</tr>

<tr>

<td>

<asp:PlaceHolder ID="pnl" runat="server" Visible="true"></asp:PlaceHolder>

</td>

</tr>

</table>

</div>

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


using System;
using System.Data;
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 Dynamic : System.Web.UI.Page
{

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
HtmlTable oHtmlTable = new HtmlTable();
oHtmlTable.ID = "tbl";
oHtmlTable.EnableViewState = true;
if (IsPostBack)
{
string ostr = Page.Request.Params["__EVENTTARGET"];
string oValue = Page.Request.Form[ostr];
if (oValue != null && int.Parse(oValue) > 0)
{
for (int i = 0; i < int.Parse(oValue); i++)
{
HtmlTableCell ocell = new HtmlTableCell();
HtmlTableRow orow = new HtmlTableRow();
TextBox oTextBox = new TextBox();
oTextBox.ID = "txtBx" + i.ToString();
ocell.Controls.Add(oTextBox);
orow.Cells.Add(ocell);
oHtmlTable.Rows.Add(orow);
}
pnl.Controls.Clear();
pnl.Controls.Add(oHtmlTable);
Session["MyTable"] = (HtmlTable)FindControl("tbl");
}
else
pnl.Controls.Add((HtmlTable)Session["MyTable"]);
}

}
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder ohtmltable = (PlaceHolder)FindControl("pnl");
updatetextboxes(ohtmltable);
}
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
PlaceHolder ohtmltable = (PlaceHolder)FindControl("pnl");//(HtmlTable)FindControl("tbl");
if (ohtmltable != null)
{
updatetextboxes(ohtmltable);
Response.Write("U r successfull");
}
}

private void updatetextboxes(Control oControl)
{
if (oControl.HasControls())
{
foreach (Control c in oControl.Controls)
updatetextboxes(c);
}
else if (oControl is System.Web.UI.WebControls.TextBox)
((TextBox)oControl).Text = ((TextBox)oControl).ID;

}

}

Comments

Popular posts from this blog