0

In my web page i have a gridview which contains link to download file and link to open another web site in separate page.When i click on link gridview onRowCommand gets executed and website opens in separate window.After this when i refresh the web page, gridview onRowCommand get executed again and opens web site in separate window again which i dont want.I want that when i click on link to navigate to another web site then only it should go.I mean on page refresh, gridview onRowCommand should not get executed.

I am using Page.RegisterStartupScript to open another web site in separate window.

ASP.net 2.0

thanks

-------------------------------------------code----------------

protected void grdDisplayView_onRowCommand(object sender, GridViewCommandEventArgs e) { string path = e.CommandArgument.ToString();

        if (path.Contains("http"))
        {
            StringBuilder sbString = new StringBuilder("<script language='javascript'>");
            sbString.Append("window.open('");
            sbString.Append("http://www.yahoo.com','', 'status=no,width=600,height=500,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,titlebar=yes,top=198,left=305');");
            sbString.Append("</script>");

            if ((!Page.ClientScript.IsStartupScriptRegistered("clientScriptExportView")))
            {
                Page.RegisterStartupScript("clientScriptExportView",sbString.ToString());
            }

        }
        else
        {
            //download file which is locally
            path = Server.MapPath(path);
            System.IO.FileInfo file = new System.IO.FileInfo(path);

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
                HttpContext.Current.Response.ContentType = "Application/octet-stream";
                HttpContext.Current.Response.WriteFile(file.FullName);
                HttpContext.Current.Response.End();


        }

    }
user92027
  • 383
  • 2
  • 7
  • 13

2 Answers2

1

If the user clicks the refresh button, any information sent in the previous request will be sent again. That is to say, if the user clicks a button, and if he presses the refresh button, the button click action will be re-sent to the server.

I believe there is no easy way to distinguish between the original button click, and the button click sent via the page refresh.

This is a classic problem, and can be solved (in a not so easy way) using the Post/Redirect/Get pattern. See here (wiki) / here.

I am not sure if this will solve your problem, but it will hopefully set you out on the right track ! Hope it helps !

See this SO post that describes how to implement the pattern in ASP.NET Web Forms.

Community
  • 1
  • 1
Preets
  • 6,792
  • 12
  • 37
  • 38
  • Thank you for your reply! this is not what i want.I just want to know how to stop execution of gridview onRowCommand event on page refresh if there is a link in gridview to open another website in separate window. Page refresh is like Page load.During page load this event doesn't get executed automatically.why it gets executed on page refresh? If i remove the link which opens new web site in separate window, onRowCommand event doesn't get executed. – user92027 Oct 09 '09 at 11:26
  • Hi, Page Refresh is not the same as page loading the first time. Page refresh is identical to the LAST request that was sent from the browser to the server. If you remove the link then how are you going to click the link in a normal scenario ? The refresh will fire the RowCommand event ONLY IF the last action on your page before refresh was Clicking this Link. If you do not click the link, and refresh the page, then there is no need for row command to fire as it was not fired in the last request. – Preets Oct 12 '09 at 10:10
  • What you can do is create a simple form with two buttons. On the click of the buttons display a simple message box "Button 1" and "Button 2". Now run the application and click the first button. You will see message "Button 1". Now refresh the page. You should AGAIN see the message "Button 1". Now click the second button. You will see message "Button 2". Now refresh the page and you should see "Button 2" displayed. Through this little exercise you can understand how page refresh works. I.e. it copies the last action that look place on your page. Hope this helps:) – Preets Oct 12 '09 at 10:13
0

maybe this is not what you want but i should use:

  <a href="new page" target="_blank" >text</a>

in the gridview itemtemplate

this makes sure that there is no code behind executed :) but i don't know if that's what you want

you can create the link in the ondataitembound event

if you need the code behind you can also use the dataitembound event to add the javascript to for instance an imagebutton to launch a new page and have the code behind of the onclick event:

 imagebutton.Attributes.Add("onclick", "window.open('otherpage.aspx?courseid=" + id+ "')")
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128