connectseekers.com

Monday, 20 February 2017

Bind HyperLink Control To DataList In Asp.net Using C#

2/20/2017 - By Pranav Singh 0

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.

<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 text



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. 



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top