CodeEngine
CodeEngine

Reputation: 296

How to create a Custom control with controls inside of it so I can repeat it 25 times on webform

I need To create a form that has at least 25 of the fallowing rows. The user is going to add data and these would be saved to an existing data base with 6 Fields. What is the best way to do this. Can you create a Custom control which has this three controls inside of it ? Any help would be appreciated.

----------------   -----------  --------------
|               | |          |  |            |  
|  Text Box     | |   Label  |  | Text Box   |  
----------------  ------------  -------------

I created this ListView :

 <asp:ListView ID="ListView1" runat="server" DataSourceID="XmlDataSource2" >
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" runat="server" id="tblProducts">
        <tr id="Tr1" runat="server">
          <th id="Th1" runat="server">EmployeeID</th>
          <th id="Th2" runat="server">Name</th>
          <th id="Th3" runat="server">Absentee Code</th>
        </tr>
        <tr runat="server" id="itemPlaceholder" />
</table>    
</LayoutTemplate>
<ItemTemplate>

<tr runat="server">
<td>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>    
</ItemTemplate>        
</asp:ListView>
<asp:XmlDataSource ID="XmlDataSource2" runat="server" 
    DataFile="~/App_Data/EmptyRows.xml"></asp:XmlDataSource>
<div style="height: 36px">
    <asp:Button ID="Button1" runat="server" Text="Submitt" />
</div>

.VB code to try to get the values of each row but not working

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim lview As ListViewItem

    For Each lview In ListView1.Items

        Dim control As Control = lview.FindControl("TextBox1")
        Dim resutltxtb1 = control.Text.ToString

    Next
End Sub

Upvotes: 0

Views: 164

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

Define a Repeater or ListView with the ItemTemplate of what you desire. Bind to it 25 blank rows, and this will appear as you desire. On save, you can loop through the rows to get the control values. using FindControl().

Upvotes: 1

Related Questions