Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a user control, and, I would like to use it in two different forms within my website, however, some "parameters", might be different including:

* The underlying SQL DataSource
* Behavior might be different (gut feeling)

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Envelope.ascx.cs" Inherits="Envelope" %>
<asp:FormView ID="MessageForm" runat="server" DataSourceID="SqlDataSource1"
            AllowPaging="True"
            OnItemDeleted = "ArticleEnvelope_Deleted"
            OnItemInserted = "ArticleEnvelope_Inserted"
            OnItemUpdated = "ArticleEnvelope_Updated">
        <HeaderTemplate>
        </HeaderTemplate>
        <EditItemTemplate>
            Timestamp:
            <asp:TextBox ID="TimestampTextBox" runat="server"
                Text='<%# Bind("Timestamp") %>' />
            <br />
            Subject:
            <asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
            <br />
            EntryText:
            <asp:TextBox ID="EntryTextTextBox" runat="server"
                Text='<%# Bind("EntryText") %>' />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
         <InsertItemTemplate>
             Timestamp:
             <asp:TextBox ID="TimestampTextBox" runat="server"
                 Text='<%# Bind("Timestamp") %>' />
             <br />
             Subject:
             <asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
             <br />
             EntryText:
             <asp:TextBox ID="EntryTextTextBox" runat="server"
                 Text='<%# Bind("EntryText") %>' />
             <br />
             <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                 CommandName="Insert" Text="Insert" />
             &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
                 CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        <ItemTemplate>
            Timestamp:
            <asp:Label ID="TimestampLabel" runat="server" Text='<%# Bind("Timestamp") %>' />
            <br />
            Subject:
            <asp:Label ID="SubjectLabel" runat="server" Text='<%# Bind("Subject") %>' />
            <br />
            EntryText:
            <asp:Label ID="EntryTextLabel" runat="server" Text='<%# Bind("EntryText") %>' />
            <br />
        </ItemTemplate>
 </asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:couch_dbConnectionString %>"
    SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"></asp:SqlDataSourc

e>


So, how is it possible to dynamically assign the SQL DataSource of a User Control, from different forms (aspx)?

:confused:
Posted

1 solution

Here is how you should be able to do it:

1. Remove the SqlDataSource from the User control, and, keep only the FormView. For example:

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcDetails.ascx.cs" Inherits="UcDetails" %>
<asp:FormView ID="FormView1" runat="server">
</asp:FormView>


2. Create a SqlDataSource parameter inside the User control's code behind and assign FormView's DataSourceID in the Page_Load(). For example:

C#
public SqlDataSource DataSource
{
    get;
    set;
}
protected void Page_Load(object sender, EventArgs e)
{
    FormView1.DataSourceID = DataSource.ID;
}



3. Put the SqlDataSource in the Aspx pages. For example:

XML
<body>
    <form id="form1" runat="server">
    <uc1:UcDetails ID="UcDetails1" runat="server" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
    </form>
</body

>

4. Set the DataSource of the FormView into the Page_Load() of aspx pages. For example:

C#
protected void Page_Load(object sender, EventArgs e)
{
        UcDetails1.DataSource = SqlDataSource1;
}


I think you should get the basic idea and can implement your functionality.
 
Share this answer
 
Comments
jon-80 3-Sep-10 2:57am    
Step 2 does not compile:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Step 4. It does not seems possible to call the user control from within the Page_Load of the .aspx Page (articles.aspx)

Source code at http://cid-b712073b3513eb8e.office.live.com/self.aspx/.Public/dumps/Jon%5E4s%20Couch.rar.
Al-Farooque Shubho 3-Sep-10 3:08am    
"It does not seems possible to call the user control from within the Page_Load of the .aspx Page (articles.aspx)"

Well, not correct. Calling user control from within the Page_Load() is too common and it has been done million times.

Try debugging your codes to investigate the NullRefernceException.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900