asp.net按钮类型链接应该在新窗口中打开

asp.net Button type link should open on new window

本文关键字:在新窗口中打开 链接 net 按钮类型 asp      更新时间:2023-09-26

使用c#.net4.0

我知道类型链接的网格视图中的asp.net按钮在点击时会发布到同一页面,在实际将用户重定向到外部网站之前,我需要在服务器端进行多次操作,因此我不能使用超链接字段。我现在需要的是外部网站htm页面应该在sperate窗口中打开。我尝试了以下操作,但源网站的字体变大了???

这是我试过的

                       Response.Write("<script>");
                       Response.Write("window.open('http://www.google.co.uk','_blank')");
                       Response.Write("</script>");
                       Response.End();

我可能需要一个刷新源网站吗??

感谢

@Curt这是超链接的代码我厌倦了

页面加载时在gridview 上添加了新按钮

HyperLinkField LinksBoundField = new HyperLinkField();          
        string[] dataNavigateUrlFields = {"link"};
        LinksBoundField.DataTextField = "link";
        LinksBoundField.DataNavigateUrlFields = dataNavigateUrlFields;
        LinksBoundField.DataNavigateUrlFormatString = "http://" + Helper.IP + "/" + Helper.SiteName + "/" + Helper.ThirdPartyAccess + "?dispage={0}&token=" + Session["Token"]; 
        LinksBoundField.HeaderText = "Link";
        LinksBoundField.Target = "_blank";           
        GridViewLinkedService.Columns.Add(LinksBoundField);
        GridViewLinkedService.RowDataBound += new GridViewRowEventHandler(grdView_RowDataBound);

to append external values (refe and appid) to navigate url

       protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                string strvalue = "";
                string strvalue1 = "";
                string strRef = "";
                string strAppId = "";
                foreach (GridViewRow row in GridViewLinkedService.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                   {
                        //reference and appid
                        strAppId = row.Cells[0].Text;
                 strRef = row.Cells[1].Text;
            HyperLink grdviewLink = (HyperLink)row.Cells[5].Controls[0];
             strvalue = grdviewLink.NavigateUrl;
     strvalue1 = Regex.Replace(strvalue, "(.*dispage''=).*/(services.*)", "$1$2");
         grdviewLink.NavigateUrl = "~/My Service/FillerPage.aspx?nurl=" + strvalue1 + "&AppID=" + strAppId.ToString() + "&Ref=" + strRef.ToString();

                  }
               }
            }
 public partial class FillerPage : System.Web.UI.Page
    {
       private string refno = null;
       private string appid = null;
       private string nurl = null;
       private string strvalue1 = "";
       private string newtoken = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.GetValues("AppID") != null)
            {
                appid = Request.QueryString.GetValues("AppID")[0].ToString();
            }
            if (Request.QueryString.GetValues("Ref") != null)
            {
                refno = Request.QueryString.GetValues("Ref")[0].ToString();
            }
            if (Request.QueryString.GetValues("nurl") != null)
            {
                nurl = Request.QueryString.GetValues("nurl")[0].ToString();
            }
        while receiving the long url it gets messed up(same query multiple times and all jumbled up)?????

有更好的方法传递参数吗???

您需要注册脚本而不响应.write

所以你的代码是:

ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>window.open('http://www.google.co.uk','_blank')</script>");

阅读更多:ClientScriptManager.RegisterStartupScript.

在需要运行服务器端代码的情况下,在打开新页面之前,我有时会创建一个Generic Handler File,并通过HyperLink链接到它,将变量作为查询字符串传递。因此类似于:

/MyGenericFile.ashx?id=123

在这个文件中,我将有一些需要执行的脚本,然后是Response.Redirect()

只要HyperLink设置为target="_blank",用户甚至不会知道他们已经访问了一个通用文件,然后会重定向该文件。它将在他们打开新链接时显示。

因此,该过程将是:

  • 用户单击指向.ashx文件的链接
  • 链接在新窗口中打开
  • 运行必要的脚本
  • 运行Response.Redirect()
  • 用户被带到网页(在您的示例中为www.google.com

我相信广告管理系统也使用同样的流程来帮助跟踪点击。

您可以使用ClientScriptManager.RegisterStartupScript.

注册脚本以在页面加载时运行