How to use higher level datasource in asp.net custom usercontrol?
I'm setting up a web application with multiple forms. Each form is defined within an asp:FormView with the DataSource set to an ObjectDataSource. Each form contains it's own set of fields and开发者_运维问答 always contains one or more blocks of fields, which are the same for multiple forms.
Because this blocks are the same, I decided to define them in a custom usercontrol. The questions that came up with this:
- How can I use the same datasource for the input fields in the usercontrol as in the 'higher' asp:FormView?
- Is it possible to use DataBinding.Bind() for the input fields in the usercontrol, with this same datasource?
Thanks in advance for replies.
After a long search, I found a similar problem on this website: http://weblogs.asp.net/anasghanem/archive/2009/03/31/sharing-formview-edit-and-insert-templates-and-avoid-duplicate-markup.aspx.
In short the solution hierarchy:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="ObjectDataSource1">
<uc1:TestControl ID="TestControl1" runat="server" PhoneNumber='<%# Bind("PhoneNumber") %>' />
And the codebehind for the usercontrol:
[Bindable(true)]
public partial class TestControl : System.Web.UI.UserControl
{
[Bindable(true), DefaultValue("")]
public string PhoneNumber
{
get
{
return this.PhoneTextBox.Text;
}
set
{
this.PhoneTextBox.Text = value;
}
}
}
精彩评论