C#中的GridView HyperLink字段

问题描述 投票:9回答:5

看看下面的代码:

<asp:HyperLinkField 
    DataNavigateUrlFields="NameID" 
    DataNavigateUrlFormatString="names.aspx?nameid={0}"
    DataTextField="name" 
    HeaderText="Name" 
    ItemStyle-Width="100px"
    ItemStyle-Wrap="true" />

它只需要名称id即可导航到下一页。如何包含不在gridview中的其他两个参数。我正在使用的导航URL必须采用gridview中已存在的关键字和数据库表中的其他两个参数。我尝试使用所有这些代码。没有什么对我有用。

<asp:HyperLinkField DataTextField="Keyword" DataNavigateUrlFields="Keyword"
    DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
    HeaderStyle-VerticalAlign="Bottom" ItemStyle-HorizontalAlign="center" />

我不能使用上面的代码,因为状态和城市不在GridView中,但在我的数据表中可用。

我也尝试使用以下代码,但它不起作用:

 <asp:TemplateField HeaderText="Keyword"  ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:HyperLink ID="link" runat="server" NavigateUrl='<% # "KeywordSrchSumDtl.aspx?Keyword="Eval("Keyword")+"&State="+Request.QueryString["State"]%>' Text='<%# Eval("Keyword") %>'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

我也试过这个:

        <asp:HyperLink ID="Link1" runat="Server" NavigateUrl='<%#redirectURL()+Server.UrlEncode((Eval("Keyword")).ToString())%>' Text='<%# DataBinder.Eval(Container.DataItem,"Keyword") %>'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

.aspx.cs

return "KeywordSrchSumDtl.aspx?Keyword=" + 
    //I DONNO HOW TO CALL THE KEYWORD HERE//
    + "&State=" + System.Web.HttpContext.Current.Request.QueryString["State"]
    + "&City=" + System.Web.HttpContext.Current.Request.QueryString["City"];

我不知道如何解决这个问题。

c# gridview hyperlink
5个回答
18
投票

使用DataNavigateUrlFields属性,逗号分隔值以及"KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"中参数的字段

<asp:HyperLinkField DataNavigateUrlFields="Keyword,State,City"
                    DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}" 
                    Text="View Details" />

几个例子:

Passing two arguments in DataNavigateUrlFormatString in hyperlink field of .NET 2.0 Grid-View

Pass Multiple Values from a GridView to Another Page using ASP.NET

编辑:

NavigateUrlRowDataBound事件中设置HyperLink的GridView

<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataKeyNames="Keyword"
              DataSourceID="SqlDataSource1" 
              onrowdatabound="GridView1_RowDataBound">
   <asp:TemplateField HeaderText="Keyword"  ItemStyle-HorizontalAlign="Center"          FooterStyle-HorizontalAlign="Center">
      <ItemTemplate>
          <asp:HyperLink ID="link" runat="server" Text='<%# Eval("Keyword") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    .......
</asp:GridView>

代码背后:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
 if (e.Row.RowType == DataControlRowType.DataRow) 
 { 
    HyperLink hl = (HyperLink)e.Row.FindControl("link"); 
    if (hl != null) 
    { 
        DataRowView drv = (DataRowView)e.Row.DataItem; 
        string keyword = drv["Keyword"].ToString(); 
        string state = Request.QueryString["State"]; 
        string city = Request.QueryString["City"]; 
        hl.NavigateUrl = "~/KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&State=" + Server.UrlEncode(state) + "&City=" + Server.UrlEncode(city); 
    } 
 } 
}

1
投票

您可以尝试使用string.Format方法

NavigateUrl='<%# String.Format("KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}", DataBinder.Eval(Container.DataItem, "Keyword"), Request.QueryString["State"], Request.QueryString["City"]) %>'

-1
投票

最后通过以下代码导航,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
 if (e.Row.RowType == DataControlRowType.DataRow) 
 { 
      HyperLink hl = (HyperLink)e.Row.FindControl("Link");
      if (hl != null)
      {
           DataRowView drv = (DataRowView)e.Row.DataItem;
           string keyword = drv["Keyword"].ToString().Trim();
           string state = strState.ToString().Trim();
           string city = strCity.ToString().Trim();                 
           hl.NavigateUrl = "KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&Geo=" + geo                             + "&Site=" + site;
       }
       }
      }

谢谢你们的帮助。


-1
投票
Some time we need to pass multiple parameters with hyperlink in Gridview, datagrid or any data list control then we can use following code:-</br> 

**CODE:-** 
<asp:GridView ID="gvFin" runat="server" CellPadding="1" AutoGenerateColumns="false"> 

<Columns>
     <asp:TemplateField ItemStyle-Width="4%" HeaderStyle-Width="4%" SortExpression="CDL"
    HeaderText="CDL#" HeaderStyle-Font-Bold="true">
                                                <ItemTemplate>
   <asp:HyperLink ID="lnk1" runat="server" 
   Text='<%# DataBinder.Eval(Container.DataItem,"TestValue") %>'
   NavigateUrl='<%# "javascript:ShowACP(\"" + DataBinder.Eval(Container.DataItem, "ID")     + "\",\"" + DataBinder.Eval(Container.DataItem,"ACCOUNTPLAN") + "\");" %>'                                                  ForeColor="Blue" /  </ItemTemplate>
 </asp:TemplateField>


**JavaScript Function**

function ShowACP(id, acplabel) 
{            if (acplabel == "No")
{
                window.location = "#";
            }
            else</br> 
                window.location = "Default.aspx?gid=" + id;
        }

-2
投票

您可以使用string []从Code Behind初始化DataNavigateUrlFields:

yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
© www.soinside.com 2019 - 2024. All rights reserved.