C#中的GridView HyperLink字段

时间:2012-10-10 15:50:03

标签: c# gridview hyperlink

看看以下代码:

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

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

5 个答案:

答案 0 :(得分: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

修改

NavigateUrl <{1}}

RowDataBound事件中设置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 :(得分: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"]) %>'

答案 2 :(得分:-1)

您可以使用字符串[]

从Code Behind初始化DataNavigateUrlFields

答案 3 :(得分:-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;
       }
       }
      }

感谢各位帮忙。

答案 4 :(得分:-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;
        }