Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to create a new GridView column that contains a dynamic URL?

How to create a new GridView column that contains a dynamic URL?

Scheduled Pinned Locked Moved ASP.NET
sysadminhelptutorialquestion
4 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pzn3xq
    wrote on last edited by
    #1

    Hi I have a gridview displaying the northwind Orders table. I'd like to add a new column called "Order Details" which is a hyperlink to the order details of the order record. I created the column and added the following code...but I don't know how to create my NavigateURL dynamically based on the primary key of the order selected. "GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="dsOrders"> "True"> "OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID"> "OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"> "RequiredDate" HeaderText="RequiredDate" SortExpression="RequiredDate"> "ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate"> "Order Details"> "dsOrders" runat="server" ContextTypeName="NorthwindDataContext" TableName="Orders"> The problem with the code below is I don't know how to identify the order id, and how to convert the text into an actual hyperlink value instead of static text. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[5].text = "http://mysite/orderdetail.aspx?OrderId=?"; } }

    _ M 2 Replies Last reply
    0
    • P pzn3xq

      Hi I have a gridview displaying the northwind Orders table. I'd like to add a new column called "Order Details" which is a hyperlink to the order details of the order record. I created the column and added the following code...but I don't know how to create my NavigateURL dynamically based on the primary key of the order selected. "GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="dsOrders"> "True"> "OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID"> "OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"> "RequiredDate" HeaderText="RequiredDate" SortExpression="RequiredDate"> "ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate"> "Order Details"> "dsOrders" runat="server" ContextTypeName="NorthwindDataContext" TableName="Orders"> The problem with the code below is I don't know how to identify the order id, and how to convert the text into an actual hyperlink value instead of static text. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[5].text = "http://mysite/orderdetail.aspx?OrderId=?"; } }

      _ Offline
      _ Offline
      _AK_
      wrote on last edited by
      #2

      I didn't get the complete code which you have posted. But from your question what I can suggest is to have a template column in the gridview and then in the gridview rowdatabound event just find that control and then set the navigate URL property of it. The rowdatabound event gets fired for all the rows in the gridview. So this will suffice the need.

      Apurva Kaushal

      1 Reply Last reply
      0
      • P pzn3xq

        Hi I have a gridview displaying the northwind Orders table. I'd like to add a new column called "Order Details" which is a hyperlink to the order details of the order record. I created the column and added the following code...but I don't know how to create my NavigateURL dynamically based on the primary key of the order selected. "GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="OrderID" DataSourceID="dsOrders"> "True"> "OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID"> "OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"> "RequiredDate" HeaderText="RequiredDate" SortExpression="RequiredDate"> "ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate"> "Order Details"> "dsOrders" runat="server" ContextTypeName="NorthwindDataContext" TableName="Orders"> The problem with the code below is I don't know how to identify the order id, and how to convert the text into an actual hyperlink value instead of static text. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[5].text = "http://mysite/orderdetail.aspx?OrderId=?"; } }

        M Offline
        M Offline
        meeram395
        wrote on last edited by
        #3

        Anyway you are getting the value of OrderId in one of the cells. So get that value in an integer variable like below: int ordid = e.Row.Cells[5].Text Probably you need a conversion here to integer. Then, for making dynamic hyperlink columns, check in this site : CreateHypeLink Fields Dynamically

        Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

        P 1 Reply Last reply
        0
        • M meeram395

          Anyway you are getting the value of OrderId in one of the cells. So get that value in an integer variable like below: int ordid = e.Row.Cells[5].Text Probably you need a conversion here to integer. Then, for making dynamic hyperlink columns, check in this site : CreateHypeLink Fields Dynamically

          Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

          P Offline
          P Offline
          pzn3xq
          wrote on last edited by
          #4

          So here is my confusion...some people say add an itemTemplate field with a hyperlink, and others claim to simply a new hyperlink column and set the DataNavigateURLFields, and DataNavigateURLFormatString manually. I didn't have any luck with using the ItemTemplate field....so I added a hyperlink column instead below. Are there any advantages over using an ItemTemplate vs hyperlink column??? DataNavigateUrlFields="OrderId, CustomerId" DataNavigateUrlFormatString="HyperLinkOrderDetails.aspx?aOrderID={0}&CustomerId={1}" HeaderText = "Order Details" Text="View order details" />

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups