Pass Multiple Values from a GridView to Another Page using ASP.NET

Posted by: Suprotim Agarwal , on 4/28/2008, in Category ASP.NET
Views: 190783
Abstract: A common requirement in our projects is to select a GridView row and pass multiple values of the selected row to another page. I recently got a request from a few readers who wanted an article on this. In this article, we will explore how simple it is to achieve this requirement.
Pass Multiple Values from a GridView to Another Page  using ASP.NET
 
A common requirement in our projects is to select a GridView row and pass multiple values of the selected row to another page. I recently got a request from a few readers who wanted an article on this. In this article, we will explore how simple it is to achieve this requirement.
I assume you have some basic understanding of the GridView and how to bind it to a Data Source control. The Hyperlink control added to the GridView makes it quiet easy to select a row and send single/multiple values to a different page through the URL. Let us see how:
Step 1: Create a new ASP.NET website. Drag and drop a SqlDataSource Control to the page and use the 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 [Customers]">
        </asp:SqlDataSource>
An 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>
Step 2: Now add a GridView control to the page and using the smart tag, select the DataSource to be SqlDataSource1 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 to ‘CustomerId’, that is the primary key of the Customers table.
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
            DataSourceID="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>
Step 3: We will now add another page in our project. In the Solution Explorer, right click the project > Add New Item > Web Form > Rename it to ‘CustomerDetails.aspx’.
Step 4: Go back to Default.aspx and add two hyperlink fields. We will see how to pass a single value as well as multiple values using the two hyperlink fields.
Single Value:
Add the following hyperlink control after the <Columns> tag in the GridView as shown below:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="CustomerID" DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}" Text="Pass Single Value" />
Multiple Values:
Just below the first hyperlink field, add another hyperlink field as shown below:
<asp:HyperLinkField DataNavigateUrlFields="CustomerID, CompanyName, ContactName, Address, City" DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}&CName={1}&ContactName={2}&Addr={3}&City={4}" Text="Pass Multiple Values" />
 
In the source code shown above, we are using the hyperlink field and setting some properties that will make it extremely easy to pass values to a different page. The 'DataNavigateUrlFields' sets the names of the fields, that is to be used to construct the URL  in the HyperLinkField. In the first hyperlink, since we are passing only a single value, the DataNavigateUrlFields contains only CustomerID. However in the second hyperlink, since there are multiple values to be passed, DataNavigateUrlFields contains all the names of the fields that are to be passed as query string to CustomerDetails.aspx
Similarly, the 'DataNavigateUrlFormatString' sets the string that specifies the format in which the URL is to be created. The 'Text' property represents the text that will be displayed to the user. The entire source code will look similar to the following:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City] FROM [Customers]">
        </asp:SqlDataSource>
   
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
            DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
            <Columns>           
                 <asp:HyperLinkField DataNavigateUrlFields="CustomerID"
                    DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}"
                     Text="Pass Single Value" />
                   <asp:HyperLinkField DataNavigateUrlFields="CustomerID, CompanyName, ContactName, Address, City"
                    DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}&CName={1}&ContactName={2}&Addr={3}&City={4}"
                      Text="Pass Multiple Values" />
                <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>
    </form>
</body>
Step 5: The last step is to retrieve the query string variables from the URL in the CustomerDetails.aspx page. Add the following code for that:
C#
    protected void Page_Load(object sender, EventArgs e)
    {
        string cid = Request.QueryString["CID"];
        string cname = Request.QueryString["CName"];
        string contactName = Request.QueryString["ContactName"];
        string address = Request.QueryString["Addr"];
        string city = Request.QueryString["City"];
    }
VB.NET
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim cid As String = Request.QueryString("CID")
            Dim cname As String = Request.QueryString("CName")
            Dim contactName As String = Request.QueryString("ContactName")
            Dim address As String = Request.QueryString("Addr")
            Dim city As String = Request.QueryString("City")
      End Sub
Set a breakpoint at the Page_Load method of the CustomerDetails.aspx. Run the application and click on either the ‘Pass Single Value’ or ‘Pass Multiple Values’ hyperlink to pass values to the CustomerDetails.aspx page. Using the breakpoint, observe the values of the selected row being passed to the CustomerDetails page.
I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.
 
Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Abhay Tripathi on Tuesday, April 29, 2008 6:31 AM
really good one!!
Comment posted by nicole on Wednesday, April 30, 2008 1:29 AM
Thx ya...ur code is really helpful for me ^.^
Comment posted by Nishant on Wednesday, April 30, 2008 4:22 PM
really a good example dealing with multiple values at a time for communication b/w diff pages.
In fact I would also like to learn abt posting the multiple values from the one page to other page via different method other than reading plain querystring.I mean is it possible to encode the values and send the same.
Comment posted by g.pavan kumar on Saturday, May 3, 2008 6:59 AM
i had attended an interview  , they asked me one questions tat is what r the errors u can found if u set try and catch blocks while making database connections in ado.net, can any one help me regard of tis query
Comment posted by Suprotim Agarwal on Saturday, May 3, 2008 7:32 AM
Nishant: Check this link http://www.codeproject.com/KB/aspnet/TamperProofQueryString.aspx

Pavan: You would look out for SqlExceptions apart from the other ones.
Comment posted by pim on Tuesday, May 6, 2008 9:00 AM
Excellent article. After a couple of days trying to figure out how to get GridView to pass multiple variables to another page, i finally arrived at your article.  It didn't work the first time, but after a couple of hours of pulling out my hair, i figured out, that one of my fieldnames was "NAME" and apparently, not allowed.  As soon as i changed that to something else, everything worked like a charm.  
Comment posted by huilan on Tuesday, May 6, 2008 3:34 PM
excellent explanation
Comment posted by md.tahir1 on Friday, May 9, 2008 7:34 AM
how to save some column grid view and some textboxes, combo boxes values save in sql server in vb.net
Comment posted by amuse on Wednesday, May 14, 2008 1:29 PM
excellent article. really helped a lot.

I still need to know something more. I am trying to pass a integer(a primary key) to another page which is going fine. i get the value printed in the label fine thru the c# code. but i am still not able to use the value in the form view of the new page (insert update and delete dont work). Can you help??
Comment posted by shekhar kumar on Thursday, May 22, 2008 8:13 AM
excelent.
but can a url value be send with hyperlink field.

Thanks
Shekhar
Comment posted by suprotim agarwal on Thursday, May 22, 2008 11:15 AM
amuse: If you are getting the value, I do not see a reason why you aren't able to use that value. I would suggest to create a get set property which can be populated with this value and can be used on the new page.
Comment posted by suprotim agarwal on Thursday, May 22, 2008 11:16 AM
amuse: If you are getting the value, I do not see a reason why you aren't able to use that value. I would suggest to create a get set property which can be populated with this value and can be used on the new page.

Shekhar: The example demoed is with the hyperlink field. What else do you seek?
Comment posted by Ashok on Monday, May 26, 2008 8:08 AM
very good explanation very very useful
Comment posted by Bilal Haider on Wednesday, June 4, 2008 5:36 AM
Hi.
Great stuff.
Keep trying new tricks.
Comment posted by Ram on Thursday, June 5, 2008 2:09 AM
I have done it but only the "pass single value" is been highlighted as a hyperlink n "pass multiple values" is not highlighted as  hyperlink
Comment posted by renjith on Saturday, June 14, 2008 5:43 AM
plz tell about menu'case
Comment posted by djs75 on Thursday, June 19, 2008 11:31 AM
Do you know if anybody has extended the HyperLinkField control to obfuscate DataNavigateUrlFields ? Typically, you have to put the logic in to obfuscate as you don't want to pass the fields in the URL to guard against SQL injection. It would be easy to have a element like ObfuscateData=<key> and handle it on the Page_load to unobfuscate based on the key. Just haven't heard or seen if somebody has attempted it this way.
Comment posted by muhammed on Monday, July 7, 2008 11:19 AM
this tutorial was so good
Comment posted by Rajendra Sedhain on Wednesday, July 16, 2008 2:38 PM
Excellent...it heplps me. keep it up
Comment posted by Chaitanya on Sunday, July 20, 2008 1:24 PM
Excellent article..
Comment posted by Joel on Monday, July 21, 2008 2:19 PM
This article is very helpful and easy to understand
Comment posted by Clark Kent on Monday, July 21, 2008 5:00 PM
what does obfuscate mean?
Comment posted by qqq on Tuesday, July 22, 2008 1:13 AM
brief
Comment posted by Suprotim Agarwal on Tuesday, July 22, 2008 4:46 AM
Clark: Read more about obfuscation over here:

http://en.wikipedia.org/wiki/Obfuscated_code
Comment posted by NTT on Tuesday, July 22, 2008 5:38 AM
how do i use URlEncode to the parameter value in cases where it's value contains &, for example
Comment posted by ddo on Tuesday, July 22, 2008 11:45 AM
I was with you until step 5. This block of code is added to the CustomerDetails.aspx page? or the defualt.aspx?
and if so, where in the page is it added. It was all wonderfully specific and clear until "Set a breakpoint at the Page_Load method of the CustomerDetails.aspx"  Where can i find the Page_load method?
Comment posted by ddo on Tuesday, July 22, 2008 11:57 AM
I was with you until step 5. This block of code is added to the CustomerDetails.aspx page? or the defualt.aspx?
and if so, where in the page is it added. It was all wonderfully specific and clear until "Set a breakpoint at the Page_Load method of the CustomerDetails.aspx"  Where can i find the Page_load method?
Comment posted by ddo on Tuesday, July 22, 2008 3:41 PM
OK. i figured out step 5 and have it working. is there a way to have the returns show up in different locations on the CustomerDetails page? i.e. the customer name in the page url (as is), but the company name, address etc in the body of the page?
Sorry for my stupidity, but appreciate any help
Comment posted by Suprotim Agarwal on Wednesday, July 23, 2008 2:13 AM
NTT:  Good question. May you could modify the url before it is put in the GridView. You wld use the RowDataBound:

   protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
        foreach (row in GridView1.Rows)
{
            if ((e.Row.RowType == DataControlRowType.DataRow))
{
                string tmp = e.Row.Cells[1].Text.ToString;
                string afterEncode = HttpUtility.UrlEncode(tmp);
                e.Row.Cells[1].Text = afterEncode;
            }
        }
    }

Comment posted by Suprotim Agarwal on Wednesday, July 23, 2008 2:14 AM
NTT: Check out another method http://blog.steeleprice.net/archive/2004/07/13/365.aspx
Comment posted by Suprotim Agarwal on Wednesday, July 23, 2008 4:35 AM
ddo: Glad you figured it out.

Is there a way to have the returns show up in different locations on the CustomerDetails page?

Again, I apologize but I could not understand your question. I hope you do not mind explaining it again!
Comment posted by Jignasha Dholkiya on Wednesday, July 23, 2008 5:04 AM
Article is very good and it increase my knowledge about ASP.Net. I am a MCA Student. I want to know which questions are asked in an interview if we want to go in VB.NET and ASP.NET technology. Please send me the answer in my email.
Comment posted by Ivo Atanasov on Wednesday, July 23, 2008 6:44 AM
Using this method means that the information will be displayed in the querystring. In most scenarious this is not recommended.So there must be way the data not to be showed.
Comment posted by Suprotim Agarwal on Wednesday, July 23, 2008 7:56 AM
Jignasha: Please check the interview questions over here
http://www.dotnetcurry.com/BrowseArticles.aspx?CatID=58

Ivo: Thanks for explaining Obfuscation in simple words!!
Comment posted by Ivo Atanasov on Thursday, July 24, 2008 4:42 AM
Suprotim, Thanks for the link with question .It is very detailed list of   asked questions.
Comment posted by hayat on Thursday, July 24, 2008 6:07 AM
merci
Comment posted by Ivo Atanasov on Thursday, July 24, 2008 6:22 AM
Suprotim, Thanks for the link with question .It is very detailed list of   asked questions.
Comment posted by Suprotim Agarwal on Thursday, July 24, 2008 12:05 PM
Ivo: You are welcome!!
Comment posted by Tanner on Thursday, July 24, 2008 4:58 PM
Your articles are always well written and easy to follow.  Keep up the good work.

..and thanks for clearing up multiple values in a querystring.
Comment posted by Kamran on Friday, July 25, 2008 6:56 AM
Very Biggener/Childish Post
Comment posted by azees on Saturday, July 26, 2008 1:54 AM
Really Good
Comment posted by Hariom Thakur on Saturday, July 26, 2008 7:53 AM
how to bind multiple columns in gridview whn we r breaking string in a praticular columns
Comment posted by Hassan on Saturday, July 26, 2008 12:37 PM
Very nice trick actually. Never thought of it. However there are several other ways to do the same. No matter what this article is very useful.
Comment posted by N3PKC on Monday, July 28, 2008 7:22 AM
There's always a number of ways to do the same thing in .NET.  It's nice to find a way that is simple, easy to implement, and efficient.
Comment posted by Venkat on Tuesday, July 29, 2008 11:56 PM
simple and useful information. But passing values through URL is security threat.. how to avoid it..
Comment posted by Ivo Atanasov on Wednesday, July 30, 2008 3:21 AM
I use session variables to pass information from selected row in grid to another page.I use  simple radiobutton to select a row and with method Request.Form() I write in session the ID of the row.I am careful for session timeout and take care to clear session to not pass incorrect information to the another page.I reckon that passing data through session is more secure.  
Comment posted by Bill on Wednesday, July 30, 2008 2:05 PM
Thanks a bunch. This made my life easier. I'm definitely coming back when I have more questions.
Comment posted by mirainc on Thursday, July 31, 2008 11:51 PM
I want to display the records on a label on the directed page..
how can i do that?
ive already implemented the method above just that i need the records to display on a label.
Comment posted by Suprotim Agarwal on Friday, August 1, 2008 12:51 AM
Venkat: There was a nice article by Scott Mitchell about the same. http://aspnet.4guysfromrolla.com/articles/083105-1.aspx

Ivo: Session would also be suitable if the value is to be used multiple times in your app. If used frequently and heavily, it can load the RAM.

mirainc: Just do it in the following manner

Label1.Text = Request.QueryString["CID"];
Label2.Text = Request.QueryString["CName"];

and so on...
Comment posted by ramamoorthy on Friday, August 8, 2008 12:21 AM
its working fine thx
Comment posted by FallenAngel on Wednesday, August 13, 2008 5:55 AM
Thanks, this has made life a lot easier for the project im working on - though I do have a question. I have a page (Activities) on which users can type search criteria into a textbox. When they hit the search button it opens a new page (SearchCriteria) with all the related values in a grid box [this is where your code came in use]. The problem I have is that the multiple fields are passed back to a NEW instance of (Activities) instead of my already present one. Is there a way to get around this?
Thanks,
Comment posted by Suprotim Agarwal on Wednesday, August 13, 2008 8:43 PM
FallenAngel: Check this article of mine to solve your problem.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=99
Comment posted by vinayak on Thursday, August 14, 2008 6:48 AM
Thanks, this has made life a lot easier for the project im working on - though I do have a question. I have a page (Activities) on which users can type search criteria into a textbox. When they hit the search button it opens a new page (SearchCriteria) with all the related values in a grid box [this is where your code came in use]. The problem I have is that the multiple fields are passed back to a NEW instance of (Activities) instead of my already present one. Is there a way to get around this?
Thanks,
Comment posted by FallenAngel on Thursday, August 14, 2008 8:12 AM
Thank you again! That article contained exactly what I needed. I had managed to adapt the code on this article to run but was having problems with the values vanishing upon firing a PostBack. All sorted now though :)
Comment posted by raghu on Saturday, August 30, 2008 10:32 AM
this article is very helpful for me thank u very much.....
Comment posted by omel on Tuesday, September 16, 2008 12:05 AM
why the value pass to form not textbox??
Comment posted by Sandeep on Friday, October 3, 2008 1:54 AM
The article which you have provided is really good, It helped me a lot in passing the values from gridview to textboxes of another page. I would like to know how to make this session expire aftersome time?
Comment posted by Hemant Desai on Friday, October 3, 2008 5:03 PM
Thanks It is very good stuff.
Comment posted by Trimurthy on Tuesday, October 7, 2008 3:46 AM
Thanks for your information
how to send by using code behind page
Comment posted by Trimurthy on Tuesday, October 7, 2008 4:55 AM
Thanks for your information
how to send by using code behind page
Comment posted by Suprotim Agarwal on Tuesday, October 7, 2008 1:20 PM
Sandeep: Which session do you talk about?

Trimurthy: You could use the SelectedChanged event or use query strings and form the url to traverse to. Then use Response.Redirect to move to that page. See a sample:
http://forums.asp.net/p/1245191/2286373.aspx
Comment posted by vinay H N on Tuesday, November 4, 2008 10:01 AM
I am facing problem in passing Multiple values. Single value works fine But HyperLink is not displaying in the PAge
Comment posted by Leela on Sunday, November 9, 2008 12:18 PM
Thank you very much for such a detailed tutorial. I spent almost one whole day trying to find out how this works before I finally saw this article.This helped me a lot. Thanks again!!
Comment posted by kiran on Tuesday, November 18, 2008 4:27 AM
insert some other latest projects
Comment posted by Abhishek Sahai on Tuesday, December 9, 2008 5:49 AM
I realy Like this code very Easy and effective thanx
for help
Comment posted by mohamed on Tuesday, December 16, 2008 6:51 AM
how to count today date to another date. and how to check parent path in server location. pls reply to my email by sihabudeen
Comment posted by gagan on Friday, January 2, 2009 8:19 AM
hi hw r u
Comment posted by sudhakar on Wednesday, February 18, 2009 4:07 AM
Im not able to see the hyperlink if more than 2 parameters are passed to a differecnt in the Hyperlink. can you please help me to solve this problem, thanks in advance.

<asp:HyperLinkField DataNavigateUrlFields="CustomerID, CompanyName, ContactName, Address, City" DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}&CName={1}&ContactName={2}&Addr={3}&City={4}" Text="Pass Multiple Values" />
its not showing the hyperlink on the Gridview, when parameters are more than 2.
Comment posted by Suprotim Agarwal on Wednesday, February 18, 2009 8:36 PM
Sudhakar: I am not sure i understand when you say that hyperlink is not visible. Where is is not visible. The code in the article works well and I suggest you to look into your code for errors. Also make sure that the Hyerplink field is in the <Columns> collection.
Comment posted by sudhakar on Thursday, February 19, 2009 5:44 AM
Agarwal,thanks for reply.I mean the hyperlink text is visible, but its not a Hyperlink, where I can click. this is working when I am passing 1 or 2 parameters.but when Im passing more than 2 parameters, the link is not good for clicking.. Pls see the code Im using.
TaskList.aspx

<asp:GridView ID="TaskListGV" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="TaskNo" DataSourceID="SqlDataSource1" Height="209px" PageSize="5" Style="z-index: 100; left: 35px;
            position: absolute; top: 153px" Width="765px"  >
            <HeaderStyle CssClass="HeaderStyle" />
                <AlternatingRowStyle CssClass="AlternatingRowStyle" />
            <Columns>
            
                <asp:HyperLinkField DataNavigateUrlFields="TaskNo" DataNavigateUrlFormatString="ViewTaskList.aspx?TaskNo={0}"  
                    HeaderText="View Data" Text="Select" />
                               <asp:BoundField DataField="TaskNo" HeaderText="TaskNo" InsertVisible="False" ReadOnly="True"
                    SortExpression="TaskNo" />
                <asp:BoundField DataField="TaskName" HeaderText="TaskName" SortExpression="TaskName" />
                <asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate" />
                <asp:BoundField DataField="EndDate" HeaderText="EndDate" SortExpression="EndDate" />
                <asp:BoundField DataField="TaskType" HeaderText="TaskType" SortExpression="TaskType" />
                <asp:BoundField DataField="TaskStatus" HeaderText="TaskStatus" SortExpression="TaskDesc" />
<asp:TemplateField HeaderText="Delete">
                         <ItemTemplate>
                        <asp:CheckBox ID="cbDeleteRow"  runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                
              <%--   <asp:HyperLinkField DataNavigateUrlFields="TaskName"  HeaderText="edit" DataNavigateUrlFormatString="EditTaskList.aspx?TName={0}" /> --%>
  
                
               <asp:HyperLinkField DataNavigateUrlFields="TaskNo,TaskName" DataNavigateUrlFormatString = "~/EditTaskList.aspx?TNo={0}&TName={1}" HeaderText="Edit"  Text="Edit" />
                
                        </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TaskListConnectionString %>"
            SelectCommand="SELECT [TaskNo], [TaskName], [StartDate], [EndDate], [TaskType], [TaskStatus] FROM [Task]" ConflictDetection="CompareAllValues"
            DeleteCommand="DELETE FROM [Task] WHERE [TaskNo] = @original_TaskNo AND [TaskName] = @original_TaskName AND [StartDate] = @original_StartDate AND [EndDate] = @original_EndDate AND [TaskType] = @original_TaskType AND [TaskStatus] = @original_TaskStatus"
            InsertCommand="INSERT INTO [Task] ([TaskName], [StartDate], [EndDate], [TaskType], [TaskStatus]) VALUES (@TaskName, @StartDate, @EndDate, @TaskType, @TaskStatus)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Task] SET [TaskName] = @TaskName, [StartDate] = @StartDate, [EndDate] = @EndDate, [TaskType] = @TaskType, [TaskStatus] = @TaskStatus WHERE [TaskNo] = @original_TaskNo AND [TaskName] = @original_TaskName AND [StartDate] = @original_StartDate AND [EndDate] = @original_EndDate AND [TaskType] = @original_TaskType AND [TaskStatus] = @original_TaskStatus">
            <DeleteParameters>
                <asp:Parameter Name="original_TaskNo" Type="Int32" />
                <asp:Parameter Name="original_TaskName" Type="String" />
                <asp:Parameter Name="original_StartDate" Type="DateTime" />
                <asp:Parameter Name="original_EndDate" Type="DateTime" />
                <asp:Parameter Name="original_TaskType" Type="String" />
                <asp:Parameter Name="original_TaskStatus" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="TaskName" Type="String" />
                <asp:Parameter Name="StartDate" Type="DateTime" />
                <asp:Parameter Name="EndDate" Type="DateTime" />
                <asp:Parameter Name="TaskType" Type="String" />
                <asp:Parameter Name="TaskStatus" Type="String" />
                <asp:Parameter Name="original_TaskNo" Type="Int32" />
                <asp:Parameter Name="original_TaskName" Type="String" />
                <asp:Parameter Name="original_StartDate" Type="DateTime" />
                <asp:Parameter Name="original_EndDate" Type="DateTime" />
                <asp:Parameter Name="original_TaskType" Type="String" />
                <asp:Parameter Name="original_TaskStatus" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="TaskName" Type="String" />
                <asp:Parameter Name="StartDate" Type="DateTime" />
                <asp:Parameter Name="EndDate" Type="DateTime" />
                <asp:Parameter Name="TaskType" Type="String" />
                <asp:Parameter Name="TaskStatus" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        &nbsp;&nbsp;&nbsp;&nbsp;
        
    
    </div>
        
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 102;
            left: 18px; position: absolute; top: 96px" Text="Add" Width="51px" CssClass="button" />
        <div style="z-index: 105; left: 277px; width: 154px; position: absolute; top: 18px;
            height: 33px">
            <strong><span style="font-size: 18pt; color: #ff0000; font-family: Sans-Serif">Task
            List&nbsp;</span></strong></div>
        &nbsp;
        <asp:Button ID="GOButton" runat="server" OnClientClick="return confirmMsg(this.form)"
OnClick="GOButton_Click" Style="z-index: 103;
            left: 716px; position: absolute; top: 528px" Text="GO"  Height="24px"  Width="51px" />
        &nbsp;
    </form>
</body>
</html>

Comment posted by shri on Friday, April 17, 2009 1:44 AM
Thanks ,good one
saved my time
Comment posted by fahim on Wednesday, May 6, 2009 6:52 AM
actually iwant to retrieve the value of a simple  variable to another page but i dont wanna use session ,and other managmnt technique,ploease can you help me
Comment posted by Niveditha on Friday, May 22, 2009 10:10 AM
Im not able to see the hyperlink if more than 1 parameters are passed to a differecnt in the Hyperlink. can you please help me to solve this problem, thanks in advance.

<asp:HyperLinkField DataNavigateUrlFields="CustomerID, CompanyName, ContactName, Address, City" DataNavigateUrlFormatString="CustomerDetails.aspx?CID={0}&CName={1}&ContactName={2}&Addr={3}&City={4}" Text="Pass Multiple Values" />
its not showing the hyperlink on the Gridview, when parameters are more than 1.
Comment posted by Suprotim Agarwal on Friday, May 29, 2009 1:23 PM
Niveditha: The article explains how to pass multiple parameters. Kindly try it out once with the original code mentioned here, you can then replace it with your code once you have a complete undcerstanding of it.
Comment posted by ADIL on Monday, June 22, 2009 10:15 AM
Thank ?U so much :)
Comment posted by SismeyA on Friday, June 26, 2009 4:48 PM
Hi,

Is there any way you can add a tick boxes instead of URL to each row, select the individual rows with tick boxs and click a button to pass results over ?? This would be exactly what I require

Much apreciated

Andy
Comment posted by SismeyA on Friday, June 26, 2009 4:59 PM
Hi,

Is there any way you can add a tick boxes instead of URL to each row, select the individual rows with tick boxs and click a button to pass results over ?? This would be exactly what I require

Much apreciated

Andy
Comment posted by Nadera on Wednesday, August 26, 2009 5:57 AM
Merci beaucoup ca ma vraimenet aide .

Nadera.Algerie
Comment posted by John L on Friday, September 18, 2009 1:36 PM
The hyperlink code that you provide doesnt seem to be actually creating a hyper link. It just displays the text. Am I missing something?
Comment posted by John L on Friday, September 18, 2009 1:40 PM
Nevermind. I was attempting to do this in a repeater control instead of a gridview. Anyone know how to do this in a repeater control?
Comment posted by Andrew Z. on Friday, December 11, 2009 1:26 PM
Fabulous!  You  are a genius!  Thank the Lord for your willingness to contribute!!
Comment posted by Hassan B on Sunday, December 20, 2009 1:11 AM
i just did what you explained about passing multi parameters when pressing on a hyperlink,but when writing the parameters that i want to send the hyperlink stops being highlighted dont know the problem for more prove this is my source code:):

<%@ 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>
    
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="username" HeaderText="username"
                SortExpression="username" />
            <asp:HyperLinkField DataNavigateUrlFields="subject,Date,username"
                DataNavigateUrlFormatString="Default2.aspx?subject={0}&date={1}&username={2}" DataTextField="subject"
                DataTextFormatString="{0}" HeaderText="subject" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:hotmailusersConnectionString %>"
         SelectCommand="StoredProcedure2"
        SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:SessionParameter DefaultValue="2" Name="id" SessionField="ID" Size="5"
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>
Comment posted by Tee on Monday, January 11, 2010 9:26 AM
Brilliant article, thank you very much. I have come across an issue that I hope can be solved. One of my fields is name and it'll have something like "Sanford & Son" or "P&H Brothers" this is causing errors because it only takes the "Sanford " or "P" respectively and not return any results. How do I take care of "&"
Comment posted by Tee on Monday, January 11, 2010 9:48 AM
Brilliant article, thank you very much. I have come across an issue that I hope can be solved. One of my fields is name and it'll have something like "Sanford & Son" or "P&H Brothers" this is causing errors because it only takes the "Sanford " or "P" respectively and not return any results. How do I take care of "&"
Comment posted by Ketki Kathavate on Friday, January 29, 2010 9:54 AM
Hi, all the answers are very good but currently they are not useful for me.I have a doubt where in there is a gridview where I have selected an individual row and using hyperlink field in the gridview the selected record gets transfered into a dropdown on another page so what I want is that when I select any record the data in the gridview should remain as it is and the newly selected record should get added to it.So please help me out in this part...
Thank you
Comment posted by Ketki Kathavate on Friday, January 29, 2010 9:56 AM
Hi, all the answers are very good but currently they are not useful for me.I have a doubt where in there is a gridview where I have selected an individual row and using hyperlink field in the gridview the selected record gets transfered into a dropdown on another page so what I want is that when I select any record the data in the gridview should remain as it is and the newly selected record should get added to it.So please help me out in this part...
Thank you
Comment posted by stupid boy on Friday, March 5, 2010 2:29 AM
programmer that ask question is so stupid
Comment posted by Leon on Friday, May 14, 2010 12:18 AM
when i go to the new page,it only generates issue_id and does not generate the rest of the value

what could be wrong, below is my code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SmallWindow.aspx.vb" Inherits="log.SmallWindow" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 314px;
        }
        .style3
        {
            width: 314px;
            height: 23px;
        }
        .style4
        {
            height: 23px;
        }
        .style5
        {
            height: 23px;
            width: 359px;
        }
        .style6
        {
            width: 359px;
        }
        .style7
        {
            width: 382px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <br />
        <asp:Panel ID="Panel1" runat="server">
            <table class="style1">
                <tr>
                    <td colspan="3" style="text-align: center; background-color: #FF0000">
                        View Issue Details</td>
                </tr>
                <tr>
                    <td class="style3">
                        Issue_id:</td>
                    <td class="style5">
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td class="style4">
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        Category:</td>
                    <td class="style6">
                        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        Title:</td>
                    <td class="style6">
                        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        Date Opened</td>
                    <td class="style6">
                        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        Priority:</td>
                    <td class="style6">
                        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        Assigned_to:</td>
                    <td class="style6">
                        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        Status:</td>
                    <td class="style6">
                        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        resolution Status:</td>
                    <td class="style6">
                        <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton1" runat="server" BorderStyle="Ridge">Update Status</asp:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        Branch:</td>
                    <td class="style6">
                        <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td class="style6">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
        </asp:Panel>
        <br />
        <br />
        <asp:Panel ID="Panel2" runat="server">
            <table class="style1">
                <tr>
                    <td colspan="2" style="text-align: center; background-color: #FF0000">
                        Update Resolution Call Status</td>
                </tr>
                <tr>
                    <td class="style7">
                        Issue_id:</td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server" Height="21px" Width="190px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style7">
                        Select Status:</td>
                    <td>
                        <asp:DropDownList ID="cmbstatus3" runat="server" Height="16px" Width="194px">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="style7">
                        Comments:</td>
                    <td>
                        <asp:TextBox ID="TextBox5" runat="server" Height="86px" Width="227px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style7">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style7">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style7">
                        &nbsp;</td>
                    <td>
                        <asp:Button ID="cmbresolve" runat="server" Text="Resolve" />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="cmbcancel" runat="server" Text="Cancel" />
                    </td>
                </tr>
                <tr>
                    <td class="style7">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
        </asp:Panel>
        <br />
        <br />
        <br />
        <br />
    
    </div>
    </form>
</body>
</html>
Comment posted by Adeel on Thursday, May 20, 2010 10:16 AM
This is very helpful. I have a problem for which I need help. I need to display the GridView from one page to another..... can anyone help me out on this as I am new to .Net
Comment posted by Irvin on Friday, June 18, 2010 8:56 AM
This is very helpful for me.

But the problem is in my update page. here the scenario:

1. page view using gridDataView with link (page name: Retrive.aspx).
2. page update.aspx for updating.
3. Passing values from Retrieve.aspx to update.aspx works using above solution.
4.
    <b>Update.aspx</b>
    protected void Page_Load(object sender, EventArgs e)
    {

        //CurrencyID, Description
        txtCurrencyID.Text = Request.QueryString["cid"];
        txtDescription.Text = Request.QueryString["descr"];
}
5. Edit txtCurrencyID and enter new value ();
6.
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
         Currencies updCurr = new Currencies();

        newCurr.Update(txtCurrencyID.Text, txtDescription.Text);

    }
7. The problem is the txtCurrencyID textbox does not retained the new input value instead the values remains the same.
Comment posted by sam on Thursday, July 29, 2010 3:19 AM
how to make the data viewable in CustomerDetails.aspx?
Comment posted by ian on Tuesday, September 14, 2010 4:08 AM
wow i have just spent a week learning gridview and passong values. This one was great.
thanks a lot my problem just became very much less than it was.

thanks great.
Comment posted by bharathi on Thursday, September 16, 2010 5:39 AM
i cant understand
Comment posted by Pat on Friday, September 24, 2010 2:11 PM
Hello. I was wondering if you can help me?

When I click a link from the table, it should redirect to another page, right? Mine's redirecting but the values that should be "passed" from the table won't appear. :-(
Comment posted by Pat on Friday, September 24, 2010 9:15 PM
Hello. I was wondering if you can help me?

When I click a link from the table, it should redirect to another page, right? Mine's redirecting but the values that should be "passed" from the table won't appear. :-(
Comment posted by Suprotim Agarwal on Sunday, September 26, 2010 5:00 AM
Pat: Make sure that in the next page, you are reading back the values like Request.QueryString["CID"];
Comment posted by hulya on Thursday, November 4, 2010 3:27 PM
Thank you! You saved my day :)
Comment posted by ashkan2005 on Monday, December 13, 2010 10:44 AM
hi
how do use the datagride for give data not for view data.
for example add textbox in datagride.
thanks.
Comment posted by nirmalraj on Wednesday, December 22, 2010 7:08 AM
hi

i retrieve data frm hyperlinknavigator from grid using edit link and redirected to update page i am getting all the value in the textbox but i cant able to update the record after giving the update statement
Comment posted by Swathi on Thursday, March 17, 2011 7:47 AM
Very nice article....
Comment posted by Ravi Kumar Nikkala on Tuesday, April 12, 2011 1:56 AM
very nice article . It is very help full for me.... Thanq.
Comment posted by Rahul on Sunday, May 15, 2011 5:44 AM
hii...the code works and putting a breakpoint displays the data in the request.querystring but it shows null in the  request.querystring["id"] and hence i am unable to use it in a label or a textbox ...and yes i have used the query string also correctly..so is there any property left that can help me display values...thanx
Comment posted by Cagan on Tuesday, May 17, 2011 10:29 AM
Very nice article, worked for me.  Thank you.
Comment posted by monika on Monday, December 12, 2011 11:33 PM
scfdedfsa
Comment posted by rudresh on Friday, December 23, 2011 11:19 PM
this article is good.I had textboxes in gridview i want to tansfer some values into anthoer how to do it plz suggest me
Comment posted by ali on Wednesday, January 18, 2012 4:23 AM
Really Very useful article.
Comment posted by manikandan on Tuesday, February 21, 2012 5:32 AM
very useful in u r website many project i did this same concept use
Comment posted by Anupam Pushkar on Tuesday, February 28, 2012 12:59 PM
Thanks a lot for your great help :)
Comment posted by Chirag Shah on Wednesday, June 20, 2012 11:57 AM
Thank a lot Suprotim. A Very simple but a useful artical. Keep it up....!!!!
Comment posted by madhukumar on Tuesday, February 26, 2013 12:58 AM
its good ,
i use this code
but i need to pass the gridview values to dropdownlist in another page
how can i?

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel