繁体   English   中英

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

[英]asp.net Button type link should open on new window

使用c#.net4.0

我知道类型链接的gridview中的asp.net按钮在单击时会在同一页面上发帖,在实际将用户重定向到外部站点之前,我需要在服务器端进行多次操作,因此我无法使用Hyperlinkfield。 我现在需要的是应该在单独的窗口中打开外部站点htm页面。 我尝试了以下可行的方法,但是源站点的字体变大了???

这是我尝试过的

                       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)?????

有没有更好的方法来传递参数?

您需要注册脚本而不是response.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链接到该Generic Handler File ,并将变量作为查询字符串传递。 因此类似:

/MyGenericFile.ashx?id=123

在此文件中,我将需要执行一些脚本,然后执行Response.Redirect()

只要将HyperLink设置为target="_blank" ,用户甚至都不会知道他们曾经使用过通用文件,然后将其重定向。 当他们打开新链接时,它将显示。

因此,该过程将是:

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

我相信广告管理系统会使用相同的过程来帮助跟踪点击次数。

您可以使用ClientScriptManager.RegisterStartupScript注册脚本以在页面加载时运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM