Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Add User Control to Table

Add User Control to Table

Scheduled Pinned Locked Moved ASP.NET
question
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rockman X7
    wrote on last edited by
    #1

    Helloo all! After I know, there is no way to add the user control except using @References ( if we need the property in that user control ), now I want to add that user control to the table by add the user control to the TableCell and add that TableCell to the TableRow and finally add to the Table... After I do it... There something wrong... All the user control just like in 1 row... So that I can see all the user control in 1 row... Anyone know what this happened? //this is my code to add the user control for ( int counter = 0; counter < 5; counter++ ) { TableRow tableRow = new TableRow(); TableCell tableCell = new TableCell(); ctrlTemp ctrl= ( ctrlTemp ) LoadControl( "~/Page/Controls/ctrlTemp.ascx" ); //load tableCell.Controls.Add( ctrl ); tableRow.Cells.Add( tableCell ); table.Rows.Add( tableRow ); } and the user control just add a label... Thanks for your attention! And please reply to me if you know something!

    M 1 Reply Last reply
    0
    • R Rockman X7

      Helloo all! After I know, there is no way to add the user control except using @References ( if we need the property in that user control ), now I want to add that user control to the table by add the user control to the TableCell and add that TableCell to the TableRow and finally add to the Table... After I do it... There something wrong... All the user control just like in 1 row... So that I can see all the user control in 1 row... Anyone know what this happened? //this is my code to add the user control for ( int counter = 0; counter < 5; counter++ ) { TableRow tableRow = new TableRow(); TableCell tableCell = new TableCell(); ctrlTemp ctrl= ( ctrlTemp ) LoadControl( "~/Page/Controls/ctrlTemp.ascx" ); //load tableCell.Controls.Add( ctrl ); tableRow.Cells.Add( tableCell ); table.Rows.Add( tableRow ); } and the user control just add a label... Thanks for your attention! And please reply to me if you know something!

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, You are adding each user control in a row of the table, if you want them to display in a row, you should add them in a single row. The sample code looks like this:

      TableRow tableRow = new TableRow();

      for ( int counter = 0; counter < 5; counter++ )
      {
      TableCell tableCell = new TableCell();
      ctrlTemp ctrl= ( ctrlTemp ) LoadControl( "~/Page/Controls/ctrlTemp.ascx" ); //load

      tableCell.Controls.Add( ctrl );
      tableRow.Cells.Add( tableCell );
      

      }

      table.Rows.Add( tableRow );

      R 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, You are adding each user control in a row of the table, if you want them to display in a row, you should add them in a single row. The sample code looks like this:

        TableRow tableRow = new TableRow();

        for ( int counter = 0; counter < 5; counter++ )
        {
        TableCell tableCell = new TableCell();
        ctrlTemp ctrl= ( ctrlTemp ) LoadControl( "~/Page/Controls/ctrlTemp.ascx" ); //load

        tableCell.Controls.Add( ctrl );
        tableRow.Cells.Add( tableCell );
        

        }

        table.Rows.Add( tableRow );

        R Offline
        R Offline
        Rockman X7
        wrote on last edited by
        #3

        Helo! Firstly, I wanna say thanks for your attention... But after I see your code and tested it... It just same... Only 1 user control that appear... Okay I will tell you what I want to do... I use user control to make many duplicated control to display the user detail... In the user control I already have many properties to set and get all the value... And I want to make many user controls to display the user detail in each row of table... As you can see I use loop to test the user control by add the user control to the cell and add that cell to row and finally add that row to table... And I have another question... How do I if I want to change type of a object? Like this? Control myControl = new Control(); myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ); ( ctrlTemp ) myControl.userName = "Robert"; // can I do this? Because I need the properties... But the "myControl" still type Control can I change to ctrlTemp so that I can access the userName property Thanks!

        M 1 Reply Last reply
        0
        • R Rockman X7

          Helo! Firstly, I wanna say thanks for your attention... But after I see your code and tested it... It just same... Only 1 user control that appear... Okay I will tell you what I want to do... I use user control to make many duplicated control to display the user detail... In the user control I already have many properties to set and get all the value... And I want to make many user controls to display the user detail in each row of table... As you can see I use loop to test the user control by add the user control to the cell and add that cell to row and finally add that row to table... And I have another question... How do I if I want to change type of a object? Like this? Control myControl = new Control(); myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ); ( ctrlTemp ) myControl.userName = "Robert"; // can I do this? Because I need the properties... But the "myControl" still type Control can I change to ctrlTemp so that I can access the userName property Thanks!

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Rockman X7 wrote:

          I use user control to make many duplicated control to display the user detail... In the user control I already have many properties to set and get all the value... And I want to make many user controls to display the user detail in each row of table... As you can see I use loop to test the user control by add the user control to the cell and add that cell to row and finally add that row to table

          If so, the sample code in your first post should be working just fine, and I misunderstood that you might have wanted to display all the users in a single row. If you only see one user control in the display, you might want to debug your sample by walking step by step, and have a look at the table object after the for loop. I think the problem might be somewhere else.

          Rockman X7 wrote:

          Control myControl = new Control(); myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ); ( ctrlTemp ) myControl.userName = "Robert"; // can I do this? Because I need the properties... But the "myControl" still type Control can I change to ctrlTemp so that I can access the userName property

          Basically, the LoadControl method returns the generic type Control, and you can cast it to the real type of the user control to access its methods/properties:

          ctrlTemp myControl = (ctrlTemp)LoadControl ( "~/Page/Controls/ctrlTemp.ascx" );
          or
          ctrlTemp myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ) as ctrlTemp;

          R 1 Reply Last reply
          0
          • M minhpc_bk

            Rockman X7 wrote:

            I use user control to make many duplicated control to display the user detail... In the user control I already have many properties to set and get all the value... And I want to make many user controls to display the user detail in each row of table... As you can see I use loop to test the user control by add the user control to the cell and add that cell to row and finally add that row to table

            If so, the sample code in your first post should be working just fine, and I misunderstood that you might have wanted to display all the users in a single row. If you only see one user control in the display, you might want to debug your sample by walking step by step, and have a look at the table object after the for loop. I think the problem might be somewhere else.

            Rockman X7 wrote:

            Control myControl = new Control(); myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ); ( ctrlTemp ) myControl.userName = "Robert"; // can I do this? Because I need the properties... But the "myControl" still type Control can I change to ctrlTemp so that I can access the userName property

            Basically, the LoadControl method returns the generic type Control, and you can cast it to the real type of the user control to access its methods/properties:

            ctrlTemp myControl = (ctrlTemp)LoadControl ( "~/Page/Controls/ctrlTemp.ascx" );
            or
            ctrlTemp myControl = LoadControl ( "~/Page/Controls/ctrlTemp.ascx" ) as ctrlTemp;

            R Offline
            R Offline
            Rockman X7
            wrote on last edited by
            #5

            Helo... All your suggestion still not working... Can you please create 1 example for me? I already desperate... In previous version it's working... But after I migrate to asp2.0 all become nightmare... Thanks!

            M 1 Reply Last reply
            0
            • R Rockman X7

              Helo... All your suggestion still not working... Can you please create 1 example for me? I already desperate... In previous version it's working... But after I migrate to asp2.0 all become nightmare... Thanks!

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              The web user control simply includes a label control and a public property UserName, the sample code and markup look like this:

              <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ctrlTemp.ascx.cs" Inherits="ctrlTemp" %>
              <asp:Label ID="Label1" runat="server"></asp:Label>

              public partial class ctrlTemp : System.Web.UI.UserControl
              {
              private string userName;

              public string UserName
              {
                  get { return userName; }
                  set { userName = value; }
              }
              
              protected void Page\_Load(object sender, EventArgs e)
              {
                  Label1.Text = userName;
              }
              

              }

              The testing web page goes like this:

              <%@ Page Language="C#"%>
              <%@ Reference Control="~/Page/Controls/ctrlTemp.ascx" %>

              <!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>
              <script runat="server">
              protected void Page_Load(object sender, EventArgs e)
              {
              for ( int counter = 0; counter < 5; counter++ )
              {
              TableRow tableRow = new TableRow();

                          TableCell tableCell = new TableCell();
                          ctrlTemp ctrl = LoadControl("~/Page/Controls/ctrlTemp.ascx") as ctrlTemp;
                          ctrl.UserName = "Robert";
                          tableCell.Controls.Add( ctrl );	
                          tableRow.Cells.Add( tableCell );
                                       
                          table.Rows.Add(tableRow);
                      }
                  }
              </script>
              

              </head>
              <body>
              <form id="form1" runat="server">
              <asp:Table ID="table" runat="server">
              </asp:Table>
              </form>
              </body>
              </html>

              R 1 Reply Last reply
              0
              • M minhpc_bk

                The web user control simply includes a label control and a public property UserName, the sample code and markup look like this:

                <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ctrlTemp.ascx.cs" Inherits="ctrlTemp" %>
                <asp:Label ID="Label1" runat="server"></asp:Label>

                public partial class ctrlTemp : System.Web.UI.UserControl
                {
                private string userName;

                public string UserName
                {
                    get { return userName; }
                    set { userName = value; }
                }
                
                protected void Page\_Load(object sender, EventArgs e)
                {
                    Label1.Text = userName;
                }
                

                }

                The testing web page goes like this:

                <%@ Page Language="C#"%>
                <%@ Reference Control="~/Page/Controls/ctrlTemp.ascx" %>

                <!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>
                <script runat="server">
                protected void Page_Load(object sender, EventArgs e)
                {
                for ( int counter = 0; counter < 5; counter++ )
                {
                TableRow tableRow = new TableRow();

                            TableCell tableCell = new TableCell();
                            ctrlTemp ctrl = LoadControl("~/Page/Controls/ctrlTemp.ascx") as ctrlTemp;
                            ctrl.UserName = "Robert";
                            tableCell.Controls.Add( ctrl );	
                            tableRow.Cells.Add( tableCell );
                                         
                            table.Rows.Add(tableRow);
                        }
                    }
                </script>
                

                </head>
                <body>
                <form id="form1" runat="server">
                <asp:Table ID="table" runat="server">
                </asp:Table>
                </form>
                </body>
                </html>

                R Offline
                R Offline
                Rockman X7
                wrote on last edited by
                #7

                Oh my GOD... I just realize my mistake! Do you know what? I already automatically set all my control to absoluted position... And it makes all my user control in 1 position... Hahaha! Oh my... But know i have another question... When I set relative position... I couldnt use the alignment option... When I set all my control in the left alignment... It become so funny... Do you have another suggestion to make my control/label to have position x n y except relative position < alignment problem > and absolute position < that occur in previous problem > in the style? Thanks very much! I aprreciate your help!

                M 1 Reply Last reply
                0
                • R Rockman X7

                  Oh my GOD... I just realize my mistake! Do you know what? I already automatically set all my control to absoluted position... And it makes all my user control in 1 position... Hahaha! Oh my... But know i have another question... When I set relative position... I couldnt use the alignment option... When I set all my control in the left alignment... It become so funny... Do you have another suggestion to make my control/label to have position x n y except relative position < alignment problem > and absolute position < that occur in previous problem > in the style? Thanks very much! I aprreciate your help!

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  The simple way to lay out the control is to use CSS, you can define a couple of CSS classes, then assign to the CssClass property of the controls.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups