繁体   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