Datalist & usercontrol ?????
-
Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.
-
Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.
You can create an event handler for the
ItemCreated
event of the DataList control, in the handler you can use theLoadControl
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 [^] -
You can create an event handler for the
ItemCreated
event of the DataList control, in the handler you can use theLoadControl
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 [^]can you explain clearly & demo code for me ( i'm a beginner ), i still don't understand what your mean :confused:
-
can you explain clearly & demo code for me ( i'm a beginner ), i still don't understand what your mean :confused:
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> -
Please tell me how adding some dynamic usercontrols into datalist ( I am a newcomer to ASP.NET ).Thanks a lot.
drag drop them Regards. Sudhakar D P
-
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>thanks a lot , 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>I save a lot of time with your help. Can you help me paging this datalist above ( + demo code ):) phucuong
-
I save a lot of time with your help. Can you help me paging this datalist above ( + demo code ):) phucuong
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).