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
  1. Home
  2. Web Development
  3. ASP.NET
  4. Datalist & usercontrol ?????

Datalist & usercontrol ?????

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netquestion
8 Posts 3 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.
  • P Offline
    P Offline
    phucuong273
    wrote on last edited by
    #1

    Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.

    M Y 2 Replies Last reply
    0
    • P phucuong273

      Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.

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

      You can create an event handler for the ItemCreated event of the DataList control, in the handler you can use the LoadControl method to dynamically load your user control, then add it to a container placed the item template. You can read more about this in MSDN: How to: Create Instances of ASP.NET User Controls Programmatically [^] DataList Web Server Control [^]

      P 1 Reply Last reply
      0
      • M minhpc_bk

        You can create an event handler for the ItemCreated event of the DataList control, in the handler you can use the LoadControl method to dynamically load your user control, then add it to a container placed the item template. You can read more about this in MSDN: How to: Create Instances of ASP.NET User Controls Programmatically [^] DataList Web Server Control [^]

        P Offline
        P Offline
        phucuong273
        wrote on last edited by
        #3

        can you explain clearly & demo code for me ( i'm a beginner ), i still don't understand what your mean :confused:

        M 1 Reply Last reply
        0
        • P phucuong273

          can you explain clearly & demo code for me ( i'm a beginner ), i still don't understand what your mean :confused:

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

          Below is a quick example to demonstrate what I am getting at:

          <%@ Page Language="C#" %>

          <script runat="server">

          protected void Page\_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  DataList1.DataSource = GetSimpleDataSource();
                  DataList1.DataBind();
              }
          }
                   
          private ArrayList GetSimpleDataSource()
          {
              ArrayList list = new ArrayList();
              list.Add(new object());
              list.Add(new object());
              list.Add(new object());
          
              return list;
          }
                     
          protected void DataList1\_ItemCreated(object sender, DataListItemEventArgs e)
          {
              if (e.Item.ItemType == ListItemType.Item || 
                          e.Item.ItemType == ListItemType.AlternatingItem)
              {
                  Control userControl = LoadControl("SimpleControl.ascx");
                  PlaceHolder container = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
                  container.Controls.Add(userControl);
              }
          }
          

          </script>

          <html>
          <head runat="server">
          <title>Untitled Page</title>
          </head>
          <body>
          <form id="form1" runat="server">
          <div>
          <asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated">
          <ItemTemplate>
          <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
          <br />
          </ItemTemplate>
          </asp:DataList>
          </div>
          </form>
          </body>
          </html>

          ------------SimpleControl.ascx-----------
          <%@ Control Language="C#" ClassName="SimpleControl" %>
          <h3>This is a simple user control</h3>

          P 2 Replies Last reply
          0
          • P phucuong273

            Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.

            Y Offline
            Y Offline
            yesimlucky
            wrote on last edited by
            #5

            drag drop them Regards. Sudhakar D P

            1 Reply Last reply
            0
            • M minhpc_bk

              Below is a quick example to demonstrate what I am getting at:

              <%@ Page Language="C#" %>

              <script runat="server">

              protected void Page\_Load(object sender, EventArgs e)
              {
                  if (!IsPostBack)
                  {
                      DataList1.DataSource = GetSimpleDataSource();
                      DataList1.DataBind();
                  }
              }
                       
              private ArrayList GetSimpleDataSource()
              {
                  ArrayList list = new ArrayList();
                  list.Add(new object());
                  list.Add(new object());
                  list.Add(new object());
              
                  return list;
              }
                         
              protected void DataList1\_ItemCreated(object sender, DataListItemEventArgs e)
              {
                  if (e.Item.ItemType == ListItemType.Item || 
                              e.Item.ItemType == ListItemType.AlternatingItem)
                  {
                      Control userControl = LoadControl("SimpleControl.ascx");
                      PlaceHolder container = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
                      container.Controls.Add(userControl);
                  }
              }
              

              </script>

              <html>
              <head runat="server">
              <title>Untitled Page</title>
              </head>
              <body>
              <form id="form1" runat="server">
              <div>
              <asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated">
              <ItemTemplate>
              <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
              <br />
              </ItemTemplate>
              </asp:DataList>
              </div>
              </form>
              </body>
              </html>

              ------------SimpleControl.ascx-----------
              <%@ Control Language="C#" ClassName="SimpleControl" %>
              <h3>This is a simple user control</h3>

              P Offline
              P Offline
              phucuong273
              wrote on last edited by
              #6

              thanks a lot , minhpc_bk ! :)

              1 Reply Last reply
              0
              • M minhpc_bk

                Below is a quick example to demonstrate what I am getting at:

                <%@ Page Language="C#" %>

                <script runat="server">

                protected void Page\_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        DataList1.DataSource = GetSimpleDataSource();
                        DataList1.DataBind();
                    }
                }
                         
                private ArrayList GetSimpleDataSource()
                {
                    ArrayList list = new ArrayList();
                    list.Add(new object());
                    list.Add(new object());
                    list.Add(new object());
                
                    return list;
                }
                           
                protected void DataList1\_ItemCreated(object sender, DataListItemEventArgs e)
                {
                    if (e.Item.ItemType == ListItemType.Item || 
                                e.Item.ItemType == ListItemType.AlternatingItem)
                    {
                        Control userControl = LoadControl("SimpleControl.ascx");
                        PlaceHolder container = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
                        container.Controls.Add(userControl);
                    }
                }
                

                </script>

                <html>
                <head runat="server">
                <title>Untitled Page</title>
                </head>
                <body>
                <form id="form1" runat="server">
                <div>
                <asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated">
                <ItemTemplate>
                <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
                <br />
                </ItemTemplate>
                </asp:DataList>
                </div>
                </form>
                </body>
                </html>

                ------------SimpleControl.ascx-----------
                <%@ Control Language="C#" ClassName="SimpleControl" %>
                <h3>This is a simple user control</h3>

                P Offline
                P Offline
                phucuong273
                wrote on last edited by
                #7

                I save a lot of time with your help. Can you help me paging this datalist above ( + demo code ):) phucuong

                M 1 Reply Last reply
                0
                • P phucuong273

                  I save a lot of time with your help. Can you help me paging this datalist above ( + demo code ):) phucuong

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

                  By design, the DataList control does not support the paging functionality, if you want that, you need to provide your sample code to implement the custom paging. There are a couple of options which you can use, and you can see the sample code from here[^]. If you want to use the built-in paging functionality, you may consider using another control like DataGrid, or GridView(in the ASP.NET 2).

                  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