简体   繁体   English

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

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

using c# .net4.0 使用c#.net4.0

I am aware the asp.net button with in the gridview of type link does a post to the same page when clicked, i need make several manipualtion on server side before actually redirecting user to an external site hence i can't use Hyperlinkfield. 我知道类型链接的gridview中的asp.net按钮在单击时会在同一页面上发帖,在实际将用户重定向到外部站点之前,我需要在服务器端进行多次操作,因此我无法使用Hyperlinkfield。 What i need now is the external site htm page should open up in sperate window. 我现在需要的是应该在单独的窗口中打开外部站点htm页面。 I tried the following which works but source site's fonts get bigger??? 我尝试了以下可行的方法,但是源站点的字体变大了???

heres what i tried 这是我尝试过的

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

may be i need a refresh source site?? 可能我需要刷新源站点吗?

Thanks 谢谢

@ Curt Here is the code for Hyperlink i tired @ Curt这是我累的超链接代码

on page load added new button on gridview 页面加载在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)?????

is there a better way to pass parameters ??? 有没有更好的方法来传递参数?

you need to register script not response.write 您需要注册脚本而不是response.write

so the code for you is : 所以您的代码是:

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

Read more : ClientScriptManager.RegisterStartupScript . 阅读更多: ClientScriptManager.RegisterStartupScript

In a situation where I need to run server side code, before then opening a new page, I sometimes create a Generic Handler File and link to this with a HyperLink, passing variables as Query Strings. 在需要运行服务器端代码的情况下,在打开新页面之前,有时会创建一个Generic Handler File并使用HyperLink链接到该Generic Handler File ,并将变量作为查询字符串传递。 Therefore something like: 因此类似:

/MyGenericFile.ashx?id=123

In this file, I would have some scripting that needs to be carried out, followed by a Response.Redirect() . 在此文件中,我将需要执行一些脚本,然后执行Response.Redirect()

As long as the HyperLink is set to target="_blank" , the user won't even know they've been to a generic file, which is then redirected. 只要将HyperLink设置为target="_blank" ,用户甚至都不会知道他们曾经使用过通用文件,然后将其重定向。 It will appear as they've opened a new link. 当他们打开新链接时,它将显示。

Therefore the process would be: 因此,该过程将是:

  • User clicks link to .ashx file 用户单击指向.ashx文件的链接
  • Link opens in new window 链接在新窗口中打开
  • Necessary scripting is ran 必要的脚本已运行
  • Response.Redirect() is ran Response.Redirect()已运行
  • User is taken to web page ( www.google.com in your example) 用户被带到网页(在您的示例中为www.google.com

I believe this same process is used by advert management systems to help track clicks. 我相信广告管理系统会使用相同的过程来帮助跟踪点击次数。

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

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

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