Javascript function and web service call synchronization - javascript

Hi i have this code in page_load event on aspx page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillSomeDropDownLists();
}
EnableExportButton(false);
if (!string.IsNullOrEmpty(Page.Request["__EVENTTARGET"]) && !string.IsNullOrEmpty(Page.Request["__EVENTARGUMENT"]) && Page.Request["__EVENTARGUMENT"] == "true")
{
ScriptManager.RegisterStartupScript(Page, GetType(), "Show", "ShowPopup();", true);
if (CallToWebService(Convert.ToInt32(Page.Request["__EVENTTARGET"]), 1))
{
SomeOtherCallToWebService();
Global.ShowMessage(this.Page, StringConstants.SUCCESS_TRANSACTION_TITLE);
}
else
{
Global.ShowMessage(this.Page, StringConstants.FAIL_TRANSACTION_TITLE);
}
ScriptManager.RegisterStartupScript(Page, GetType(), "Hide", "HidePopup();", true);
}
}
I would like to show a javascript popup, then do webservice related stuff, and then hide the popup. That is how I coded it.
But in page execution I get call to webservices first, and then shows and hides the popup. Showing and hiding the popup is so fast that it seems nothing happens.
Is there solution to get what I want?

Related

How can I change cursor to wait icon in Visual Studio asp.net?

I have a Web Forms project in VS 2013 in which I call an asynchronous process from a button within an update panel. I would like the wait icon to show while the asynchronous process is running.
I am using the code- behind shown below, but the cursor does not change from the default pointer when cmdAutoCaption is clicked. However, after the asynchronous process has completed and content_string returned, the cursor changes to a wait icon if it is moved outside the update panel.
protected void cmdAutoCaption_Click(object sender, EventArgs e)
{
string sUser = AuthoriseUser();
if (sUser == "") return;
string script1 = "document.body.style.cursor = 'wait';";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script1, true);
CreateAutoCaption();
}
private async void CreateAutoCaption()
{
await MakeAnalysisRequestAsync(Session["strImagePath"].ToString());
}
private async MakeAnalysisRequestAsync(string imageFilePath)
{
...
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
...
string script1 = "document.body.style.cursor = 'auto';";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script1, true);
...
}
You can use the following code to change the cursor:
Cursor.Current = Cursors.WaitCursor
This should solve your problem.

update label.text multiple times during button click event in asp.net

I want to show the processing of the code,which runs on button click event.
Please help.
My code is something like below.
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Do some stuff
//Show the message that the application is processing
lblCaption.Text = "Processing...updating label data";
//Do more stuff
lblCaption.Text = "Processing...creating label files.";
//More stuff that will take some time
lblCaption.Text = "Processing...updating label counts.";
//More stuff
lblCaption.Text = "Processing...completed.";
}

Redirect to a page after a pop up opens

I am trying to redirect a web page after a condition is returned as true but I can't seem to get it work. In theory this should, shouldn't it. What am I missing, is it even possible!
protected void btnVerify_Click(object sender, EventArgs e)
{
if (value == txtVerification.Text || txtVerification.Text == "****")
{
//defines a bool to tell if the popup window has been shown, this will only ever return true
bool PopupShown = doRedirect();
if(PopupShown)
{
Response.Redirect("somewebpage.aspx");
}
}
else
{
lblVerificationFailed.Visible = true;
}
}
//Opens the popup window to fire off the download and returns true
bool doRedirect()
{
string url = "GetDocs.aspx";
string s = "window.open('" + url + "', 'GetDocs', 'height=150,width=300,left=100,top=30,resizable=No,scrollbars=No,toolbar=no,menubar=no,location=no,directories=no, status=No');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
return true;
}
You are trying to do in the server things that can be much more easily done on the client side.
You're using a server event to catch the click of a button on your view, launch a client popup and later redirect your page execution.
Try with something like this on javascript:
var btnVerify = document.getElementById("btnVerify");
btnVerify.addEventListener("click", function() {
window.open('GetDocs.aspx', 'GetDocs', 'height=150,width=300,left=100,top=30,resizable=No,scrollbars=No,toolbar=no,menubar=no,location=no,directories=no, status=No');
window.location.href = "somewebpage.aspx";
});
Sussed it, if I use window.location.replace instead of window.location it works exactly like I want it to. Many thanks all :)

RegisterStartupScript is not working

i am new at ASP.NET and i got problem at using RegisterStartupScript.
i have one page with two UserControl. each UserControl have GridView that can display Detail Page, just like this.
here's the portion of my code:
SenderUserControl.ascx
<script type="text/javascript">
function ShowInsertFormSender() {
window.radopen("WebfrmManageMemo.aspx?RefType=S", "UserListDialog");
return false;
}
function refreshGridSender(arg) {
if (!arg) {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindSender");
}
else {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindAndNavigateSender");
}
}
function RowDblClickReceiver(sender, eventArgs) {
window.radopen("WebfrmManageMemo.aspx?RefType=S&MemoID=" + eventArgs.getDataKeyValue("MemoID"), "UserListDialog");
}
ReceiverUserControl.ascx
<script type="text/javascript">
function ShowInsertFormReceiver() {
window.radopen("WebfrmManageMemo.aspx?RefType=R", "UserListDialog");
return false;
}
function refreshGridReceiver(arg) {
if (!arg) {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindReceiverReferral");
}
else {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindAndNavigateReceiverReferral");
}
}
function RowDblClickReceiver(sender, eventArgs) {
window.radopen("WebfrmManageMemo.aspx?RefType=R&MemoID=" + eventArgs.getDataKeyValue("MemoID"), "UserListDialog");
}
DetailView.aspx
<script type="text/javascript">
function CloseAndRebindSender(args) {
GetRadWindow().BrowserWindow.refreshGridSender(args);
GetRadWindow().close();
}
function CloseAndRebindReceiver(args) {
GetRadWindow().BrowserWindow.refreshGridReceiver(args);
GetRadWindow().close();
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
return oWindow;
}
function CancelEdit() {
GetRadWindow().close();
}
</script>
DetailView.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Request["RefType"].ToString() == "S")
{
ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeSender", "CloseAndRebindSender('navigate');", true);
}
else if (Request["RefType"].ToString() == "R")
{
ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeReceiver", "CloseAndRebindReceiver('navigate');", true);
}
}
My problem is when i click button at DetailView.aspx, ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeSender", "CloseAndRebindSender('navigate');", true); is not working, but ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeReceiver", "CloseAndRebindReceiver('navigate');", true); is working perfectly.
i have search on stackoverflow about why RegisterStartupScript is not working and found this question, but i didn't see something is wrong with my code.
is there anything i miss?
Please help. Thank you
i've got the answer from this question.
the problem is with the javascript in Sender.ascx.
i don't know what's going on but i solved it.
Thank you
Look into using the Sys.Application.Load event when working with IScriptControls, as accessing them in earlier client-side events will give you null. Here is an article on the matter: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-javascript-from-server-side.html.

Null reference from control after page loads

I am trying to run a piece of javascript after the page loads:
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (IsPostBack)
{
string controlName = getPostBackControlName();
if (controlName == "btnSubmit" || controlName == "ddlSalary")
{
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "onLoadDisplay(this)", true);
}
}
}
Here is the referenced javascript:
function onLoadDisplay(sender) {
PerDiemClicked(sender);
}
function PerDiemClicked(sender) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>').checked == true) {
document.getElementById("PerDiemDisplay").style.display = 'inline';
}
else {
document.getElementById("PerDiemDisplay").style.display = 'none';
}
}
I'm getting this error:
0x800a138f - JavaScript runtime error: Unable to get property
'checked' of undefined or null reference
I dont understand why the checked is coming back null because I am waiting for the page to complete postback before checking. How do I check this control and run the above code correctly?
edit: here is the checkbox:
<asp:CheckBox ID="chkbxPerDiem" runat="server" Checked="false" onclick="PerDiemClicked(this)" />
Your script likely executed before chkbxPerDiem was added to the DOM. Perform your script in the window load event:
window.addEventListener("load", function() {
onLoadDisplay(this);
});
Try this:
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", #"
window.addEventListener('load', function() {
onLoadDisplay(this);
});", true);
See this answer
Here is how I solved it.
This code does in fact post after page load:
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (IsPostBack)
{
string controlName = getPostBackControlName();
if (controlName == "btnSubmit" || controlName == "ddlSalary")
{
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "window.addEventListener('load', function() { onLoadDisplay(this); });", true);
}
}
}
I had to modify the javascript function to check if it exists before checking whether its checked:
function PerDiemClicked(sender) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>') != null) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>').checked == true) {
document.getElementById("PerDiemDisplay").style.display = 'inline';
}
else {
document.getElementById("PerDiemDisplay").style.display = 'none';
}
}
}
Thanks everyone.
Page_LoadComplete is a server-side event, and all server-side events will run before the page is even sent to the browser. Remember, these server-side events are related to constructing the page. You need to run your JavaScript after the page has been constructed, served, and fully loaded by the browser. This means the Page_LoadComplete event runs long before your JavaScript code can run.
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "onLoadDisplay(this)", true);
The code above registers a call to your JavaScript load function. But it will insert the call to that function at the top of your page. So it will run before the rest of the page is loaded.
You need to review how ASP.NET events work. Again, all of the server-side events run before the page is even sent to the browser. The JavaScript shouldn't run until after the browser has received and loaded the page.

Categories

Resources