C#中的GridView超链接字段

编程入门 行业动态 更新时间:2024-10-12 05:50:22
本文介绍了C#中的GridView超链接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

看看下面的代码:

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

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

It takes only the name id to navigate to the next page. How will I include the two other parameters which are not in the gridview. The navigate URL I'm using has to take the keyword which is already present in the gridview and the other two parameters from the database table. I tried using all these codes. Nothing did work for me.

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

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

I cant use the above the code because the state and city are not in the GridView but available in my data table.

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

I tried using the following code too, but it doesn't work:

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

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

I don't know how to solve this.

推荐答案

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

Use the DataNavigateUrlFields property, comma-delimited value with the fields for parameters in "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 传递到另一个页面

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); 
    } 
 } 
}

这篇关于C#中的GridView超链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:48:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   超链接   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!