【问题标题】:go to external site if it's an hyperlink如果是超链接,请转到外部站点
【发布时间】:2009-08-22 04:45:54
【问题描述】:

我在每一行的gridview 中都有一个指向aspx 页面的“视图”链接。

根据资源的类型 1)File 或 2)Hyperlink ,它应该下载文件或转到提到的超链接。

<asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <a id="View" href="../resources/ResourceFile.aspx?Id=<%# Eval("Id")%>" target="_blank">View</a>
                </ItemTemplate>
    </asp:TemplateField>

我让它适用于文件类型,但是如果它是一个超链接,我如何重定向到像“www.yahoo.com”这样的外部链接。

在后面的代码中

if(resource.ResourceType.ToLower().Equals("hyperlink")){
                    // what should i do here?
               // the link is stored in resource.value
                }

编辑:认为链接应该有一个 http:// 前缀才能工作。现在感觉很蠢:)

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    认为链接应该有一个 http:// 前缀才能工作。

    【讨论】:

      【解决方案2】:

      资源对象是否有权访问您希望重定向到的 URL?如果是这样,您可以使用 Response.Redirect。

      if(resource.ResourceType.ToLower().Equals("hyperlink")){
         Response.Redirect(resource.Url);
      }
      

      【讨论】:

      • 很好,然后是 Response.Redirect(resource.value);应该这样做。 :)
      • @大卫。发现该值应该有一个 http:// 前缀。这就是为什么它之前对我不起作用的原因。感谢您的帮助。
      【解决方案3】:

      为项目模板添加一个 asp.net 超链接。然后处理 RowDataBound 事件以动态更改超链接的 NavigateURL 属性。这样可以避免邮包。

      <asp:TemplateField HeaderText="View">
          <ItemTemplate>
              <asp:Hyperlink runat="server" id="View" target="_blank">View</a>
          </ItemTemplate>
      </asp:TemplateField>
      
      void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if(e.Row.RowType == DataControlRowType.DataRow)
          {
              HyperLink hl = (HyperLink)e.Row.FindControl("View");
              hl.NavigateUrl = "Link to file or url based on resource type";
          }
      }
      

      [http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx][1]

      [1]:MSDN 示例

      【讨论】:

      • 我在其他几个地方使用了resourcefile.aspx。我将逻辑放在该页面后面的代码中以确定它是文件还是url。无论是文件还是url,链接都是一样的
      猜你喜欢
      • 1970-01-01
      • 2013-06-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2017-12-07
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多