How can i invoke javascript function within the webbrowser control? - c#

I want my website/webpage to be navigated to a new window which displays the report.Even the Website gets redirected to a login page rather than the report page the moment when the javascript function is invoked.I have given my requirement below.can anybody help me out in finding the solution?
InvokeScript doesnot work in the following case.
<a onclick="return showReport('RPTType=SReport');" id="ctl00_tbr_lnkbtnS" class="blckclr" href="javascript:__doPostBack('ctl00$tbr$lnkbtnS','')">S</a>
I used the following statement
webBrowser1.Document.InvokeScript("showReport", new String[] { "RPTType=SReport" });
Thanks in advance....

I'm not sure I understand the question correctly, but I would think you should return false from the onclick event of the link if you're going to handle it with javascript, otherwise it will execute the javascript and then redirect to the href url.

Related

Redirect to previous page not working

i have used windows.history.back() to return user to previous page. now this command is not working but when i put debugger and do debug then page is getting redirect to previous page. but without debugging in firebug it is not working .
i have also tried windows.go(-1). and even i tried all this option after clearing cache. still not working
can anyone tell me what is problem? or give me alternative way to get browser back button functionality using java script/J Query or asp.net c#.
just try to use html button instead of asp.net button or put return false at the end of javascript
may be server side click is being called
You can use C# Coding also for it.
On Button click
if (Request.UrlReferrer != null)
{
Response.Redirect(Request.UrlReferrer.ToString());
}
Which control are you using to write code to previous page.
If you're using Asp:Button
write the javascript code on onclientclick event
window.history.go(-1)
If you're using Html button
write the javascript code on onclick event
window.history.go(-1)
If you're using anchor tag <a>
write the javascript code to href attribute
<a href="javascript:window.history.go(-1);">
Try this
onclick="window.history.go(-1)"

c# check page has loaded before redirecting

I've have a page which currently does a small task then redirects to a 3rd party website.
The redirect is triggered by a Response.Redirect();
Now I want to add another function but this time it's javascript.
I'm using the following method to add the javascript to the page:
if (!page.ClientScript.IsStartupScriptRegistered(key))
{
ScriptManager.RegisterStartupScript(page, typeof(Page), key, script, false);
}
The function of the javascript is to track a sale, so I need the page to render it and trigger the track before moving on.
Anyone know if this will work or not, and if not how it can be achieved?
You're not going to be able to use Response.Redirect() to handle redirecting the user anymore. Instead, you'll have to redirect through JavaScript after your start-up script executes. I would simply tack on:
window.location.replace("http://www.whateveryourredirecturlis.com");
To the end of your existing start-up script.
Use something like this in your javascript.
document.onready = function () { window.location = "http://yoururl"; };

Running Javascript to size an ASP.NET control once it is rendered or updated by a postback

I have an ascx control that is loaded in an aspx page.
How can I instruct my ascx to run javascript once it is rendered in the browser.
It should run on every postback as well.
I looked around but could not find a satisfactory answer.
Have you tried using the "RegisterStartupScript" method along with JQuery to call a JS function?
This code might help you:
ScriptManager.RegisterStartupScript(Page, GetType(Page), "myScript", "$(function() {{ alert ('Your page is loaded.'); }});", True)
You can replace the alert with any JS function you want :)
I hope this helps.

Run javascript function after Server-Side validation is complete

Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed.
However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid.
Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator?
You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel.
if (Page.IsValid){
ScriptManager.RegisterStartupScript(
customValidator1,
typeof(MyPageClass),
"closeBox",
"myLightBoxVariableOnThePage.close()",
true);
}
When that server side validator runs, it will send a whole new page to the browser. Anything that was shown in the browser before was destroyed, including any state kept in your javascript. If new page bears a strong resemblance to the old page, you should consider this a happy coincidence.
Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place.

Javascript popup Messagebox not working when we use Response.Redirect on ASP.NET Page?

I have update button and after saving the record to database, I am displaying popup Msg using Javascript as below.
When i don't use Response.Redirect, Popup is working fine.
But when i use Response.Redirect, Popup is not displaying.
Anybody faced the same issue. Please throw some light.
Appreciate your help.
ScriptManager.RegisterStartupScript(
this,
typeof(string),
"popup",
"alert('Thank you for visiting the MedInfo website. Your request has been submitted.');",
true);
Response.Redirect("Default.aspx");
Create a js function , in function show message then navigate to the page
Server side
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
JS
function ShowMessage()
{
alert('your message');
window.location.href='default.aspx';
}
The very reason you don't see the message is that you navigate from this page, so the page with this script injected in it never gets rendered.
Response.Redirect transfers the control to another page, and your script is loaded in the current page.
See Response.Redirect Method
Remove the response.redirect method and then change the scriptmanager. Remove the alert message and instead call a javascript function. Inside the function you can show the alert message and then on the next line write
document.location = "Default.aspx";
ScriptManager.RegisterStartupScript(
this,
typeof(string),
"popup",
"alert('Thank you for visiting the MedInfo website. Your request has been submitted.')" + "location.href='Default.aspx';",true);
As mentioned earlier, Response.Redirect interrupt current page execution and transfers control to another page. If you want to show message box after redirect, register your javascript code in Page_Load event handler routine of page to which you are redirect.
Instead of doing a server side, try doing it in the client side. Add window.top.location ="Default.aspx" to your javascript code

Categories