[英]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 这可能对您有很大帮助,请点击这里
1 I suggest you to find
your control on Page_Init
, so you pass your DataSource (DataTable or other) 1我建议您在
Page_Init
上find
控件,以便您传递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.