简体   繁体   中英

How can i use <%# Bind(“myField”) %> from a usercontrol in asp.net?

Like the title says, i would like to bind data from my usercontrol to my page's EntityDataSource. I tried this:

in my user control:

<asp:Textbox runat="server" id="mytxtbox" Text='<%# Bind("myField") %>

In my asp page:

<% Register TagPrefix="uc" TagName="myControl" src="~/MyControl.ascx" %>
...
<asp:EntityDataSource ID="EDS" runat="server" ....></asp:EntityDataSource
<asp:FormView id="FormView1" runat="server" DataKeyNames="id" DataSourceId="EDS">
    <EditItemTemplate>
        <uc:myControl runat="server" id="customControl"/>
        ...
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
        <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <uc:myControl runat="server" id="customControl"/>
        ...
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
        <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </InsertItemTemplate>
    <ItemTemplate>
        <uc:myControl runat="server" id="customControl"/>
        ...
        <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
        <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"/>
    </ItemTemplate>
</asp:formview>

Using this method, i was able to display my user control's fields properly (with the appropriate content from by database), but i cannot insert/Update data.

Have your tried detailsview/gridview in VS2010. I use gridview to display and then link it to detailsview by passing value where I can edit/delete records in VS2010.

This can be a big help Click here

Also, check this out Click

1 I suggest you to find your control on Page_Init , so you pass your DataSource (DataTable or other)

var control = (UserControl)this.FindControl("id");
control.Property = .....;

2 You declare for example an public property on your control

3 And you bind your control in his code behind.

Nota : If you have grid control in your userControl you can use ItemDataBound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM