开发者

Databinding on a property of a UserControl in DetailsView

I have developped a usercontrol, but I can't seem to find how to databind on a property of the usercontrol in a detailsview.

The property is defined this way:

<Bindable(True, BindingDirection.TwoWay)>
Public Property Value As String
    Get
        Return combobox.SelectedValue
    End Get
    Set(value As String)
        combobox.SelectedValue = value
    End Set
End Property

My databinding in my details view is done this way:

<myctls:DropDown runat="server" 
                 ID="ctlSupplier" 
                 Value='<%#Bind("supplier_id") %>' />

Whenever I try to Databind, I get the following exception:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Anyone got any ideas of how to make it possible to databind on a property of a usercontrol ?

*Edit 1: Added DetailsViewCode and object datasource *

<asp:DetailsView runat="server" ID="dvMain" DataSourceID="odsReception" AutoGenerateRows="false" GridLines="None" DefaultMode="Edit">
  <Fields>  
     <asp:TemplateField HeaderText="Supplier">
       <ItemTemplate>
         <myctls:DropDown runat="server" ID="ctlSupplier" Width="255" Value='<%#Bind("supplier_id") %>' />                                                                                                                    
       </ItemTemplate>
     </asp:TemplateField>

     [... bunch of fields ...]
   </Fields>
</asp:DetailsView>

<asp:ObjectDataSource runat="server" ID="odsReception" Delet开发者_如何学运维eMethod="Delete" 
        InsertMethod="Insert" SelectMethod="Select" 
        TypeName="DataAccess.Reception" 
        UpdateMethod="Update">

    <SelectParameters>
       [... Bunch of parameters ...]
    </SelectParameters>

    [... Parameters for other methods ...]
</asp:ObjectDataSource>


Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles DetailsView1.DataBound

            Dim drv As DataRowView = CType(DetailsView1.DataItem,DataRowView)
            Dim supplierId As String = CType(drv("supplier_id"),String)
            Dim ctrl As MyUCControl = CType(DetailsView1.FindControl("ctlSupplier"),MyUCControl)
            If (Not (ctrl) Is Nothing) Then
                ctrl.Value = supplierId
            End If

    End Sub

'Needs

Imports System.Data
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜