LinkButton in GridView and QueryString in ASP.NET

2:24:00 am 0 Comments

LinkButton in GridView and QueryString in ASP.NET

In this example i'm going to show how to work with LinkButton in GridView and pass / transfer variable or data to other page and populate another gridview based on it using QueryString and QueryStringParameters.

For this example , there is no need of any code behind.

I've put a searchbox at the top of page and based on search text entered in textbox gridview is populated, In the gridview i've put a Link Button in ItemTemplate column.


LinkButton field is hyperlink which points to Details.aspx and sends RecordID as QueryString to Details.aspx page where another gridview is populated to show Details of selected record by retrieving the QueryString variable using QueryStringParameters.

The html source of default.aspx page is like this

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" TabIndex="1"
Text="Search" OnClick="btnSearch_Click" />
<br />

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server"
Text='<%#Eval("Name") %>'
PostBackUrl='<%#"~/Details.aspx?ID="+Eval("ID") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="City" HeaderText="City"
SortExpression="City" />

</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [City], [Country]
FROM [Location]
WHERE ([Name] LIKE '%' + @Name + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch"
Name="Name"
   PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

And html source of Details.aspx is like this

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Details.aspx.cs" Inherits="Details" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="City" HeaderText="City"
SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [City], [Country]
FROM [Location] WHERE ([ID] = @ID)">
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="ID"
Type="Decimal" />
</SelectParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>


Download the sample code attached

0 comments: