Using ASP. NET and jquery to pass multiple
Values from a gridview to another pageIn
One of our previous article pass multiple values
From a gridview to another page using ASP. NET, we had seen how to select
Gridview row and pass multiple values of the selected row to another page. We
Had made use of the 'datanavigateurlfields 'to set the names of the fields and
Construct the URL in the hyperlinkfield. In
This article, we will simplify the approach using jquery and pass values
The need of a hyperlink field. If you are new to jquery, I wowould strongly
Suggest you to check this: Using jquery with ASP. NET-a beginner's
Guideopen
Visual Studio 2008> File> New> website> choose 'asp. Net 3.5
Website 'from the templates> choose your language (C # Or VB)> enter
Location> OK. In the Solution Explorer, right click your project> New
Folder> rename the folder as 'scripts'. Right
Click the scripts folder> Add existing item> browse to the path where
You downloaded the jquery Library (jquery-1.2.6.js) and the intelliisense
Documentation (jquery-1.2.6-vsdoc.js)> select the files and click Add.
Now
Drag and Drop the jquery-1.2.6.js file from the Solution Explorer to
<Head> section of your page to create a reference as shown
Below: Now
Drag and Drop a sqldatasource control to the default. aspx page and use
Wizard to connect to the northwind database. Select the customerid, companyName,
Contactname, address and city from the customers table. The Wizard will also
Prompt you to save the connection string in the web. config file. Choose to do
So. The design code will look similar to the following: <asp: sqldatasource
Id = "sqldatasource1" runat = "server" connectionstring = "<% $
Connectionstrings: northwindconnectionstring %> "selectcommand =" select
[Customerid], [companyName], [contactname], [address], [City] From
[MERs] ">
</ASP: sqldatasource>
Entry will be added to the web. config file as shown below: <connectionstrings>
<Add
Name = "northwindconnectionstring"
Connectionstring = "Data Source = (local); initial catalog = northwind; Integrated
Security = true"
Providername = "system. Data. sqlclient"/> </connectionstrings> now
Add a gridview control to the page and using the Smart Tag, select
Datasource to be sqlperformance1 in the gridview tasks panel. Using the same
Panel, click on the Enable paging and enable sorting checkboxes. The source will
Look similar to the following. observe that the datakeynames is set
'Mermerid', that is the primary key of the MERs table <asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" datakeynames = "customerid" ceceid = "sqldatasource1" allowpaging = "true"
Allowsorting = "true"> <columns> <asp: boundfield datafield = "customerid" headertext = "customerid" readonly = "true" sortexpression = "customerid"/> <asp: boundfield datafield = "companyName" headertext = "companyName" sortexpression = "companyName"/> <asp: boundfield datafield = "contactname" headertext = "contactname" sortexpression = "contactname"/> <asp: boundfield datafield = "Address"
Headertext = "Address" sortexpression = "Address"/> <asp: boundfield datafield = "city"
Headertext = "city" sortexpression = "city"
/> </Columns>
</ASP: gridview> WE
Will now add another page in our project. In the Solution Explorer, right click
The project> Add new item> Web form> rename it
'Mermerdetails. aspx '. In the page_load of mermerdetails. aspx, add
Following code to retrieve the query string variables from the URL: C # protected void
Page_load (Object sender, eventargs e) {string cid = request. querystring ["CID"]; string cname = request. querystring ["cname"]; response. write ("customerid =" + CID + ":" + "companyName =
"+ Cname );
}
VB. NET protected sub
Page_load (byval sender as object, byval e
Eventargs) dim CID as string = request. querystring ("CID") dim cname as string = request. querystring ("cname ")
Response. Write ("customerid =" & cid & ":" & "companyName = "&
Cname)
End subtime
To see some jquery magic. Go back the default. aspx and add the following jquery
Code in the $ (Document ). ready (function () {$ ("TR "). click (function (event) {var ROW = jquery (this) var firstparam = row. children ("TD: eq (0 )"). text (); var secondparam = row. children ("TD: eq (1 )"). text (); var navurl = "http: // localhost: 7250/gridviewrowjquery/customerdetails. aspx? Cid ="
+ Firstparam + "& cname =" +
Secondparam; top. Location
= Navurl ;}); </SCRIPT>
Code handles the click event on a table row and extracts the first and second
Column value for that row. The URL is then formed using the two parameters and
Passed to the customerdetails. ASPX page. That's it. Run the Code and on click
Every row of the gridview, The customerid and customername are passed to
Next page. Simple! I
Hope this article was useful and I thank you for viewing it. The entire Source
Code of the article can be downloaded from here. If
You liked the article