Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to pass external values with GridView HyperLinkField which are not part of your Gridview DataSource member

, 4 Aug 2010
Rate this:
Please Sign up or sign in to vote.
How to pass external values with GridView HyperLinkField which are not part of your Gridview DataSource member

Few days ago, I published an article “How to pass multiple values using GridView HyperLinkField?”, where I explained how you can pass multiple parameters with Gridview HyperLinkField using DataNavigationUrlField and DataNavigateUrlFormatString properties. In this post, I am going to explain how you can pass some external values as parameters with the same hyperlink field.

DataNavigationUrlField uses only those fields as parameter which are part of GridView DataSource. Now the problem comes when you want to pass some other variables as parameters which are not part of the DataSource. As shown in the image below, we are passing EmpID and ParentId as arguments and these two field are the data members of GridView DataSource.

GridField

Now say, You want to pass ChildID for that particular record along with ParentID and EmpID and you want that the hyperlink URL should be like “Default.aspx?EmpID=1&ParentID=P1&ChildID=C1”where ChildID is not part of datasource.

You can achieve this by writing code in code behind for the particular GridView. There are two events where you can overwrite the navigation URL of that hyperlink field. You can use Gridview_RowDataBound or Gridview_PreRender for the same.

Let’s start with Gridview_RowDataBound. The RowDataBound event of GridView is raised when a data row is bound to data. So for each and every row RowDataBound event is raised to bind with actual data from the data source. On the RowDataBound event, you can check for the particular Cell control and can append the NavigateURL. Below is the code snippet for the same:

/// <span class="code-SummaryComment"><summary>
</span>/// Handles the RowDataBound event of the grdStudent control.
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="sender">The source of the event.</param>
</span>/// <span class="code-SummaryComment"><param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> 
</span>/// instance containing the event data.<span class="code-SummaryComment"></param>
</span>protected void grdStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink hyperlink = (HyperLink)e.Row.Cells[0].Controls[0];
        hyperlink.NavigateUrl += "&ChildID=" + this.ExternalValue;
    }
} 

And you can do the same thing in Gridview_PreRender event in a similar way. As per the ASP.NET page life cycle, Pre_Render for control is raised just before save view state and Render event. So this is the last event where you can customize your control before saving the viewstate data and rendering it. Below is the code snippet for the same:

/// <span class="code-SummaryComment"><summary>
</span>/// Handles the PreRender event of the grdStudent control.
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="sender">The source of the event.</param>
</span>/// <span class="code-SummaryComment"><param name="e">The <see cref="System.EventArgs"/> 
</span>/// instance containing the event data.<span class="code-SummaryComment"></param>
</span>protected void grdStudent_PreRender(object sender, EventArgs e)
{
    foreach (GridViewRow row in grdStudent.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
             HyperLink grdviewLink = (HyperLink)row.Cells[0].Controls[0];
             grdviewLink.NavigateUrl += "&ChildID=" + this.externalValue;
        }
    }
}

If you want to know how it’s actually working, just set a breakpoint during databound, you will find NavigateURL for that hyperlink field has already been set with the value that you have passed as DataNavigationUrlField. And inside RowDataBound or Pre_Render, we are appending the same NavigateURL with an external parameter.

GridNavigation

GridUpdated

Below is the HTML snippet for the same HyperLinkField.

HyperlinkHTML

You can use NavigateURL properties for Hyperlinkfield to set URL, but NavigateURL will set the same URL for all the rows. So if you want a different URL for each and every row, you have to use DataNavigationUrlField or you need to override the NavigateURL during RowDataBound or Pre_Render. And if you set both the properties (NavigateURL and DataNavigationUrlField) the DataNavigationUrlField property takes precedence.

I hope this post will help you.

Shout it


Filed under: ASP.NET, Tips and Tricks, Visual Studio

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband
 
Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 PinmvpAbhishek Sur14-Sep-10 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 4 Aug 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid