In this article I will show you that how you can pass the values from the GridView control to other pages. We will see some of the common scenarios in which you wish to pass the values to different pages. The article is also supplemented with code so please feel free to download it.
Introduction:
In this article I will show you that how you can pass the values from the GridView control to other pages. We will see some of the common scenarios in which you wish to pass the values to different pages. The article is also supplemented with code so please feel free to download it.
Using bound columns with select columns:
Let's first examine the most basic scenario. In this you are displaying the data in the GridView control using the BoundColumns and you want to click on the select column and get the value of the primary key or any other column. Once you have displayed the columns on the GridView control using the BoundColumns. You can access the select link button using the "SelectedIndexChanged" event of the GridView control.
Check out the code below:
protected
void
GridView1_SelectedIndexChanged(object
sender, EventArgs
e) { /* In the line below you can retreive the value of the CategoryID * But the CategoryID is displayed on the page which is not a good idea You can easily set the visible property of the CategoryID column to false but you will still be able to access the value of the CategoryID*/ Response.Write(GridView1.SelectedRow.Cells[0].Text); } |
As, you have noticed that since I am using BoundColumns to access the value of the first column using the Cells property. Although this will work as expected but this is not a very good solution. The problem with this approach is that when you switch the columns from one to three or any other way you will be forced to change the code behind to reflect those changes so that the Cells property will target the correct column.
Most of the time you don't want to display the primary key of the table which in this case is CategoryID. You can make the CategoryID column invisible by using its property visible = false.
Using template column with select columns:
This is a much better approach since now your GridView is no longer dependent on the position of the columns in the GridView control. Let's see some code that is used to generate the template columns of the GridView control.
<asp:TemplateField
HeaderText="CategoryID"
Visible="False"> <ItemTemplate> <asp:Label ID="lblCategoryID" runat="server" Text='<%# Eval("CategoryID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryName"> <ItemTemplate> <asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Link"> <ItemTemplate> |
As, you can see in the code above I have used ASP.NET Label control to bind the text from the database. And check out the code below which shows how we can access individual columns.
protected
void
GridView1_SelectedIndexChanged(object
sender, EventArgs
e) { string selectedText = ((Label)GridView1.SelectedRow.FindControl("lblCategoryID")).Text; Response.Write(selectedText); } |
As you can see in the code above the columns are no longer dependent on the position of the cell index.
Generating links inside GridView control from id from database:
You might find this the most important. Many times we have a hyperlink inside the GridView control and we want that hyperlink to go to certain page based on the id present in the database. I will tell you a very simple and easy way to accomplish this thingy :).
First make a hyperlink column inside the GridView control.
<asp:TemplateField
HeaderText="Link"> <ItemTemplate> <asp:HyperLink ID="hlUrl" runat="server" NavigateUrl='<%# FormatUrl( (int) Eval("CategoryID") ) %>' Text = '<%# Eval("CategoryName") %>' /> </ItemTemplate> </asp:TemplateField> |
Let's first see the Text property which is binds the "CategoryName". "NavigateUrl" property calls a method which is named "FormatUrl". The FormatUrl takes an integer parameter which in this case is "CategoryID". Let's now look at the FormatUrl method which is defined in the code behind of the aspx page.
// This method is used
to generate a new url
protected string FormatUrl(int categoryID) { if (categoryID < 1) throw new ArgumentOutOfRangeException("CategoryID"); return "CategoryDetails.aspx?categoryID=" + categoryID; } |
FormatUrl method is pretty simple to understand. It take a categoryID which is of type Int32. If the categoryID is less than 1 then there is some error and hence we throw an exception. If however the categoryID is greater then 0 then we simply concatenates the categoryID with the string url and return the newly generated url back to the GridView control.
Sending Mulitple Values from GridView to different page:
I think I have discussed this issue lot of times in my previous articles. But here it is once again. If you want to select multiple values from the GridView control and send to a new page you can easily do this by using checkboxes inside the GridView control which will enable you to select multiple items. After selecting the items you need to code for the ASP.NET Button server control which will send the selected values to the new page.
Let's take a look at the code:
protected
void
Button1_Click(object
sender, EventArgs
e) { ArrayList selectedValues = new ArrayList(); // This will select multiple items foreach (GridViewRow row in GridView1.Rows) { bool result = ((CheckBox) row.FindControl("CheckBox1")).Checked; if (result) { int categoryID = Convert.ToInt32(((Label)row.FindControl("lblCategoryID")).Text); selectedValues.Add(categoryID); } } // Put the selected values in the Session to be used in a different page Session["SELECTEDVALUES"] = selectedValues; // transfer to a new page Response.Redirect("NewPage.aspx"); }
|
And here is the code for NewPage.aspx:
if
(!Page.IsPostBack) { // Check that if the session object contains anything or not if (Session["SELECTEDVALUES"] != null) { ArrayList selectedValues = (ArrayList)Session["SELECTEDVALUES"]; // print out the values foreach (int intValue in selectedValues) Response.Write(intValue); } } |
Using HyperLinkColumn to send the values to a new page:
The last technique I will discuss is that if you want to use the HyperLinkColumn to pass values to a new page. This is probably the easiest of them all. Let's take a look how this is done. First thing you need to do is to add a new HyperLinkColumn.
<asp:HyperLinkField
DataTextField="CategoryName"
DataNavigateUrlFields="CategoryID"
DataNavigateUrlFormatString="NewPage.aspx?id={0}" HeaderText="HyperLink Column" /> |
The DataTextField property is used to display the text on the screen. In this case I have set it to "CategoryName" which means "CategoryName" will be displayed as Text inside the GridView control.
"DataNavigateUrlFields" represents what you want to send to the next page which in this case is "CategoryID".
"DataNavigateUrlFormatString" property represents which page you want to send the value. In this case it is NewPage.aspx and it will be in the querystring variable called id.
You can easily receive the values send by the HyperlinkColumn using the following code:
|
As, you can see how easy it is to send as well receive the values. I know you must be wondering how you can pass multiple values. Well, its pretty simple. Check out the code below:
<asp:HyperLinkField
DataTextField="CategoryName"
DataNavigateUrlFields="CategoryID,CategoryName"
DataNavigateUrlFormatString="NewPage.aspx?id={0},CId={1}"HeaderText="HyperLink
Column" />
|
Simply, separate the column names using comma and that's it.
I hope you liked the article, happy coding!