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


Top 5 Contributors of the Month
david stephan

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



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

Hello,

Use the code below.

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


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;
Gv1.DataBind();


Hope it helps.

Thanks,
Maverick


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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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