This article will show you how you can bind hyperlink
control to datalist in asp.net using c#. In this I have shown how you can bind
the hyperlink control with text and page link with parameter using c# in
datalist.
So for this article first we will create a new asp.net application and add the datalist control in it. After adding datalist we will bind the values and page.
So for this article first we will create a new asp.net application and add the datalist control in it. After adding datalist we will bind the values and page.
<asp:DataList ID="DataList1"
runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Both" RepeatColumns="3" RepeatDirection="Horizontal">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<ItemStyle BackColor="White" ForeColor="#330099" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink1"
runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "PageName.aspx?Id="+Eval("Id") %>'></asp:HyperLink>
</ItemTemplate>
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
</asp:DataList>
|
Now in above code I have bind text and link to datalist with parameter in itemtemplate. Now check the item template to bind the values.
For binding navigation URL. Now check the code to bind the datalist.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace Shoping_Cart
{
public partial class DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> _student = new List<Student>();
_student = StudentList();
DataList1.DataSource = _student;
DataList1.DataBind();
}
private List<Student> StudentList()
{
List<Student> _student = new List<Student>();
_student.Add(new Student { Id = 1, Address = "Address1", Marks = "80%", Name = "student1" });
_student.Add(new Student { Id = 2, Address = "Address2", Marks = "81%", Name = "student2" });
_student.Add(new Student { Id = 3, Address = "Address3", Marks = "82%", Name = "student3" });
_student.Add(new Student { Id = 4, Address = "Address4", Marks = "83%", Name = "student4" });
_student.Add(new Student { Id = 5, Address = "Address5", Marks = "84%", Name = "student5" });
_student.Add(new Student { Id = 6, Address = "Address6", Marks = "85%", Name = "student6" });
return _student;
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Marks { get; set; }
}
}
|
Here in above code I have taken a static list for binding to datalist. You can pull the data from database and bind it to datalist.
Now we have done run the application to check the output.
0 comments:
Please let me know your view