![](/img/trans.png)
[英]How can i call method from usercontrol from another usercontrol using asp.net?
[英]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>
使用這種方法,我能夠正確顯示我的用戶控件的字段(具有來自數據庫的適當內容),但是我無法插入/更新數據。
1我建議您在Page_Init
上find
控件,以便您傳遞DataSource(DataTable或其他)
var control = (UserControl)this.FindControl("id");
control.Property = .....;
2例如,您聲明控件上的公共財產
3然后,您將控件綁定在他的代碼后面。
注意:如果用戶控件中有網格控件,則可以使用ItemDataBound
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.