VB.net。如何为转发器中的所有下拉列表执行数据绑定

时间:2011-04-07 14:09:52

标签: asp.net vb.net

我的转发器中有一个下拉列表,下拉列表由数据源进行数据绑定。最初所有下拉列表都将具有所有相同的项目选项。我试着做一个显示下拉列表中的一个选择一个选项,然后在其他下拉列表中这个选项将消失。

这是asp:

<asp:Repeater ID="UnitMatchRepeater" runat="server" DataSourceID="OldUnitsDataSource" >
    <ItemTemplate>
       <tr>
       <td>
       <asp:Label ID="OldUnitNumber" runat="server" Text='<%# Eval("Vehicle")%>' value='<%#Container.DataItem("LinkNumber") %>'></asp:Label>
       </td>
       <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        <td>
        <asp:DropDownList ID="NewUnitsDropDownBox" runat="server" DataSourceID="NewUnitsDataSource" DataTextField="UnitNumber"
            DataValueField="LinkNumber" OnDataBound="call_it" AutoPostBack="true" value='<%# Container.DataItem("LinkNumber")%>'></asp:DropDownList>
        </td>
        <br/>
        </tr>
    </ItemTemplate>
    </asp:Repeater>
<asp:SqlDataSource ID="OldUnitsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:minotaurSQLConnectionString %>">
    </asp:SqlDataSource>
    <br />
    <asp:SqlDataSource ID="NewUnitsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:minotaurSQLConnectionString %>" 
        SelectCommand="SELECT Minotaur.dbo.Units.LinkNumber AS LinkNumber, RTAFleet.dbo.vehfile.vehicle AS UnitNumber FROM RTAFleet.dbo.vehfile INNER JOIN Minotaur.dbo.Units ON RTAFleet.dbo.vehfile.link_number = Minotaur.dbo.Units.LinkNumber AND Minotaur.dbo.Units.AcquisitionOrderID = @AcquisitionOrderID AND Minotaur.dbo.Units.SnapShotID = 1 AND Minotaur.dbo.Units.OldLinkNumber IS NULL">
        <SelectParameters>
            <asp:QueryStringParameter Name="AcquisitionOrderID" QueryStringField="orderID" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

这是背后的代码:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim LinkNoArray As String() = Request.QueryString("linkNumber").Split(",")
        Dim SqlStr As String = "SELECT Vehicle = CASE WHEN ISNUMERIC(RTAFleet.dbo.vehfile.vehicle) = 1 AND RTAFleet.dbo.vehfile.vehicle IS NOT NULL THEN RTAFleet.dbo.vehfile.vehicle ELSE  RTAFleet.dbo.vehfile.veh_xref_num END, RTAFleet.dbo.vehfile.link_number AS LinkNumber FROM RTAFleet.dbo.vehfile INNER JOIN Minotaur.dbo.Units ON RTAFleet.dbo.vehfile.link_number = Minotaur.dbo.Units.LinkNumber AND  Minotaur.dbo.Units.SnapShotID = 1 WHERE "
        For Each Str As String In LinkNoArray
            SqlStr = SqlStr & " Minotaur.dbo.Units.LinkNumber = " & Trim(Str) & " OR"
        Next
        If Not Page.IsPostBack Then
            SqlStr = Left(SqlStr, (SqlStr.Length - 2))
            OldUnitsDataSource.SelectCommand = SqlStr
            UnitMatchRepeater.DataBind()
        End If



End Sub

Private Sub UnitMatchRepeater_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles UnitMatchRepeater.ItemCreated
        If Not e.Item.ItemType = ListItemType.Item And Not e.Item.ItemType = ListItemType.AlternatingItem Then
            Exit Sub
        End If
        Dim NewUnitsDDB As DropDownList = e.Item.FindControl("NewUnitsDropDownBox")

        AddHandler NewUnitsDDB.SelectedIndexChanged, AddressOf NewUnitsDDB_SelectedIndexChanged
    End Sub

    Protected Sub NewUnitsDDB_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim linkNo As String = CType(sender, DropDownList).SelectedValue
        Dim sqlStr As String = "UPDATE dbo.Units SET OldLinkNumber = @oldLinkNum WHERE LinkNumber = @newLinkNum"
        Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("minotaurSQLConnectionString").ConnectionString)
        Dim cmd As SqlCommand = New SqlCommand(sqlStr, con)
        cmd.Parameters.Add("@oldLinkNum", SqlDbType.Int).Value = CType(sender, DropDownList).Attributes("value")
        cmd.Parameters.Add("@newLinkNum", SqlDbType.Int).Value = CInt(linkNo)

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()

        dlistdatabinding()


    End Sub

 Private Sub dlistdatabinding()

        For Each rep As RepeaterItem In UnitMatchRepeater.Items
            Dim list As DropDownList = rep.FindControl("NewUnitsDropDownBox")
            list.DataBind()
        Next

End Sub

请帮助了解如何使其正常工作。 感谢

1 个答案:

答案 0 :(得分:0)

将下拉列表数据绑定代码放入转发器的ItemDataBound事件中。这是每次创建&#34;行&#34;在你的中继器。从那里,您可以在项目中找到单独的下拉列表。

这样做的另一个好处是,由于每个项目都是数据绑定的,因此您可能会根据您使用的数据更改下拉列表的内容。