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#

41 comments:

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

    thanks...

    ReplyDelete
  2. @Avinash: thanks for the feedback :)

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

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

    ReplyDelete
  5. 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 ?

    ReplyDelete
  6. @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 :)

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

    ReplyDelete
  8. @Akalanka:

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

    ReplyDelete
  9. 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 ...

    ReplyDelete
  10. Can i get your email ?

    ReplyDelete
  11. @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

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

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

    ReplyDelete
  14. thank u nice demo, nice artical.

    Ram
    Malaysia.

    ReplyDelete
  15. 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.

    ReplyDelete
  16. 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

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

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

    ReplyDelete
  19. 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

    ReplyDelete
  20. Thanks my problem soleved with your article

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

    ReplyDelete
  22. 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

    ReplyDelete
  23. how it is work without any event fire

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

    ReplyDelete
  24. 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.

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

    Regards
    Aleesya

    ReplyDelete
  26. 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

    ReplyDelete
  27. gr8 job dude buckets of thanks

    ReplyDelete
  28. Really helped me. Thank you very much!

    ReplyDelete
  29. thanks a lot man,.....

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

    ReplyDelete
  31. 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?

    ReplyDelete
  32. 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

    ReplyDelete
  33. @VARTHY : i use cropper for capturing mouse movements

    ReplyDelete
  34. 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..

    ReplyDelete
  35. @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>

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

    ReplyDelete
  37. Awesome !!!!!!
    Thanks a lot....

    ReplyDelete
  38. it's working now.thanks.

    ReplyDelete