Click here to Skip to main content
15,918,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have gridview w/ a hyperlinkfield column. I am looking for some means of specifying the NavigateURL of the hyperlinkfield, based on the value in one of the other columns in the gridview. For instance, if databound column contains "car", the NavigateURL should be "~/myweb/car.aspx".........likewise, if column contains "truck", the NavURL should be "~/myweb/truck.aspx".

What I have tried:

Markup:
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" OnRowDataBound="GridView2_RowDataBound">
<alternatingrowstyle backcolor="White" forecolor="#284775">
<columns> <asp:CommandField ShowSelectButton="True" >
<HeaderStyle Width="100px" />

<asp:BoundField DataField="Column1" HeaderText="Analysis" ReadOnly="True" SortExpression="Column1">
<HeaderStyle Width="120px" />

<asp:BoundField DataField="testdate" DataFormatString="{0:d}" HeaderText="Test Date" ReadOnly="True" SortExpression="testdate" >
<HeaderStyle Width="100px" />

<asp:BoundField DataField="Date Out" DataFormatString="{0:d}" HeaderText="Date Out" ReadOnly="True" SortExpression="Date Out" >
<HeaderStyle Width="100px" />

<asp:BoundField DataField="FailureDate" DataFormatString="{0:d}" HeaderText="Failure Date" SortExpression="FailureDate" />
<asp:BoundField DataField="sampleid" HeaderText="Sample ID" ReadOnly="True" SortExpression="sampleid">
<itemstyle horizontalalign="Center">

<%--<asp:HyperLinkField HeaderText="SampleID" DataNavigateUrlFields="SampleID" DataNavigateUrlFormatString="~/Reports/LabDetail/ATPDetails?SampleID={0}" DataTextField="SampleID" SortExpression="SampleID"/>--%>

<asp:HyperLinkField NavigateUrl="~/Reports/LabDetail/ATPDetails.aspx" Text="View Report" />
<asp:TemplateField HeaderText="aaaa">
<itemtemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='~/bogus.aspx'>Contact



<editrowstyle backcolor="#999999">
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center">
<rowstyle backcolor="#F7F6F3" forecolor="#333333">
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#E9E7E2">
<sortedascendingheaderstyle backcolor="#506C8C">
<sorteddescendingcellstyle backcolor="#FFFDF8">
<sorteddescendingheaderstyle backcolor="#6F8DAE">


Code Attempt 1:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (row.Cells[1].Text == "ATP")
{
HyperLink hl = e.Row.FindControl("Hyperlink1") as HyperLink;
hl.NavigateUrl = "~/ Reports / LabDetail / ATPDetails.aspx";
}
if (row.Cells[1].Text == "Water")
{
HyperLink hl = e.Row.FindControl("Hyperlink1") as HyperLink;
hl.NavigateUrl = "~/ Reports / LabDetail / WaterDetails.aspx";
}



}

Code Attempt 2:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

foreach (GridViewRow row in GridView2.Rows)
{
if (row.Cells[1].Text == "ATP")
{
HyperLink hl = e.Row.FindControl("Hyperlink1") as HyperLink;
hl.NavigateUrl = "~/ Reports / LabDetail / ATPDetails.aspx";
}
if (row.Cells[1].Text == "Water")
{
HyperLink hl = e.Row.FindControl("Hyperlink1") as HyperLink;
hl.NavigateUrl = "~/ Reports / LabDetail / WaterDetails.aspx";
}
}


}
Posted
Updated 11-Aug-16 8:16am
v2
Comments
Karthik_Mahalingam 11-Aug-16 13:16pm    
what is the issue?
martyres72 11-Aug-16 13:33pm    
the code behind is not updating the NavigateURL.
Karthik_Mahalingam 11-Aug-16 13:35pm    
post the markup code for whole gridview
Karthik_Mahalingam 11-Aug-16 13:47pm    
row.Cells[1].Text
this data belongs to "column1" ??
martyres72 11-Aug-16 13:48pm    
yes.

try this

C#
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               string text = e.Row.Cells[1].Text;
               HyperLink hl = e.Row.FindControl("Hyperlink1") as HyperLink;
               if (hl != null)
               {
                   if (text == "ATP")
                       hl.NavigateUrl = "~/Reports/LabDetail/ATPDetails.aspx";
                   else if (text == "Water")
                       hl.NavigateUrl = "~/Reports/LabDetail/WaterDetails.aspx";
               }
           }
       }
 
Share this answer
 
v2
final code (working)
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

foreach (GridViewRow row in GridView2.Rows)
{
if (row.Cells[1].Text == "ATP")
{
HyperLink hl = row.FindControl("HyperLink1") as HyperLink;
hl.NavigateUrl = "~/Reports/LabDetail/ATPDetails.aspx";
}
if (row.Cells[1].Text == "Water")
{
HyperLink hl = row.FindControl("HyperLink1") as HyperLink;
hl.NavigateUrl = "~/Reports/LabDetail/WaterDetails.aspx";
}

}

}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900