Assigning a cell value (dynamic url?) to NavigateUrl in GridView. Or making relative value/address - clickable/transfarable to sub-page

Eni Nede 21 Reputation points
2021-11-14T16:07:45.077+00:00

GridView allows to create a virtual HyperLinkField column that converts another column assigned to it, (and its cell values) into hyperlinks, but to NavigateUrl must be assigned a "predetermined" url address - however, in my case hyperlink (as a relative address) is generated each time by running a procedure that creates a hyperlink address, and it has form of:
./(directory_with_different_name_each_time_when_the_value_is_generated_by_the_procedure)/index.aspx.
Is it possible to make this value (newly generated relative url) as "clickable" link in GridView - NavigateUrl that by click on it (specific column-every-cell of GridView) to redirect to the newly generated subpage?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
825 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 4,806 Reputation points
    2021-11-14T18:14:44.653+00:00

    If I am reading this correct, you have a GV, and based on the data from database, you want to use code to cook up, create a URL for the hyper link button in the GV based on that data?

    Sure, you can use a hyper link button, or even a plane jane asp.net button in the GV.

    Lets do BOTH cases (button, and a hyper link.

    so, a simple GV - and we drop in a hyper link, and drop in a button into the GV.

    So, our markup looks like this:

    149134-image.png

    Ok, now our code to fill the GV looks like this:

           protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                    LoadGrid();  
            }  
      
            void LoadGrid()  
            {  
                using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))  
                {  
                    using (OleDbCommand cmdSQL = new OleDbCommand("SELECT ID, Animal, PictureName from tblAnimals", conn))  
                    {  
                        conn.Open();  
                        DataTable rstData = new DataTable();  
                        rstData.Load(cmdSQL.ExecuteReader());  
                        GridView1.DataSource = rstData;  
                        GridView1.DataBind();  
                    }  
                }  
            }  
    

    And now we have this output:

    149060-image.png

    so, now we want some code to take information from each data row, and cook up + create a URL link of anything we want based on the row data.

    The code for the hyper link, and also the plane jane button to jump to that new page can thus look like this:

    We use the GV data row binding event for this

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
                if (e.Row.RowType == DataControlRowType.DataRow)  
                {  
                    // get data row  
                    DataRowView gData = (DataRowView)e.Row.DataItem;  
      
                    // example set hyper link  
                    HyperLink myHyper = (HyperLink)e.Row.FindControl("HyperLink1");  
                    // create URL based on row data  
                    myHyper.NavigateUrl = "~/Content/Animals/" + gData["PictureName"];  
      
                    // example buttion click  
                    Button myBtn = (Button)e.Row.FindControl("Button1");  
                    myBtn.CommandArgument = "~/Content/Animals/" + gData["PictureName"];  
                }  
            }  
      
    

    So, note how I wired up both a hyper link based on the row data (could be anything you cook up and imagine).

    And for good measure, I also wire up the plane jane asp button that I also dropped into the GV. As a result, both the hyper link click, or the button click wind up doing much the same thing - navigation to a new URL on click.

    Of couse, for the button click, I shove the url into the command argument - (a handy trick by the way).

    Thus, for the hyper link, we don't have (need) a event stub, but for the button click, we have this code (click) event:

            protected void Button1_Click(object sender, EventArgs e)  
            {  
                Button btn = (Button)sender;  
                Response.Redirect(btn.CommandArgument);  
            }  
    

    so you are quite much free to take the data row information, and based on that information cook up and create any URL you want for that hyper link button, or as the above shows, you can even use a simple button and again, the end result is quite much the same.

    Regards,

    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada