【问题标题】:GridView HyperLink field in C#C# 中的 GridView 超链接字段
【发布时间】:2012-10-01 03:57:40
【问题描述】:

看看下面的代码:

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

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

【问题讨论】:

    标签: c# gridview hyperlink


    【解决方案1】:

    使用DataNavigateUrlFields 属性,逗号分隔的值与"KeywordSrchSumDtl.aspx?Keyword={0}&amp;State={1}&amp;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

    编辑:

    GridViewRowDataBound事件中设置超链接的NavigateUrl

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

    【讨论】:

    • 在所选数据源上找不到名为“State”的字段或属性。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.Web.HttpException:在所选数据源上找不到名为“State”的字段或属性。源错误:第 40 行:dv = bindGridView();第 41 行:GridView1.DataSource = dv;第 42 行:GridView1.DataBind();第 43 行:第 44 行:}
    • STATE 和 CITY 不在 gridview 中。
    • 但如果这些字段不存在于表 ryt 中,它将不起作用??
    • 只有关键字,如果你看后面的代码你会发现其他参数的值实际上来自请求对象。
    • 我不知道它没有采用其他两个参数。这就是它的重定向方式 --> reliable/folder2/KeywordSrchSumDtl.aspx?Keyword=ups &State=&City=
    【解决方案2】:

    你可以试试string.Format方法

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

    【讨论】:

    • 您的代码没有将关键字列绑定到表。所以我添加了 现在它绑定了。但没有显示输出.. 谢谢
    • 是的……!!我得到了输出.. 谢谢.. :)
    【解决方案3】:

    最后它通过以下代码导航,

    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】:
      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;
              }
      

      【讨论】:

        【解决方案5】:

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

        yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-13
          相关资源
          最近更新 更多