Browser crash detection server side c# - c#

As when browser or page crashes I need to detect it in the server-side what I actually found is this solution so far when the browser closes it reads as below.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication2010.WebForm3" %>
<%# Register Assembly="AjaxControlToolkit, Version=3.0.30512.17815, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Handling Browser Close Event at Server Side: ASP.NET</title>
</head>
<body onunload="deleteRecord();">
<script type="text/javascript" language="javascript">
function deleteRecord() {
//this method will call server-side method
PageMethods.deleteRecord();
}
</script>
<form id="form1" runat="server">
<div id="div1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Click on Close Button
</div>
</form>
</body>
</html>
This is the client page above and the server code is below written.
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static void deleteRecord()
{
// You can write any logic you want to perform when the browser is getting closed
System.Diagnostics.Debug.Write("You can write your logic here");
}
Here is the excerpt which I used - https://www.dotnetspider.com/resources/30715-Handling-Browser-Close-Event-at-Server-Side.aspx
Any suggestions might help out in this browser crash context.

Check the comments below the post
Not only onunload will be called on browser closed, It will also be called whenver postback your page.
Therefore the deleteRecord() method will be called for all postback.
Also onunload will be fired twice when you click link buttons.
You can also not be sure that anything will run on crash. If an exception is thrown on the browser, there is no crash event that is raised. More often than not a crash will stop the javascript altogether, so no ajax will run.
Best bet for client side errors is to use something like application insights to send error telemetry on javascript errors.
Check client side monitoring here: https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net#add-client-side-monitoring

Related

AjaxFileUpload & asyncfileupload both causing full page postback

I have googled and have search this forum, i can not find solution to my problem. As stated by the title in my webforms project using vs 2015 both these mentioned controls are causing a full page post back after uploading is done. In my other projects this works fine. But in this particular one i cant figure it out. Is there just a common thing I am over looking, I have created fresh pages with out using the master-page I still get the same results - full post back. I did not post my code because i am using the controls as any other programmer would.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pic1.aspx.cs"
Inherits="SSWebAppV1._1.pic1" %>
<%# Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
Width="500px" AllowedFileTypes="jpg,jpeg,png" MaximumNumberOfFiles="4"
OnUploadComplete="AjaxFileUpload1_UploadComplete" />
</div>
</form>
</body>
</html>
****code-behind***
protected void AjaxFileUpload1_UploadComplete(object sender,
AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string fileName = Path.GetFileName(e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath("~/Eimages/" + fileName));
}

What do I have to do to get my ASP.NET MasterPage to work on my server?

I followed some examples in my ASP.NET 4.5 in C# book for Master Pages. So I made a more complex site using them, and it all works locally. But when I push it to my remote server, it is saying the file cannot be found. So I made a more basic master page in Visual Studio 2015 to check. And it works locally, but when pushed to the remote server, it still does not work. I couldn't find any answers online either. I've tried some different urls thinking I was using the wrong url too. I've used
myWebSite.com/TestFolder/MasterPage.master
myWebSite.com/TestFolder/
myWebSite.com/TestFolder/MasterPage
So I'm not sure what I'm doing wrong. Any help would be greatly appreciated.
The code for my master page looks like this:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Tester</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<h1>This is a test of the master page.</h1>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
I fixed it. I made sure that the file path to the CodeFile in the header was "./MasterPage.master.cs" and the MasterPageFile reference in the header of the content page was "./MasterPage.master" not "~/MasterPage.master".
The url also worked just as myWebSite.com/
Thank you for the help anyways. Much appreciated.

How to put loading image when something is processing?

I am having page in that when we click on create button its sends web request to get the all links but mean while it takes some time to get all links working or not so at that time I want to put image load
Button ID="btnRender" runat="server" Text="Page Render" OnClick="btnRender_Click" />
<asp:Button ID="btn_submit" runat="server" Text="Submit" OnClientClick="javascript:finda();" />
<asp:Button ID="btn_createlink" runat="server" Text="Create link" OnClick="btn_createlink_Click" />
i want to call on btn_createlink_Click" button when user clicks the image or text should appear
Without Ajax Tool:
This will call the loading image on button click and hide the image on page load completes.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function ShowProgressBar() {
document.getElementById('dvProgressBar').style.visibility = 'visible';
}
function HideProgressBar() {
document.getElementById('dvProgressBar').style.visibility = "hidden";
}
</script>
</head>
<body onload="javascript:HideProgressBar()" >
<form id="form1" runat="server">
<div>
<div style="float:left;">
<asp:Button ID="btn_createlink" runat="server" Text="Create link" OnClick="btn_createlink_Click" OnClientClick="javascript:ShowProgressBar()" />
</div>
<div ID="dvProgressBar" style="float:left;visibility: hidden;" >
<img src="/images/progress_bar.gif" /> resolving address, please wait...
</div>
<br style="clear:both" />
</div>
</form>
</body>
</html>
You can make this happen with the help of Multithreading with the BackgroundWorker Component.
Have a look on Multithreading with Backgroundworker
Backgroundworker
C# backgroundworker
and this
You can call your loading.gif image in _Dowork method. Hope this helps.
EDIT:
You might want to have a look at HttpResponse.Flush
Forces all currently buffered output to be sent to the client. The Flush method can be called multiple times during request processing.
See the following link: HttpResponse.Flush Method
You could show a loading image and call this method every X number of loaded links.
OLD:
Since the question is missing information, I will assume that you are doing an Ajax call in order to get a collection of links from the server and update a table.
I would suggest you to use jQuery and follow this previous answer on SO in order to display an animated loading spinner when a request is made and hide it when it is completed.
See the link: Jquery Ajax Loading image

Javascript Unable to GetElementById in ASP.NET C#

I know this is a very common problem. I was trying to get a "enter key" event to invoke a click on a certain button on the page through javascript. However, I can't for the life of me fetch a single element by its id or name from my asp.net page.
It is my understanding that this issue is related to where the javascript is located and whether the elements have been rendered by the time the javascript is loaded or something of that nature?
Every time I attempt to use var x = document.getElementById('btn_AddAdmin') I end up with a null value.
My asp.net page has SiteMaster page that it inherits from. I have included a trimmed down version of my asp.net page and the SiteMaster page.
ASP.NET Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="HERMES.Admin" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<input type="text" id="txt_AddAdmin" runat="server" onkeypress="Populate()" />
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs"
Inherits="HERMES.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="./Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
</form>
</body>
Sorry for the formatting, I seem to be doing something wrong...
I have attempted placing the javascript in many different locations -- anywhere from the sitemaster page to the top and bottom of my asp.net page. No luck either way.
var x = document.getElementById('<%= btn_AddAdmin.ClientID %>');
If your button is a Server Control it prepends a naming scheme to the input button so it can be bound back to the appropriate server control during postback. Thus the need to use the ClientID property to retrieve the "rendered id".
To learn more visit:
http://www.asp.net/web-forms/overview/client-script,-jquery,-and-ajax
http://www.asp.net/
Google it with Bing:
clientid
aspnet webforms client side programming

ASP.NET page should not post back the whole page on Submit

I have an ASP.NET page with a Submit button. When I click the button my code runs and then the page reload/refresh....the whole page gets posted back.
What must I use or do to prevent the page from reload? AJAX? How would this be done?
Code examples would be great!
Check the below code with AJAX and Without AJAX
Case 1 With AJAX
In this we added following controls
1. Script Manager
2. Ajax Update Panel with Trigger and Content Template
3. Button
In this case, you will notice that on clicking the button, the controls which are outside the Update Panel will not be updated. That means controls inside the Update Panel will be updated on clicking the button. That means complete page will not be refreshed. I hope this fulfill your requirement....
Output
Date outside the update panel will not be updated and date inside the update panel will only be updated because it is inside the update panel so complete page will not be refreshed.
Source Code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now %>
<br />
<asp:ScriptManager ID="scr" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="Upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%=DateTime.Now %>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="btn" runat="server" Text="Click Me" />
</div>
</form>
</body>
</html>
Case 2 Without AJAX
The below case will also update the date but this time page will be refreshed completely due to absence of Update Panel
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now %>
<asp:Button ID="btn" runat="server" Text="Click Me" />
</div>
</form>
</body>
</html>
Hope this article will help you understand the quick basics
If you don't want the page to reload, you should take a look at the UpdatePanel Control: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx
If you want to avoid that your code is run again
you can do in your page_load event
If(!page.IsPostBack())
{
your code here
}
and ajax to avoid reload in jquery
with the example here
or with asp ajax panel

Categories