Background
I have a client-side button click that triggers a server-side function. Before the server side function is called, a loading panel is displayed (div).
The loading panel needs to be removed once the server-side function has completed.
My Solution
After the server-side function is complete I will call a JavaScript function that will remove the div. So as a test i'm calling an alert script. I'm trying to do this from the master page.
Client-Side Code
My PopUp Function
<script>
function PopUp() {
debugger;
alert('TEST');
}
</script>
My Script Manager
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Server-Side Code
My Call after server side functions have completed.
//Add the script after <form> tag
bool isClientScriptBlockRegistered = ClientScript.IsClientScriptBlockRegistered("ShowStatus");
if (!isClientScriptBlockRegistered)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ShowStatus", "PopUp();", true);
}
Problem
My script isn't called by the server. No alert window is created. When I try to do this from any page other that the master page it works. However on the master page it does not.
Questions
Is there something I'm missing?
Does there need to be a callback or some kind of refreshing of the page for the alert to appear, or can the server just call the script without any action from the client?
Change
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ShowStatus", "PopUp();", true);
into
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ShowStatus", "setTimeout(function () { PopUp(); }, 10);", true);
or into
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "ShowStatus", "PopUp();", true);
RegisterClientScriptBlock adds the JavaScript code at the top of the page just after the ViewState tag while RegisterClientScriptBlock adds it to the bottom. So if you use RegisterClientScriptBlock and your PopUp() is somewhere lower on the page you'll get an error.
Or by setting a setTimeout you ensure that all the contents is generated and then PopUp() will also be found even is the script block is at the top.
Related
I have master page with custom button control. I have one javascript function in master page. When i try to call this javascript function with in custom control code behind its not invoking. pls check below code.
Javascript code in master page
function loader()
{
//Note: ctl00_loader is master page control.
document.getElementById('ctl00_loader')="X";
}
Mycustom control is
<Shri:ConfirmBtn ID="UsrBtn" runat="server">
Code-behind code is
Protected Sub UsrBtn_ConfirmClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles UsrBtn.ConfirmClick
ClientScript.RegisterStartupScript(GetType(String), "MyCode1", "<Script
Language=Javascript>loader();</script>")
End sub
code behind line is working fine in page loading time. but when i place it custom control click (UsrBtn_ConfirmClick) inside its not loading. how to solve this one? any idea please.
Use jQuery:
In the header of your html page:
<script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
Then capture the button click
$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
// Do whatever you need here ....
});
To debug code embedded in the page add the keyword debugger; like so:
$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
// Do whatever you need here ....
debugger;
});
This will force the IDE to stop on that line then you can use the F10 key to step through the code like you would in the code behind.
I try to call the JS function from CodeBehind ( C# ) :
function goToBottom() {
window.scrollTo(0, document.body.scrollHeight);
}
The function works when I call it directly from my asp.net.
I tried this but it doesn't work... :
Page.ClientScript.RegisterStartupScript(this.GetType(), "goBot", "goToBottom()", true);
RegisterClientScriptBlock and RegisterStartupScript do not "call" code or functions, they simply add code to the page; RegisterClientScriptBlock adds script to the top of the page—so it might not see all the html because it's not loaded; RegisterStartupScript adds script to the bottom of the page—so all the html is available to it.
If you want to scroll down when the page loads, take it out of the function:
// this will run the *first time* the page loads.
window.scrollTo(0, document.body.scrollHeight); // no function.
If you want to call it from a click, use a function:
function goToBottom() {
window.scrollTo(0, document.body.scrollHeight);
}
Calling code behind from javascript is another issue. It has been asked before. Search the site or start another question.
I am trying to invoke the js function implemented on the aspx page from a .cs class. But, ScriptManager doesn't seem to exist in .cs class. Basically, the .cs file is a part of dll that I am using in my project. I need to invoke the js function that is implemented on the aspx page, from my .cs file present in the dll.
The js function is successfuly invoked from the aspx page, but when I try the same code in .cs file it says
ScriptManager is inaccessible due to its protection level.
here is code I am trying
protected void MyMethod()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
}
any ideas why the same code runs successfuly on the aspx page but not from a .cs class?
ScriptManager.RegisterStartupScript accept Page or Control as first argument. Make sure you pass your current Page to the cs method
protected void MyMethod(Page page)
{
ScriptManager.RegisterStartupScript(page, typeof(UpdatePanel), new Guid().ToString() , "jsfunction();", true);
}
And call from aspx.cs page using:
MyMethod(this.Page);
Well, to overcome the above issue I simply tried with this piece of code
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
Notice the use of full namespace with ScriptManager.
The following lines of code are in a user control in a SharePoint website.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/jquery-1.4.4.min.js\"></script>", false);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "javascriptgeral", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/javascript.js\"></script>", false);
Why doesn't it work? I also can't find the respective script tags in the HTML generated in the response.
The problem has to do with an update panel. After the partial postback, the Javascript stops working. Shouldn't it work with the above code?
(I also tried the RegisterClientScriptInclude method but with the same result.)
This isn't going to work on a partial postback. You need to register the scripts on Page_Load. In the user control, attach to the Load event and call ScriptManager.RegisterClientScriptBlock() from that handler.
After the partial postback, the Javascript stops working
An UpdatePanel partial postback is DOM update of the UpdatePanel <div> contents. This means the previous contents are lost, so the state of inline script contained within that <div> loses its state.
See here for more information:
UpdatePanel does its work on the client through the innerHTML DOM property. A delta is retrieved from the server, finds itself in the existing DOM, disposes of the contents, and then assigns the new content via innerHTML. ... But inline script doesn't work this way. Setting the innerHTML of a DOM element to HTML which contains a script block does not cause that script to execute.
Likewise, calling ScriptManager.RegisterClientScriptBlock() on an UpdatePanel update is not going to act like a page load. You could add the <script> elements directly to the UpdatePanel contents, but it won't execute.
Update
You can call javascript code after a partial postback by adding an endRequest handler to the PageRequestManager:
<script>
function load() {
//register the handler
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
//this handler will execute after a partial postback
function EndRequestHandler(){
//...arbitrary code...
}
window.onload = load;
</script>
I am new in development, I want to show the alert message on button click after that I want to redirect the on another page. my button code is like below:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script type='text/javascript'> alert('Please wait for approval!');</script>");
Response.Redirect("~/profile/scrapbook.aspx?uid=" + Request.QueryString["uid"], false);
}
in this case alert is not working, If I remove the Response.Redirect then it will works properly.
Please suggest me how to call he alert message after that redirect on another page.
You are mixing server side and client side code.
The Response.Redirect("~/profile/scrapbook.aspx?uid= is server side and the Alert won't wait the action of the user to execute it...
What you will need is to do you redirect in Javascript or to use something else than an Alert for the message.
Solution 1
You display a Alert message in Javascript (client side) when the user press okay you do a redirect in Javascript.
Solution 2
You display a message in the HTML with a button in ASP with an event that will do a call to the server and redirect your user to the page you desire.
Add your script like this:
const string scriptString = "<script type='text/javascript'> alert('Your friend request sent to the user! Please wait for approval!');</script>";
ClientScriptManager script = Page.ClientScript;
script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);
Do not use Response.Write to render your JavaScript. Use RegisterClientScriptBlock or RegisterStartupScript instead.
Your example should be using RegisterStartupScript, since it's rendering executing script, and not function declarations.