简体   繁体   English

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

[英]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. 就像标题所说的那样,我想将用户控件中的数据绑定到页面的EntityDataSource。 I tried this: 我尝试了这个:

in my user control: 在我的用户控件中:

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

In my asp page: 在我的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>

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. 在VS2010中使用您尝试过的detailsview / gridview。 I use gridview to display and then link it to detailsview by passing value where I can edit/delete records in VS2010. 我使用gridview进行显示,然后通过传递值将其链接到detailsview,在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) 1我建议您在Page_Initfind控件,以便您传递DataSource(DataTable或其他)

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

2 You declare for example an public property on your control 2例如,您声明控件上的公共财产

3 And you bind your control in his code behind. 3然后,您将控件绑定在他的代码后面。

Nota : If you have grid control in your userControl you can use ItemDataBound 注意:如果用户控件中有网格控件,则可以使用ItemDataBound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用asp.net从另一个usercontrol中调用usercontrol的方法? - How can i call method from usercontrol from another usercontrol using asp.net? 如何从usercontrol调用aspx内容页面方法? ASP.NET,C# - How can I call aspx content page methods from usercontrol? ASP.NET, C# 如何在ASP.NET UserControl中指定* .css文件? - How can I specify a *.css file in an ASP.NET UserControl? 当属性是asp.net中的列表时,如何创建具有从Page属性设置的用户控件? - How can I create a usercontrol with properties set from Page when that property is a list in asp.net? Asp.net UserControl-如何对列表中的对象进行数据绑定? - Asp.net UserControl - How to Data-Bind objects from a list? 如何(如果有的话)使ASP.NET UserControl从另一个UserControl继承? - How (if at all) can you make an ASP.NET UserControl inherit from another UserControl? 如何在ASP.net中将列表框与哈希表绑定 - How can I bind listbox with hashtable in ASP.net 如何在asp.net中使用jquery数组,并从asp.net中读取数组的值 - How can I use a jquery array in asp.net, and reading values of array from asp.net 如何使用ASP.NET MVC将ModelStateDictionary中的数据手动绑定到表示模型? - How can I manually bind data from a ModelStateDictionary to a presentation model with ASP.NET MVC? 如何在用户控件内使用Asp.Net Ajax UpdatePanel - How to use Asp.Net Ajax UpdatePanel inside an usercontrol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM