How can generated dynamic controls in asp.net and fetch its value.
How can generated dynamic controls in asp.net and fetch its value.
The need of this dynamic controls is emerge ,when I was
working in some project, where I have to gather information of Number of customer,
which is un-defined.
So its not practical to defined no. of hardcoded controls on
page.so its very good idea to work on dynamic controls of asp.net.
In this article I am creating dynamic textbox’s using
asp.net.You can generated any controls according to your requirements.
.aspx Part
Here I am defined a panel
control inside a update panel ,so
that whenever a new set of control is added on my application, my page should not be refreshed every time. I
also defend a Gridview For display
its Data.
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="plc_dept" runat="server"></asp:PlaceHolder>
<asp:Button ID="btn_dpt_count" runat="server" Text="Add Deptartment" CausesValidation="false"
onclick="btn_dpt_count_Click" />
</ContentTemplate>
</asp:UpdatePanel>
//Submit button for fetching its values.
<asp:Button ID="btn_submit" runat="server" Text="submit"
Width="250px"
CausesValidation="false"
onclick="btn_submit_Click" />
<asp:GridView id-"gr" runat="server"></asp:GridView>
.CS part
static int cno_code = 0;
protected void Page_Load(object sender, EventArgs e)
{
//This is the function which is called every time whenever
a new set of control is added
show_dept_list(cno_dept+1);
}
protected void
btn_dpt_count_Click(object sender, EventArgs e)
{
// cno_dept = cno_dept + 1;
// show_dept_list(cno_dept);
}
public void
show_dept_list(int count)
{
for (int i = 0; i
< count; i++)
{
//
Table t_dept = new Table();
//TableRow tr_main = new TableRow();
//TableCell tc_mai1 = new TableCell();
//TableCell tc_mai2 = new TableCell();
//TableCell tc_mai3 = new TableCell();
//tc_mai1.Text = "Department";
//tc_mai2.Text = "Titel";
//tc_mai3.Text = "Cost";
TableRow tr_dept = new TableRow();
TableCell tc_dept = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TextBox txt = new TextBox();
txt.ID = "txt_dept" +
i.ToString();
txt.Text = "Department";
txt.MaxLength = 100;
txt.Width = new Unit(300,
UnitType.Pixel);
txt.Attributes.Add("runat",
"Server");
TextBox txt_titel = new TextBox();
txt_titel.ID = "txt_titel"
+ i.ToString();
txt_titel.Text = "Titel" ;
txt_titel.MaxLength = 250;
txt_titel.Width = new Unit(300,
UnitType.Pixel);
txt_titel.Attributes.Add("runat",
"Server");
TextBox txt_code = new TextBox();
txt_code.ID = "txt_code" +
i.ToString();
txt_code.Text = "Cost" ;
txt_code.MaxLength = 100;
txt_code.Width = new Unit(300,
UnitType.Pixel);
txt_code.Attributes.Add("runat",
"Server");
RequiredFieldValidator rq1 = new
RequiredFieldValidator();
rq1.Attributes.Add("runat",
"Server");
rq1.ID = "rqq1" +
i.ToString();
rq1.ControlToValidate = "txt_dept"
+ i.ToString();
rq1.ErrorMessage = "*";
tc_dept.Controls.Add(rq1);
RequiredFieldValidator rq2 = new
RequiredFieldValidator();
rq2.Attributes.Add("runat",
"Server");
rq2.ID = "rqq2" +
i.ToString();
rq2.ControlToValidate = "txt_titel"
+ i.ToString();
rq2.ErrorMessage = "*";
tc2.Controls.Add(rq2);
RequiredFieldValidator rq3 = new
RequiredFieldValidator();
rq3.Attributes.Add("runat",
"Server");
rq3.ID = "rqq3" +
i.ToString();
rq3.ControlToValidate = "txt_code"
+ i.ToString();
rq3.ErrorMessage = "*";
tc3.Controls.Add(rq3);
//p1.Controls.Add(txt);
tc_dept.Controls.Add(txt);
tc2.Controls.Add(txt_titel);
tc3.Controls.Add(txt_code);
//tr_main.Controls.Add(tc_mai1);
//tr_main.Controls.Add(tc_mai2);
//tr_main.Controls.Add(tc_mai3);
tr_dept.Controls.Add(tc_dept);
tr_dept.Controls.Add(tc2);
tr_dept.Controls.Add(tc3);
//t_dept.Controls.Add(tr_main);
t_dept.Controls.Add(tr_dept);
plc_dept.Controls.Add(t_dept);
}
}
///Button submit for
fetching its values
protected void
btn_submit_Click(object sender, EventArgs e)
{
DataTable DT = new
DataTable();
DT.Columns.Add("dept");
DT.Columns.Add("titel");
DT.Columns.Add("code");
string tvalue1 = string.Empty;
string controlid1 = string.Empty;
string controlid_titel = string.Empty;
string controlid_code = string.Empty;
for (int i = 0; i
<= plc_dept.Controls.Count; i++)
{
controlid1 = "txt_dept" +
i.ToString();
controlid_titel = "txt_titel"
+ i.ToString();
controlid_code = "txt_code"
+ i.ToString();
TextBox tb1 = plc_dept.FindControl(controlid1) as
TextBox;
TextBox tb2 = plc_dept.FindControl(controlid_titel) as TextBox;
TextBox tb3 = plc_dept.FindControl(controlid_code) as TextBox;
if (tb1 != null)
{
DT.Rows.Add(tb1.Text.ToString(), tb2.Text.ToString(),
tb3.Text.ToString());
}
Gr.DataSourse=DT;
Gr.DataBind();
}
}
Comments
Post a Comment