【问题标题】:hyperlink in gridview网格视图中的超链接
【发布时间】:2011-08-31 18:42:57
【问题描述】:

我在 gridview 中有一个超链接,我希望用户单击它,它将他们引导到特定页面,并且还传入 gridview 的第一个字段(ID)或将其保存在会话中,最好是在会话中。

该链接只是静态文本,因此无论他们点击什么记录,我都希望将他们带到同一页面,但该记录 ID 可用。

只是不确定如何将其添加到超链接的 NavigateUrl。

感谢任何提示,谢谢

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    您可以轻松地在 GridView 的标记中生成 URL,而无需借助代码。你需要做的是:

    1. 在 DataNavigateUrlFields 中 你的 HyperLinkField 的属性,把 包含的列的名称 你的身份证。
    2. 在 DataNavigateUrlFormatString,把 页面的路径,加上 下一页的查询字符串 用于获取 id,但在哪里 value 应该去,把 {0} 改为。

    例如

    <asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />
    

    在运行时呈现控件时,您会发现对于每一行,{0} 都替换为 ProductId 列的值。

    请参阅String.FormatDataNavigateUrlFormatString 了解更多详情。

    【讨论】:

      【解决方案2】:
      1. 您可以处理 Row_DataBound 事件来查找超链接控件并更新 NavigateUrl 属性。
      2. 您可以添加简单的html控件文本链接,它会产生相同的html。

      【讨论】:

        【解决方案3】:

        使用 HyperLink 控件,然后为 RowDataBound 事件编写一个事件处理函数,如下所示:

        protected void OnRowDataBound(object source, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink hyperLink = e.Row.FindControl("hyperLinkID") as HyperLink;
        
                // example, adjust this to your needs.
                hyperLink.NavigateUrl = "~/detail.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "ID");     
            }
        }
        

        【讨论】:

          【解决方案4】:

          不知道为什么你采取了服务器控制而不是 HTML 标记。

          有两种方法可以做到。

          1) 如果它是静态链接,只需在页面名称前面加上 id 即可。 例如

          <a  href='myPage.aspx<%#Eval("YourID")%>'><strong>Click me to navigate</strong></a>
          

          2) 给 a 标签一些 id 并让它运行在服务器上处理数据绑定事件并将值绑定到它。

           protected void MyGridview_ItemDataBound(object sender, ListViewItemEventArgs e)
              {
          
          
           HtmlAnchor AncImage = e.Item.FindControl("AncImage") as HtmlAnchor;
          AncImage.href="myPage.aspx"/id=" + DataBinder.Eval(e.Row.DataItem, "ID"); ;
          //the id is the value that you want to append for redirection
          }
          

          【讨论】:

            猜你喜欢
            • 2020-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-26
            • 2015-04-28
            • 1970-01-01
            • 2021-11-20
            相关资源
            最近更新 更多