.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> WPF >> Post New QuestionBookmark and Share Subscribe to Forum

Adding Controls Dynamically with a loop.

Posted By: Divya     Posted Date: May 23, 2011    Points:5   Category :WPF
How can i dynamically add textboxes on a button click. I'll be showing 4 text boxes initially with a link button ADD MORE, Then after clicking ADD MORE button i need to generate same 4 text boxes.

I'm having a SQL table with 4 textboxes values as columns and i need to insert them into the database.which user enters and need to insert those 4 textboxes values into DB under same columns.How to do this one in wpf

Author: Maverick          Accepted Answer   
Posted Date: May 23, 2011     Points: 40   


Use the code below.

<tr runat="server" id="TrNumberOfSamples" style="display: none">
Number of Sample
<asp:TextBox ID="TxtNumberOfSample" runat="server" SkinID="txtMedium" />&nbsp;&nbsp;
<asp:Button runat="server" ID="LnkBtnSourceSample" Text="Go" CssClass="gridsearchbox_button"
OnClick="LnkBtnSourceSample_Click" />
</tr> <tr><td> <asp:Gridview ID="Gv1" runat="server" SkinID="CustomHeightWidth"
Width="401px" AutoGenerateColumns="false">
<asp:TemplateField HeaderText="Sample">
<HeaderStyle Width="30%" />
<asp:TextBox runat="server" ID="TxtSample" SkinID="txtSmall" Text='<%# Eval("Sample").ToString() %>' /></ItemTemplate>
<asp:TemplateField HeaderText="Source Sample">
<HeaderStyle Width="40%" />
<asp:TextBox runat="server" ID="TxtSourceSample" SkinID="txtSmall" /></ItemTemplate>
<asp:TemplateField HeaderText="Standard Id">
<HeaderStyle Width="30%" />
<asp:TextBox runat="server" ID="TxtStandardId" SkinID="txtSmall" /></ItemTemplate>

on button click event paste the following code.

DataTable dt = new DataTable();
dt .Columns.Add("Id", typeof(string));
dt .Columns.Add("Id1", typeof(string));
dt .Columns.Add("Id3", typeof(string));

for (int i = 1; i <= Convert.ToInt16(TxtNumberOfSample.Text.Trim()); i++)
dt .Rows.Add(DdlSampleType.SelectedItem.ToString() + i.ToString(), "0");
Gv1.DataSource = dt;

Hope it helps.


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend