簡體   English   中英

我如何在asp.net的用戶控件中使用<%#Bind(“ myField”)%>?

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

就像標題所說的那樣,我想將用戶控件中的數據綁定到頁面的EntityDataSource。 我嘗試了這個:

在我的用戶控件中:

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

在我的ASP頁面中:

<% 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>

使用這種方法,我能夠正確顯示我的用戶控件的字段(具有來自數據庫的適當內容),但是我無法插入/更新數據。

在VS2010中使用您嘗試過的detailsview / gridview。 我使用gridview進行顯示,然后通過傳遞值將其鏈接到detailsview,在VS2010中可以編輯/刪除記錄。

這可能對您有很大幫助,請點擊這里

另外,檢查一下一下

1我建議您在Page_Initfind控件,以便您傳遞DataSource(DataTable或其他)

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

2例如,您聲明控件上的公共財產

3然后,您將控件綁定在他的代碼后面。

注意:如果用戶控件中有網格控件,則可以使用ItemDataBound

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM