VB.NET Repeater Simple Data Binding Without Dataso

2019-01-26 17:20发布

问题:

I've got a form that has to be a repeater on a webpage. I have a number parameter that tells me how many additional customer details are to be added. In this page I have a repeater control that needs to repeat items a set number of times. For each item there is a set of input boxes that needs an id appended to it. All I need to bind is a number for each additional input box ie <% #id %>. I dont know how to set up simple databind without a specific datasource, but just build one in a loop.

<asp:Repeater ID="myRepeater" runat="server"> 
    <HeaderTemplate>  
       <div class="AdditionalRowTitle">
            <div class="AdditionalCellTitle">Title</div>
            <div class="AdditionalCellFirstName">First Name</div>
            <div class="AdditionalCellLastName">Last Name</div>
            <div class="AdditionalCellDOB">DOB</div>
            <div class="AdditionalCellRelationship">Relationship</div>
        </div>
    </HeaderTemplate>

    <ItemTemplate>
       <div class="AdditionalRow">
            <div class="AdditionalInputTitle">
                <asp:DropDownList ID="AdTitle" runat="server">
                    <asp:ListItem Selected="True" Value="" Text=""/>
                    <asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
                    <asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
                    <asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
                    <asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
                    <asp:ListItem Selected="False" Value="Other" Text="Other"/>
                </asp:DropDownList>
            </div>

            <div class="AdditionalInputFirstName">
                <asp:TextBox ID="AdFirstName" runat="server"></asp:TextBox>
            </div>

            <div class="AdditionalInputLastName">
                <asp:TextBox ID="AdLastName" runat="server"></asp:TextBox>
            </div>

            <div class="AdditionalInputDOB">
                <asp:DropDownList ID="AdDOBDay" runat="server">
                    <asp:ListItem Selected="True" Value="" Text=""/>
                    <asp:ListItem Selected="False" Value="1" Text="1"/>
                    <asp:ListItem Selected="False" Value="2" Text="2"/>
                    <asp:ListItem Selected="False" Value="3" Text="3"/>
                    <asp:ListItem Selected="False" Value="4" Text="4"/>
                    <asp:ListItem Selected="False" Value="5" Text="5"/>
                    <asp:ListItem Selected="False" Value="6" Text="6"/>
                    <asp:ListItem Selected="False" Value="7" Text="7"/>
                    <asp:ListItem Selected="False" Value="8" Text="8"/>
                    <asp:ListItem Selected="False" Value="9" Text="9"/>
                    <asp:ListItem Selected="False" Value="10" Text="10"/>
                    <asp:ListItem Selected="False" Value="11" Text="11"/>
                    <asp:ListItem Selected="False" Value="12" Text="12"/>
                    <asp:ListItem Selected="False" Value="13" Text="13"/>
                    <asp:ListItem Selected="False" Value="14" Text="14"/>
                    <asp:ListItem Selected="False" Value="15" Text="15"/>
                    <asp:ListItem Selected="False" Value="16" Text="16"/>
                    <asp:ListItem Selected="False" Value="17" Text="17"/>
                    <asp:ListItem Selected="False" Value="18" Text="18"/>
                    <asp:ListItem Selected="False" Value="19" Text="19"/>
                    <asp:ListItem Selected="False" Value="20" Text="20"/>
                    <asp:ListItem Selected="False" Value="21" Text="21"/>
                    <asp:ListItem Selected="False" Value="22" Text="22"/>
                    <asp:ListItem Selected="False" Value="23" Text="23"/>
                    <asp:ListItem Selected="False" Value="24" Text="24"/>
                    <asp:ListItem Selected="False" Value="25" Text="25"/>
                    <asp:ListItem Selected="False" Value="26" Text="26"/>
                    <asp:ListItem Selected="False" Value="27" Text="27"/>
                    <asp:ListItem Selected="False" Value="28" Text="28"/>
                    <asp:ListItem Selected="False" Value="29" Text="29"/>
                    <asp:ListItem Selected="False" Value="30" Text="30"/>
                    <asp:ListItem Selected="False" Value="31" Text="31"/>
                </asp:DropDownList>

                <asp:DropDownList ID="AdDOBMonth" runat="server">
                    <asp:ListItem Selected="True" Value="" Text=""/>
                    <asp:ListItem Selected="False" Value="1" Text="January"/>
                    <asp:ListItem Selected="False" Value="2" Text="Febuary"/>
                    <asp:ListItem Selected="False" Value="3" Text="March"/>
                    <asp:ListItem Selected="False" Value="4" Text="April"/>
                    <asp:ListItem Selected="False" Value="5" Text="May"/>
                    <asp:ListItem Selected="False" Value="6" Text="June"/>
                    <asp:ListItem Selected="False" Value="7" Text="July"/>
                    <asp:ListItem Selected="False" Value="8" Text="August"/>
                    <asp:ListItem Selected="False" Value="9" Text="September"/>
                    <asp:ListItem Selected="False" Value="10" Text="October"/>
                    <asp:ListItem Selected="False" Value="11" Text="November"/>
                    <asp:ListItem Selected="False" Value="12" Text="December"/>
                </asp:DropDownList>

                <asp:DropDownList ID="AdDOBYear" runat="server">
                </asp:DropDownList>

                <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2"
                                           Category="AdDOBYear"
                                           LoadingText=""
                                           ServicePath="TravelWebService.asmx"
                                           ServiceMethod="GetYears"
                                           PromptText=""
                                           TargetControlID="AdDOBYear"
                                           runat="server">
                </ajaxToolkit:CascadingDropDown>
            </div>

            <div class="AdditionalInputRelationship">
                <asp:DropDownList ID="AdRelationship" runat="server">
                    <asp:ListItem Selected="True" Value="" Text=""/>
                    <asp:ListItem Selected="False" Value="Partner" Text="Partner"/>
                    <asp:ListItem Selected="False" Value="Child" Text="Child"/>
                    <asp:ListItem Selected="False" Value="Unrelated" Text="Unrelated"/>
                </asp:DropDownList>
            </div>
        </div>
   </ItemTemplate>
</asp:Repeater>

回答1:

Here's a way to repeat the items. In your page load event, add the following:

Dim repeatTimes() As Integer = New Integer(){1, 2, 3}
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()

The repeater items will be repeated by the number of elements in the Array. In this case, there are 3 elements. The actual contents of the array don't matter, just the number of elements.

EDIT: I don't know VB.NET that well, but I think this should work

Sub Page_Load()
  Dim NumberToRepeat As Integer ''*Should come from the parameter
  If Not Page.IsPostBack Then
    Dim repeatTimes(NumberToRepeat) As Integer
    myRepeater.DataSource = repeatTimes
    myRepeater.DataBind()
  End If
End Sub