onRowCommand gets fired again on refreshing the page - asp.net

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();
}
}

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.

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+ "')")

Related

asp.net open new tab and postback event

My asp.net hat is a bit dusty, so hopefully this will be a simple question.
C# asp.net 4.5
I need a control on my form that will:
open a page in a new tab (not window because its not showing up on mobile browsers)
do a postback to a server-side method. I need the server-side callback because I have to modify some databound data.
I can't seem to get both conditions at the same time.
If I use an asp:Button, I can get the server-side event, but can't get the new page to open in a tab, rather than a window.
If I use an asp:HyperLink, it will open in a new tab, but I can't get a server-side event.
If I use an asp:LinkButton, it will open in the current tab. If I set the target="_blank", then I get an error "'webform_postbackoptions' is undefined"
Questions:
Is there a control available that will do both situations correctly?
If not, for controls with no URL, what code can I use from the server-side to open a new Tab rather than a Window? (I know its usually up to the browser to decide what to open, but somehow the hyperlink control opens properly, so there must be a way!)
OR for controls with no OnClick events, how can I call my server-side function? (from javascript in a client-side event maybe?)
note, any solution that changes the form's target to "_Blank" ruins the functionality of the rest of the page.
Code:
protected void btn_Docsign_Click(object sender, EventArgs e)
{
//Do stuff here
//open window - this always opens a window, not a tab
Page page = (Page)HttpContext.Current.Handler;
String DocusignLink = "myurl.com"
if (page != null)
{
string script = String.Format(#"window.open(""{0}"", ""{1}"", ""{2}"");", DocusignLink, "_Blank", "toolbar=1,menubar=1,resizable=1,scrollbars=1,top=0,left=0");
ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
}
//Response.Redirect(DocusignLink); //opens in current tab
}

Post back and browser back button issue in asp.net

I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column.
Issues
My code is like
if (!Page.IsPostBack)
{
//CODE
}
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE.
How can I execute the CODE , on browser back button click ?
It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
Avoid using button to change page. Generate an hyperlink with the target url.
Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.
Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}

ASP.NET change captcha without page refresh

I've created a simple captcha system in my assp.net web application, I use an ASPX page as ImageUrl of my captcha image, this ASPX file creates captcha image and returns it to be used as my image source. It works fine but I'm going to insert a Change Captcha button which I'd like to change my captcha WITHOUT REFRESH, I've used to methods with no success,
first I tried Ajax Update panel and inserted my change captcha button and my captcha image into it, my captcha didn't change (of course when I use a button outside of update panel to change captcha, it works fine but I have page refresh),
this is my button click code:
protected void Button1_Click(object sender, EventArgs e)
{
imgCaptcha.ImageUrl = "CreateCaptcha.aspx?New=1";
}
then I used a Jquery Ajax call (web method) to call my CaptchaImage.ASPX file and use it as my image source, but again I had no luck! what is going wrong here? in both cases my CaptchaImage.ASPX file is never called! how can I change my captcha image without page refresh?
thanks
$('#btnSearch').click(function () {
var src = //Url to image
$('#image').attr("src", src);
});
Above code changes source of image (#image) used b'coz image is the id for the imagebox.or whatever u using.

is there any method for know on which hyperlink user has clicked in asp.net?

hi actually i want to store value that has been clicked by user on web page for instance.
suppose this is my web page content of list
**
**google.com**
**yahoo.com**
**facebook login**
**stackoverflow.com**
**
now suppose user click on facebook login
then how to know that user has clicked on facebook login actually i want to keep record for further processing.
Abatischev's suggestion will work, but there's an easier method that doesn't involve making AJAX calls if you don't want to go through the hassle.
Instead of having the link go directly to the page that you're linking to, you should have it submit to your asp.net page. You can then record the click there before redirecting to the destination page.
Client-side way:
Before finish the page render, add programmatically onclick event to each hyperlink, call on click an async JavaScript script to record url
Server-side way: (extending #Justin's answer)
<asp:LinkButton runat="server" OnClick="urlGoogle_Click">google.com</asp:LinkButton>
protected void urlGoogle_Click(object sender, EventArgs e)
{
DataBase.Record("google.com");
this.Response.Redirect("http://google.com");
}

Firing Postback in ASP.NET Without javascript

Is this possible?
EDIT
I want to user the asp:ListView
List item
I want to use it for editing
I dont want the postback to use Javascript when I put it into the edit mode
I've been trying to use a to do this instead of a link button but to no avail.
.
The only way to trigger a PostBack without JavaScript is by using a submit button. This is an HTML limitation, not an ASP.NET one.
I'm a little unsure of what you're trying to do without more detail. If I knew more specifically what you're trying to accomplish I could give you more details.
Like you mentioned in your post, you could use the <a> tag to send the user to the page you want.
You can add information to the link like so:
Click here for a new item
Then in the page load of newpage.aspx you can check what the action the user selected was, the query parameters are stored in the Request (Language is C#).
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if(!String.IsNullOrEmpty(action))
{
switch(action)
{
case "newitem":
//handle the new item action
break;
case "deleteitem":
//handle the delete item action
break;
//handle other actions.
}
}
}
EDIT: You should be aware that the <a> tag will send the user to the page specified, the page though will act as if it is the first time the user has visited the page. With that said, the page variable IsPostBack will be false.

Resources