13

看看下面的代码:

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

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

<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"];

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

4

5 回答 5

20

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" />

几个例子:

在 .NET 2.0 Grid-View 的超链接字段中的 DataNavigateUrlFormatString 中传递两个参数

使用 ASP.NET 将多个值从 GridView 传递到另一个页面

编辑:

事件中的一组NavigateUrl超链接RowDataBoundGridView

<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); 
    } 
 } 
}
于 2012-10-10T16:55:50.427 回答
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"]) %>'
于 2012-10-10T15:59:29.257 回答
-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;
       }
       }
      }

谢谢你们的帮助。

于 2012-10-12T16:52:01.800 回答
-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;
        }
于 2013-01-07T19:15:16.237 回答
-2

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

yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
于 2012-10-10T16:01:02.820 回答