How to "duplicate" (or copy) the existing control that cointains other controls?

Last post 05-16-2008 5:39 AM by XIII. 16 replies.

Sort Posts:

  • How to "duplicate" (or copy) the existing control that cointains other controls?

    05-12-2008, 11:10 AM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Let's say I have some kind of container control (HTMLGeneric control or Placeholder or Panel, whatever)

    It contains various controls (databinded dropdownlist, buttons, labels...) that have their events that are processed in code. So controls are bounded and processed in other ways in run-time.

    They also have their IDs, so I obviously can't just copy and paste them at some other position on the page. 

    My question is: how do I make a exact "copy" of container control on a different position on the same page in run time? Absolutely nothing would be changed, I just want that control to be displayed twice on 2 different positions on the page.


    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-12-2008, 11:16 AM
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 10,083
    • Moderator
      TrustedFriends-MVPs

    You can create a user control from it and place multiple instances of that in PlaceHolder controls on your page at runtime.

    Grz, Kris.

    Read my blog | Members are volunteers so please keep out the (it's urgent | asap | reply me directly) stuff. Nobody's interested in that.
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-12-2008, 11:27 AM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Will it work, cause the dropdown list I mentioned is dependable on Gridview? (I mean, it's not bound directly, but the same code depends on both the Gridview outside the container and the dropdownlist)

    I'll try anyway, but I'd prefer if I can keep it all in one page if possible... 

    BTW, is it possible to create user control in the same aspx page? Or do I have to use new ascx page.

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-12-2008, 11:34 AM
    Answer
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 10,083
    • Moderator
      TrustedFriends-MVPs

    imbrod:
    Will it work, cause the dropdown list I mentioned is dependable on Gridview

    Could you please specify such information in the original question since that changes the answers you're after.

    You could provide a public property to which you can pass in information from the page to the user control to have your dropdownlist dependent on the gridview to have both/multiple user controls in sync.

    imbrod:
    is it possible to create user control in the same aspx page

    No, the meaning of a user control is to have an entity that you can reuse easily in code or by dragging it on your page.

    Grz, Kris.

    Read my blog | Members are volunteers so please keep out the (it's urgent | asap | reply me directly) stuff. Nobody's interested in that.
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-12-2008, 12:38 PM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Thanks, I get it.

    Yes 

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-13-2008, 9:15 AM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Sorry, seems like I don't get it after all... Embarrassed

    I have a gridview and a set of buttons

      

    <asp:Button ID="lPagerFirs" runat="server" EnableViewState="false" 
                CausesValidation="False" Text="<<" onclick="lPagerFirs_Click"></asp:Button>
            <asp:Button ID="lPagerPrev" runat="server" EnableViewState="false" 
                Text="Prev" onclick="lPagerPrev_Click"></asp:Button>
            <asp:Button ID="lPagerNext" runat="server" EnableViewState="false" 
                Text="Next" onclick="lPagerNext_Click"></asp:Button>
            <asp:Button ID="lPagerLast" runat="server" EnableViewState="false" 
                Text=">>" CausesValidation="False" onclick="lPagerLast_Click"></asp:Button>
     

    and the code is

     

        Sub lPagerFirs_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            GridView1.PageIndex = 0
        End Sub
    
        Sub lPagerPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If GridView1.PageIndex > 0 Then
                GridView1.PageIndex -= 1
            End If
        End Sub
    
        Sub lPagerNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If GridView1.PageIndex < GridView1.PageCount - 1 Then
                GridView1.PageIndex += 1
            End If
        End Sub
    
        Sub lPagerLast_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            GridView1.PageIndex = GridView1.PageCount - 1
        End Sub
      

      Now:

    - if I put subs in container page, how do I check for events? ( onclick="???.lPagerLast_Click" )

    - if I put subs in user control, how do I reference GridView1?
     

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-13-2008, 9:42 AM
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 10,083
    • Moderator
      TrustedFriends-MVPs

    So your gridview's on your page and you want to encapsulate your navigation buttons in a user control?

    Grz, Kris. 

    Read my blog | Members are volunteers so please keep out the (it's urgent | asap | reply me directly) stuff. Nobody's interested in that.
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-13-2008, 3:06 PM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Exactly.

    Navigation buttons and one dropdownlist (that I excluded from this example to keep it simpler).

    The dropdownlist is infact the pager of GridView.

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-14-2008, 1:50 PM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     How to encapsulate navigation buttons in a user control from a container page  or  reference gridview that's on container page from the user control?

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-14-2008, 5:06 PM
    Answer
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 10,083
    • Moderator
      TrustedFriends-MVPs

    Hi,

    sorry for the delay but I didn't have a lot of time in the evening to experiment. I played around with it myself a bit and came up with 2 possible ways which can probably be made easier (it just took me 3,5 hours by train to get to home after a stressfull day at work so please be gentile).

    page

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewTest1259967.aspx.cs" Inherits="TestEnvironment.Forums.GridViewTest1259967" %>
    
    <%@ Register src="GridViewTestUC1259967.ascx" tagname="GridViewTestUC1259967" tagprefix="uc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ContactName" HeaderText="ContactName" 
                        SortExpression="ContactName" />
                    <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" 
                        SortExpression="ContactTitle" />
                    <asp:BoundField DataField="Country" HeaderText="Country" 
                        SortExpression="Country" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>" 
                SelectCommand="SELECT [ContactName], [ContactTitle], [Country] FROM [Customers]">
            </asp:SqlDataSource>
            <uc1:GridViewTestUC1259967 ID="GridViewTestUC12599671" G="GridView1" runat="server" />
        
        </div>
        </form>
    </body>
    </html>
     

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    namespace TestEnvironment.Forums
    {
        public partial class GridViewTest1259967 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                GridViewTestUC12599671.Grid = GridView1;
            }
        }
    }
     user control

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridViewTestUC1259967.ascx.cs" Inherits="TestEnvironment.Forums.GridViewTestUC1259967" %>
    <asp:LinkButton runat="server" ID="LB1" onclick="LB1_Click" Text="<<" />
     <asp:LinkButton runat="server" ID="LB2" onclick="LB2_Click" Text=">>">
     

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    namespace TestEnvironment.Forums
    {
        public partial class GridViewTestUC1259967 : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void LB1_Click(object sender, EventArgs e)
            {
                if (Grid.PageIndex > 0)
                {
                    Grid.PageIndex -= 1;
                }
            }
    
            protected void LB2_Click(object sender, EventArgs e)
            {
                //if (Grid.PageIndex < Grid.PageCount - 1)
                //{
                //    Grid.PageIndex += 1;
                //}
    
                if (GetGrid(G).PageIndex < GetGrid(G).PageCount - 1)
                {
                    GetGrid(G).PageIndex += 1;
                }
            }
    
            private GridView GetGrid(string grid)
            {
                return (GridView)Parent.FindControl(grid);
            }
    
            public string G { get; set; }
    
    
            public GridView Grid { get; set; }
        }
    }

    If you want to pass in the Id of the grid in markup you should use property G and find it on the page. The other method uses code behind and you directly inject in the Page_Load of the page the grid to the user control. So if you have multiple user controls you should pass in for both controls the grid.

     Grz, Kris.

    Read my blog | Members are volunteers so please keep out the (it's urgent | asap | reply me directly) stuff. Nobody's interested in that.
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-14-2008, 6:18 PM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

     Wow, thanks, XIII !!!

    I appreciate it a lot! I'm sorry if you got sense I was rude or something; I wasn't really, I was just worry that my post will be "buried" at the bottom of the forum so nobody would respond me to something I believed was a simple solution - so I replied twice.

    I'll check it out; thanks again!

    Yes  Yes  Yes 

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-15-2008, 7:15 AM
    • Loading...
    • imbrod
    • Joined on 10-27-2004, 8:43 AM
    • Posts 210

    Sorry for my delay now, XIII!

    First what I did is replace your connectionstring with mine that points to Northwind SQL db.

    Second, there was a slight error, just wanted to let you know to update it in original code: Add / at the end of tag of LB2 in u.c.

    Rest of the code I left intact, but I get this error:
    Could not load type 'TestEnvironment.Forums.GridViewTest1259967'

    Then I removed TestEnvironment.Forums. part and I got:
    Could not load type 'GridViewTest1259967'.

    Then I removed the Inherits=... part completely and I the same error happened with u.c. Page directive, so I removed it from the u.c.

    Then I got this, in the line 2 of u.c. :
    CS1061: 'ASP.gridviewtestuc1259967_ascx' does not contain a definition for 'LB1_Click' and no extension method 'LB1_Click' accepting a first argument of type 'ASP.gridviewtestuc1259967_ascx' could be found (are you missing a using directive or an assembly reference?)

    So I guessed I did some damage by removing "Inherits" part from both pages. But as I said, it didn't work with it.

    What am I doing wrong?

    (Please cope with me, remember I'm a old school ASP-er and a newbie in .NET Wink )

    I love spaghetti (both code and food)!
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-15-2008, 7:21 AM
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 10,083
    • Moderator
      TrustedFriends-MVPs

    imbrod:
    Could not load type 'TestEnvironment.Forums.GridViewTest1259967'
     

    That's the name of my test webform/class so you should change those to reflect the classname of your own. Sorry forgot to mention that.

    GridViewTest... is the name of the webform while GridViewTestUC... is the name of the user control.

    I ment only to show the concept, not copy and paste ready code. If you repeat the steps in your application you can obtain a working application. Perhaps it's handy, I like to do it, to create a small test application to isolate the problem and to solve it before copying over the concept to the "real deal project". 

    Grz, Kris. 

    Read my blog | Members are volunteers so please keep out the (it's urgent | asap | reply me directly) stuff. Nobody's interested in that.
  • Re: How to "duplicate" (or copy) the existing control that cointains other controls?

    05-15-2008, 2:33 PM