如何动态更改gridview超链接字段 [英] How do I dynamically change a gridview hyperlinkfield
问题描述
我有一个gridview w /一个超链接字段列。我正在寻找一些方法来指定超链接域的NavigateURL,基于gridview中其他列之一的值。例如,如果数据绑定列包含car,则NavigateURL应为〜/ myweb / car.aspx.........同样,如果列包含truck,则NavURL应为〜/ myweb /truck.aspx。
我尝试过:
标记:
< asp:GridView ID =GridView2runat =serverAllowPaging =TrueAutoGenerateColumns =FalseCellPadding =4DataSourceID =SqlDataSource2ForeColor =#333333 GridLines =NoneOnSelectedIndexChanged =GridView2_SelectedIndexChangedOnRowDataBound =GridView2_RowDataBound>
< alternatingrowstyle backcolor =Whiteforecolor =#284775>
<列> < asp:CommandField ShowSelectButton =True>
< HeaderStyle Width =100px/>
< asp :BoundField DataField =Column1HeaderText =AnalysisReadOnly =TrueSortExpression =Column1>
< HeaderStyle Width =120px/>
< asp:BoundField DataField =testdateDataFormatString ={0:d}HeaderText =测试日期ReadOnly =TrueSortExpression =testdate>
< HeaderStyle Width =100px/>
< asp:BoundField DataField =Date OutDataFormatString ={0:d} HeaderText =Date OutReadOnly =TrueSortExpression =Date Out>
< HeaderStyle Width =100px/>
< asp :BoundField DataField =FailureDateDataFormatString ={0:d}HeaderText =失败日期SortExpression =FailureDate/>
< asp:BoundField DataField =sampleidHeaderText = 样品IDReadOnly =TrueSortExpression =sampleid>
< itemstyle horizontalalign =中心>
<% - < asp:HyperLinkField HeaderText =SampleIDDataNavigateUrlFields =SampleIDDataNavigateUrlFormatString =〜/ Reports / LabDetail / ATPDetails?SampleID = {0}DataTextField =SampleIDSortExpression =SampleID/> - %>
< asp:HyperLinkField NavigateUrl =〜/ Reports / LabDetail / ATPDetails.aspxText =查看报告/>
< asp:TemplateField HeaderText =aaaa>
< itemtemplate>
< asp: HyperLink ID =HyperLink1runat =serverNavigateUrl ='〜/ bogus.aspx'>联系
< editrowstyle backcolor =#999999>
< footerstyle backcolor =#5D7B9Dfont-bold =Trueforecolor =White>
< HeaderStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>
< pagerstyle backcolor =#284775forecolor =White horizontalalign =中心>
< rowstyle backcolor =#F7F6F3forecolor =#333333>
< SelectedRowStyle BackColor =#E2DED6 Font-Bold =TrueForeColor =#333333/>
< sortedascendingcellstyle backcolor =#E9E7E2>
< sortedascendingheaderstyle backcolor =# 506C8C>
< sorteddescendingcellstyle backcolor =#FFFDF8>
< sorteddescendingheaderstyle backcolor =#6F8DAE>
代码尝试1:
protected void GridView2_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(row.Cells [1] .Text ==ATP)
{
HyperLink hl = e.Row.FindControl(Hyperlink1)作为HyperLink;
hl.NavigateUrl =〜/ Reports / LabDetail / ATPDetails.aspx;
}
if(row.Cells [1]。文字==水)
{
HyperLink hl = e.Row.FindControl(Hyperlink1)作为HyperLink;
hl。 NavigateUrl =〜/ Reports / LabDetail / WaterDetails.aspx;
}
< br $>
}
代码尝试2:
protected void GridView2_RowDataBound(object sender,GridViewRowEventArgs e)
{
foreach(GridView2.Rows中的GridViewRow行)
{
if(row.Cells [1] .Text ==ATP)
{
HyperLink hl = e .Row.FindControl(Hyperlink1)作为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;
}
}
}
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";
}
}
}
试试这个
try this
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";
}
}
}
最终代码(正常工作)
protected void GridView2_RowDataBound(对象发送者,GridViewRowEventArgs e)
{
foreach(GridView2.Rows中的GridViewRow行)
{
if(row.Cells [1] .Text ==ATP)
{
HyperLink hl = row.FindControl(HyperLink1 )作为HyperLink;
hl.NavigateUrl =〜/ Reports / LabDetail / ATPDetails.aspx;
}
if(row.Cells [1] .Text ==Water)
{
HyperLink hl = row.FindControl(HyperLink1)作为HyperLink;
hl.NavigateUrl =〜/ Reports / LabDetail / WaterDetails.aspx;
}
}
}
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";
}
}
}
这篇关于如何动态更改gridview超链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!