In some scenarios we will get the situations where we need to generate the controls dynamically and get the values from those dynamically generated controls on different page post backs. The below example will explain how we can generate the text boxes dynamically on change of drop down selection and data which is there in text boxes will be displayed when user click on Read data button.
---------------------Design page-----------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextboxes.aspx.cs"
Inherits="DynamicTextboxes" %>
<!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:DropDownList ID="ddlControls" AutoPostBack="true" runat="server"
onselectedindexchanged="ddlControls_SelectedIndexChanged">
<asp:ListItem Value="-1">--select--</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
<asp:ListItem Value="2">3</asp:ListItem>
<asp:ListItem Value="3">4</asp:ListItem>
</asp:DropDownList>
<br />
<asp:PlaceHolder ID="phTextBoxes" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnRead" runat="server" onclick="btnRead_Click"
Text="Read Data" />
</div>
</form>
</body>
</html>
-------------------------End-------------------------------------------
--------------------------------Code behind code---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DynamicTextboxes : System.Web.UI.Page
{
string strValue = string.Empty;
public int NumberOfControls
{
get
{
if (ViewState["Count"] == null)
{
return 0;
}
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value++;
}
}
protected void Page_Load(object sender, EventArgs e)
{
NumberOfControls = 10;
}
protected override void CreateChildControls()
{
if (ddlControls.SelectedValue != "-1")
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
//NumberOfControls += 1;
CreateTextBoxes(NumberOfControls);
}
else
{
CreateTextBoxes(NumberOfControls);
// Increase the control value to 1
//NumberOfControls = 0;
}
}
}
private void CreateTextBoxes(int noOfTextboxes)
{
phTextBoxes.Controls.Clear();
for (int counter = 0; counter <= noOfTextboxes; counter++)
{
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 18;
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
//tb.Text = "Enter Title " + counter;
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br/>"));
}
}
private void ReadTextBoxes()
{
strValue = string.Empty;
int n = NumberOfControls;
for (int i = 0; i <= NumberOfControls; i++)
{
string boxName = "TextBoxID" + (i + 1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
if (tb != null)
strValue += tb.Text + "\n";
}
Response.Write(strValue);
}
protected void btnRead_Click(object sender, EventArgs e)
{
ReadTextBoxes();
}
protected void ddlControls_SelectedIndexChanged(object sender, EventArgs e)
{
NumberOfControls = Convert.ToInt32(ddlControls.SelectedItem.Text);
CreateTextBoxes(NumberOfControls);
}
}
-----------------------------------End-----------------------------------------
By this way we can dynamically create the controls and get the data from the controls.
---------------------Design page-----------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextboxes.aspx.cs"
Inherits="DynamicTextboxes" %>
<!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:DropDownList ID="ddlControls" AutoPostBack="true" runat="server"
onselectedindexchanged="ddlControls_SelectedIndexChanged">
<asp:ListItem Value="-1">--select--</asp:ListItem>
<asp:ListItem Value="0">1</asp:ListItem>
<asp:ListItem Value="1">2</asp:ListItem>
<asp:ListItem Value="2">3</asp:ListItem>
<asp:ListItem Value="3">4</asp:ListItem>
</asp:DropDownList>
<br />
<asp:PlaceHolder ID="phTextBoxes" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnRead" runat="server" onclick="btnRead_Click"
Text="Read Data" />
</div>
</form>
</body>
</html>
-------------------------End-------------------------------------------
--------------------------------Code behind code---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DynamicTextboxes : System.Web.UI.Page
{
string strValue = string.Empty;
public int NumberOfControls
{
get
{
if (ViewState["Count"] == null)
{
return 0;
}
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value++;
}
}
protected void Page_Load(object sender, EventArgs e)
{
NumberOfControls = 10;
}
protected override void CreateChildControls()
{
if (ddlControls.SelectedValue != "-1")
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
//NumberOfControls += 1;
CreateTextBoxes(NumberOfControls);
}
else
{
CreateTextBoxes(NumberOfControls);
// Increase the control value to 1
//NumberOfControls = 0;
}
}
}
private void CreateTextBoxes(int noOfTextboxes)
{
phTextBoxes.Controls.Clear();
for (int counter = 0; counter <= noOfTextboxes; counter++)
{
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 18;
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
//tb.Text = "Enter Title " + counter;
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br/>"));
}
}
private void ReadTextBoxes()
{
strValue = string.Empty;
int n = NumberOfControls;
for (int i = 0; i <= NumberOfControls; i++)
{
string boxName = "TextBoxID" + (i + 1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
if (tb != null)
strValue += tb.Text + "\n";
}
Response.Write(strValue);
}
protected void btnRead_Click(object sender, EventArgs e)
{
ReadTextBoxes();
}
protected void ddlControls_SelectedIndexChanged(object sender, EventArgs e)
{
NumberOfControls = Convert.ToInt32(ddlControls.SelectedItem.Text);
CreateTextBoxes(NumberOfControls);
}
}
-----------------------------------End-----------------------------------------
By this way we can dynamically create the controls and get the data from the controls.
No comments:
Post a Comment