Posted by : Anwar Hossain
Category : State Management in ASP C #

How to Pass Query String parameter From DataList

Dear viewers in this tutorial I will explain how to pass Query-string parameter one page to another page from ASP.Net DataList Control . I have used HyperLink Control to bind data from DataList Control and used it’s NavigateUrl Property to Pass Query String parameter to other page.

Query String parameter From Data-list

HTML (Data List Page)

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Passing Query String parameter From DataList</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       <h2 style=" color: #990000;">Passing Query String parameter From DataList</h2>

        <asp:DataList ID="lstStudentlist" runat="server" OnItemDataBound="lstStudentlist_ItemDataBound"

            Width="50%" CellPadding="0" ForeColor="#333333" BorderColor="#CC6600"

            BorderStyle="Solid" GridLines="Horizontal">

            <AlternatingItemStyle BackColor="White" />

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <ItemStyle BackColor="#EFF3FB" />

            <ItemTemplate>

                <table >

                    <tr>

                        <td valign="top" align="center" width="200">

                            <asp:HyperLink ID="lnkName" runat="server" Font-Bold="False" Target="_blank" ForeColor="#CC3300"></asp:HyperLink>

                        </td>

                        <td valign="top" align="center" width="200">

                            <asp:Label ID="lblAddress" runat="server" Text=""></asp:Label>

                        </td>

                        <td valign="top" align="center" width="200">

                            <asp:Label ID="lblAge" runat="server" Text=""></asp:Label>

                        </td>

                    </tr>

                </table>

            </ItemTemplate>

            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

        </asp:DataList>

    </div>

    </form>

</body>

</html>

 

CODE BEHIND (Data List Page)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class DatalistandQueryString : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

      

        LoadAllStudent();

    }

    private void LoadAllStudent()

    {

 

        List<Student> list = new List<Student>();

        list.Add(new Student("Iqbal", 39, "Noakhali"));

        list.Add(new Student("Roman Mozomdar", 48, "Chandpur"));

        list.Add(new Student("Palowan", 39, "Dhaka"));

        list.Add(new Student("Anwar", 31, "Dhaka"));

        list.Add(new Student("Samim", 39, "Dhaka"));

        list.Add(new Student("Asad", 48, "Chittagong"));

        list.Add(new Student("Nayed", 39, "Sylhet"));

        list.Add(new Student("Javed Hossain", 48, "Laxmipur"));

        lstStudentlist.DataSource = list;

        lstStudentlist.DataBind();

 

    }

    public class Student

    {

 

        public Student(string StudentName, int StudentAge, string Address)

        {

 

            _StudentName = StudentName;

            _StudentAge = StudentAge;

            _Address = Address;

 

 

        }

 

        private string _Address;

 

        public string Address

        {

            get { return _Address; }

            set { _Address = value; }

        }

 

 

        private int _StudentAge;

 

        public int StudentAge

        {

            get { return _StudentAge; }

            set { _StudentAge = value; }

        }

 

        private string _StudentName;

 

        public string StudentName

        {

            get { return _StudentName; }

            set { _StudentName = value; }

        }

 

    }

    protected void lstStudentlist_ItemDataBound(object sender, DataListItemEventArgs e)

    {

 

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            Student entry = e.Item.DataItem as Student;

 

            HyperLink lnkName = e.Item.FindControl("lnkName") as HyperLink;

            lnkName.Text = entry.StudentName.ToString();

            lnkName.NavigateUrl = "QueryStringFromDataList.aspx?Name=" + entry.StudentName;

 

            Label lblAddress = e.Item.FindControl("lblAddress") as Label;

            lblAddress.Text = entry.Address;

           

 

            Label lblAge = e.Item.FindControl("lblAge") as Label;

            lblAge.Text = entry.StudentAge.ToString();

        }

 

    }

}

Out Put

Query_String_URL_Parameter

HTML ( Dispaly Qurey String )

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Display Query String From DataList</title>

   

</head>

<body>

    <form id="form1" runat="server">

    <div>

     <h2 style=" color: #990000; width: 400px;" >Display Query String From DataList</h2>

    

     <b>Student Name : </b>   <asp:Label ID="lblStudentName" runat="server"

            ForeColor="#CC3300" style="font-weight: 700"></asp:Label>

      

    </div>

    </form>

</body>

</html>

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class QueryStringFromDataList : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (Request.QueryString["Name"].ToString() != null)

        {

 

            string Name = Request.QueryString["Name"];

            lblStudentName.Text = Name;

 

 

 

        }

    }

}

Out Put

Passing_Query_String_parameter_From_DataList



Realted Article Headline

How to Pass Query String parameter From DataList
Query String Example in ASP.NET C#
How to increase session time in ASP.Net C#
stroe datatable in session retrive display session vlaues in ASP.Net C #
How to clear Session state in ASP.NET C#
Session state in ASP.NET C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection