Gauls
Gauls

Reputation: 1965

asp.net Button type link should open on new window

using 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. What i need now is the external site htm page should open up in sperate window. 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

on page load added new button on 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 ???

Upvotes: 0

Views: 3279

Answers (3)

jrummell
jrummell

Reputation: 43087

You can register a script to run on page load with ClientScriptManager.RegisterStartupScript.

Upvotes: 0

Curtis
Curtis

Reputation: 103368

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. 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().

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. It will appear as they've opened a new link.

Therefore the process would be:

  • User clicks link to .ashx file
  • Link opens in new window
  • Necessary scripting is ran
  • Response.Redirect() is ran
  • User is taken to web page (www.google.com in your example)

I believe this same process is used by advert management systems to help track clicks.

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176916

you need to register script not 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.

Upvotes: 3

Related Questions