LinkButton in GridView and QueryString in ASP.NET

This example explains how to use LinkButton in GridView and QueryString in ASP.NET to pass or transfer variable data to other page using QueryString and QueryStringParameters.

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 in GridView and QueryString in ASP.NET

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.

HTML SOURCE OF DEFAULT.ASPX


<asp:TextBox ID="txtSearch" runat="server"/>
<asp:Button ID="btnSearch" runat="server"
            Text="Search" OnClick="btnSearch_Click"/>
 
<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"/>
</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>

HTML SOURCE OF DETAILS.ASPX
   1:  <asp:GridView ID="GridView1" runat="server"
   2:                AutoGenerateColumns="False"
   3:                DataSourceID="SqlDataSource1">
   4:  <Columns>
   5:  <asp:BoundField DataField="ID" HeaderText="ID"/>
   6:  <asp:BoundField DataField="Name" HeaderText="Name"/>
   7:  <asp:BoundField DataField="City" HeaderText="City"/>
   8:  <asp:BoundField DataField="Country" HeaderText="Country"/>
   9:  </Columns>
  10:  </asp:GridView>
  11:   
  12:  <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  13:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  14:  SelectCommand="SELECT [ID], [Name], [City], [Country]
  15:                 FROM [Location] WHERE ([ID] = @ID)">
  16:   
  17:  <SelectParameters>
  18:  <asp:QueryStringParameter Name="ID" QueryStringField="ID" 
  19:                            Type="Decimal"/>
  20:  </SelectParameters>
  21:  </asp:SqlDataSource>



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#
If you like this post than join us or share

41 comments:

Avinash Tumulu said...

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

thanks...


Unknown said...

@Avinash: thanks for the feedback :)


Anonymous said...

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


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 ?


Unknown 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..


Unknown 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 ?


Unknown 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


Anonymous said...

LinkButton in GridView and QueryString in ASP.NET to pass data using c# code behind file


Anonymous said...

I got an error "Input string was not in a correct format"
Any suggestion?
Thank you.
Tet


Anonymous said...

thank u nice demo, nice artical.

Ram
Malaysia.


sikat ang pinoy said...

Hey webmaster, good day. Totally Great work. You have gained a new fan. Please continue this awesome work and I look forward to see more of your excellent posts. Take care.


RoJa said...

Sir,
I just want to clear some doubts.
I've one form which contains a grid.
In grid I've placed a linkbutton
We are using master page. And I want to go to another page in the click of this link button.
The forms are opening in frame.
I've tried using javascript also. Bt its not working. I dont want to open pop up. Bt want to redirect to the next page in the same frame. Please give me suggestion to my email id jishajohnsonm@gmail.com


diya076 said...

thanx... it helped me...


Anonymous said...

can i get the code to download a file from server when the link button inside the gridview is clicked.


Anonymous said...

Could not find control 'txtSearch' in ControlParameter 'Name'. Igot this particuar error
when doing this


Anonymous said...

Hi Amit,

I would like to put search functionality in my website. This functionality will help to search data from database and populate into gridview.

If i want to search all my website i mean including static pages. How can i implement that functionality . Can you help me on this ?
Once again thanks for your help.



Thanks
Raj


Anonymous said...

Thanks my problem soleved with your article


Anonymous said...

hey......... thanx......... it helped me a lot.. :)


gaur said...

this was for passing single value if i need to pass multiple values how to do that,as in my case link button is in every roww,i want to pass values of two columns of tht row of which link button is clicked


Anonymous said...

saassas


Gyana said...

how it is work without any event fire

without any click event, submit event, it works very well just pressing Enter........


Hariharan Murugan said...

Im trying your code in vb.net . I got a error while executing it.
Error:
Conversion from string "~/PurchaseDetails.aspx?ID=" to type 'Double' is not valid.


Anonymous said...

Hi,
Do you have sample code on How to view the record in Crystal Report when click on gridview Linkbutton?

Regards
Aleesya


Anonymous said...

Hi amit,
DO you have any sample code on how How to view the current record in Crystal Report when click on gridview Linkbutton.
Regards
Aleesya


Anonymous said...

gr8 job dude buckets of thanks


@Sheffy said...

thanx a lot...gr8 job...


Anonymous said...

Really helped me. Thank you very much!


kavidhas said...

thanks a lot man,.....


Anonymous said...

It was really very helpful in Understanding with illustration and really makes it very interesting.


Elijah said...

Thanks I got the demo working the way i wanted but on my actual webpage i keep getting this error:
System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:LinkButton' is of type 'System.Web.UI.WebControls.LinkButton'.


any ideas?


చక్రవర్తి said...

Thanks dude, it help me too. Btw what kind of tools you use to record your mouse moments and place them as a small clip, such as the one in the above article.

Please write a post about the same or you can let me know in person at VARTHY [at] GMAIL


Unknown said...

@VARTHY : i use cropper for capturing mouse movements


Unknown said...

i have a table named postjob.i have a search page with a textbox and a button.i want when user type in textbox and click search then it should match textbox value with a coloumn named industry in my table..can you help me out..


Unknown said...

@MEHAK SHARMA: design your page as mentioned above in this post and change your SELECT statement as

SelectCommand="SELECT * FROM [postjob]
WHERE ([industry] LIKE '%' + @industry + '%')">

<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="industry" PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>


Pi-code said...

Thank you very much, your code really helped me bro. thanks


Anonymous said...

Awesome !!!!!!
Thanks a lot....


Anonymous said...

it's working now.thanks.


Find More Articles