Tuesday, April 14, 2009

LinkButton in GridView and QueryString in ASP.NET to pass data


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





Related Posts:
Creating winforms AutoComplete TextBox using C# in Windows application
Disable copy paste cut and right click in textbox on aspx page using javascript
Ajax autocomplete extender textbox in EditItemTemaplate of GridView
ASP.NET-Exporting paging enabled GridView to pdf using iTextSharp and C#
amiT jaiN
www.tips-fb.com
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks vote it on WebDevVote.com add to del.icio.us saved by 0 users
Subscribe to Feeds

12 comments:

Avinash said...

nice article... nice image... easy to understand what you want to explain...

thanks...


amiT
amiT jaiN said...

@Avinash: thanks for the feedback :)


Anonymous said...

a small demo of what exactly is done is nice..
thnks,
sheetal


RRave said...

Dear Friends,

I hope you are doing well. I have launched a web site www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.


Please forward this email to all of your friends who are related IT. Send to us your feed about site also.


Thank you
RRaveen
Founder CodeGain.com


Anonymous said...

Hey this nice blog which belongs to .Net which i ever seen .
Thanks for giving such type os articles


Anonymous said...

Interesting but the most important is missing : the cs behind that. Is there a way to retrieve this as the download link does not work anymore ?


amiT
amiT jaiN said...

@above:

I am not using any code behind as such in this example

There is only one line of code to bind the gridView in click event of Search Button

protected void btnSearch_Click(object sender, EventArgs e)
{
GridView1.DataBind();
}

btw i've fix the download link, you can download the source code :)


Anonymous said...

Hei I'm Akalanka in Sri Lanka...
we are working on a project in Blood Bank..building a system....
Your article is so much helpfull..
that's realy great..
keep going..

Good luck..


amiT
amiT jaiN said...

@Akalanka:

Thanks a lot for the appreciation, keep visiting this site :)


Anonymous said...

I used the code ... thanx

I want you to help me in other problem
please see the: http://96.0.103.165/
Why that ?

1. Since the Web.config has true connection and correct user id and password
2. The DB is uploaded in the server and some data is written in.

Thanx Amit Jain, I wiil appreciate you ...


Anonymous said...

Can i get your email ?


amiT
amiT jaiN said...

@Above: the error suggests that u r having problem with ur sql server address, please contact your web host to get the address of your sql srver


About Me

My Photo
amiT jaiN
Hi, I am amiT jaiN Software engineer working on C#.NET and ASP.NET technologies
View my complete profile

Comments

.NET Resources

Find More Articles


Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN