How to create a new GridView column that contains a dynamic URL?
-
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=?"; } }
-
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=?"; } }
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
-
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=?"; } }
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.
-
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.
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" />