【问题标题】:How can i use <%# Bind("myField") %> from a usercontrol in asp.net?我如何使用 <%# Bind("myField") %> 从 asp.net 中的用户控件?
【发布时间】:2023-03-10 04:36:01
【问题描述】:

正如标题所说,我想将数据从我的用户控件绑定到我页面的 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>

使用此方法,我能够正确显示我的用户控件的字段(使用来自数据库的适当内容),但我无法插入/更新数据。

【问题讨论】:

  • 请向我们展示您用于插入/更新的代码! :)
  • 嗯..没有太多... formview 应该为我做的不是吗?

标签: asp.net data-binding user-controls


【解决方案1】:

在 VS2010 中尝试过 detailsview/gridview。我使用 gridview 显示,然后通过传递值将其链接到 detailsview,我可以在 VS2010 中编辑/删除记录。

这可能是一个很大的帮助 Click here

另外,请查看Click

【讨论】:

    【解决方案2】:

    1 我建议你将find 控制在Page_Init 上,这样你就可以传递你的DataSource(DataTable 或其他)

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

    2 例如,您在控件上声明一个公共属性

    3 你把你的控件绑定在他的代码后面。

    注意:如果您的 userControl 中有网格控件,则可以使用 ItemDataBound

    【讨论】:

    • “你在他的代码后面绑定你的控件”是什么意思?
    • 所以它看起来像 Text=' 还是我必须写类似 Text='' ?
    • 只需
    • 它不起作用,或者我做错了什么:我创建了一个名为“UC”的用户控件。在那个用户控件中,我设置了一个名为“DS”的 EntityDataSource 类型的公共属性。在我页面的 page_init 事件中,我找到了这个控件,并将我页面的 EntityDataSource 分配给我的用户控件的名为“DS”的属性(这是类型是实体数据源)。在我的用户控件的文本框中,我将文本属性设置为 ''...我做错了吗?
    • 它仍然不起作用:(使用这种方法,我看不出asp如何知道将 放在我的用户控件的dataItem中。 ..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多