asp.net - 从中继器数据绑定(bind)数据设置用户控件的属性

标签 asp.net vb.net webforms

我在转发器内有一个由 sqldatasource 绑定(bind)的用户控件。我收到以下错误:Eval()、XPath() 和 Bind() 等数据绑定(bind)方法只能在数据绑定(bind)控件的上下文中使用。 编辑:没关系鸡蛋在我脸上。我收到此数据绑定(bind)错误是因为我将其绑定(bind)到其他地方以解决上周五的实际问题,但我忘记了它。

我真正的问题是什么:用户控件在属性设置之前就被绑定(bind),因此看起来好像它们从未设置过。当单步执行时,我看到他们在属性(property)上的集合被击中之前就到达了属性(property)上。例如,如果我将 <%# EVal("Address_ID") %> 放在用户控件之前,我将看到显示的 ID,但随后用户控件将显示一个空数据模板,因为它传递的 ID 为 0。

    <asp:SqlDataSource ID="sqlFacilityAddresses" runat="server" DataSourceMode="DataSet" SelectCommandType="StoredProcedure" SelectCommand="SP_Facility_GetAddresses" ConnectionString="<%$ ConnectionStrings:Trustaff_ESig2 %>">
        <SelectParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
        <asp:Repeater ID="repeaterAddresses" DataSourceID="sqlFacilityAddresses" runat="server">
            <ItemTemplate>
                <Select:Address ID="AddressControl" runat="server"  AddressID='<%# EVal("Address_ID") %>' />
            </ItemTemplate>
        </asp:Repeater>

最佳答案

您可以处理中继器的 ItemDataBound-Event在代码隐藏中,通过 Item.FindControl 获取对 UserControl 的引用并根据Item.DataItem设置属性对象和您的列 Address_ID

例如(调试以查看您的数据项的类型是否确实是 DataRowView):

Sub repeaterAddresses_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
    ' This event is raised for the header, the footer, separators, and items.
    ' Execute the following logic for Items and Alternating Items.
    If (e.Item.ItemType = ListItemType.Item) Or _
        (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim addressControl as AddressControl = DirectCast(e.Item.FindControl("AddressControl", AddressControl)
        addressControl.AddressID = DirectCast(e.Item.DataItem, DataRowView)("Address_ID").ToString
    End If
End Sub

关于asp.net - 从中继器数据绑定(bind)数据设置用户控件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5459564/

相关文章:

html - 防止对齐的 div 重叠

asp.net - 是否有 “on event”可供下载?

c# - WPF 在关闭时隐藏?

c# - 安全的跨域表单提交

asp.net - ASP .NET 检查 session

c# - 放置验证器会扰乱面板布局

c# - C# 枚举中的空格

c# - 是否有 WebMethod 的替代方案以绕过回发但访问 Session

c# - 在 View 状态中存储控件的自定义属性

c# - asp.net-HTML 控件中的本地化