Install DataSource on controls in asp.net UserControl?

I created my first asp.net UserControl, which I will use in several places in my application. It contains a FormView for displaying record fields in a DataTable.

Everything seems fine, except that I cannot figure out how to set the DataSource in a FormView, which is in the UserControl. I want to set a DataSource in a method with code.

I see from intellisense that UserControl does not have the DataSource property, but it has a DataBind method. I can imagine that it might be necessary to set different DataSources for several controls in UserControl, so there must be some method for drilling in UserControl, but I cannot figure it out.

Here is the aspx code:

<%@ Register src="Controls/JobDetail.ascx" tagname="JobDetail" tagprefix="uc1" %> ... <uc1:JobDetail ID="UserControlJobDetail" runat="server" /> ... 

Here is the method that tries to set the DataSource:

 public void BindJobRecord(string SelectedJobNo) { UserControlJobDetail.DataSource = LMDataClass.GetJob(SelectedJobNo); UserControlJobDetail.DataBind(); } 

And here is the UserControl:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="JobDetail.ascx.cs" Inherits="DwgDatabase.JobDetail" %> <asp:FormView ID="fvJobDetail" runat="server" DataKeyNames="job_num"> <ItemTemplate> <div style="float: left; border-width: 1px;" class="LabelStyle TextBoxStyle" > <table> <tr> <td><asp:label runat="server" ID="lblJobNo" Text='Job No' /></td> <td><asp:TextBox runat="server" ID="txtJobNo" Text='<%# Eval("job_num") %>' /></td> </tr> <tr> <td><asp:label runat="server" ID="Label2" Text='Customer' /></td> <td><asp:TextBox runat="server" ID="txtCustNo" Text='<%# Eval("cust_num") %>' /></td> </tr> <tr> <td><asp:label runat="server" ID="Label3" Text='Quote No' /></td> <td><asp:TextBox runat="server" ID="txtQuoteNo" Text='<%# DataBinder.Eval(Container.DataItem, "quote_no", "{0:00000;;.}") %>' /></td> </tr> <tr> <td><asp:label runat="server" ID="Label4" Text='Po No.' /></td> <td><asp:TextBox runat="server" ID="TextBox4" Text='<%# Eval("p_o_num") %>' /></td> </tr> </table> </div> </ItemTemplate> </asp:FormView> 
+4
source share
1 answer

Create the DataSource property for the user control, as shown below:

 public object DataSource { get { return this.fvJobDetail.DataSource; } set { this.fvJobDetail.DataSource = value; } } 

Do the same for the DataBind () method.

+9
source

All Articles