Javascript calling c# functionhttp://forums.asp.net/t/1159888.aspx/1?Javascript+calling+c+functionThu, 20 Sep 2007 16:11:33 -040011598881914393http://forums.asp.net/p/1159888/1914393.aspx/1?Javascript+calling+c+functionJavascript calling c# function <p>Hi all.&nbsp; Tried to do a search on this one, but didnt know exactly how to word it so I wasn't able to find much info on what I wanted to do.</p> <p>Overview of what I'm trying to do.</p> <p>Version: asp.net 1.1</p> <p>I have a crystal report that takes a long time to run and impatient users click the submit button over and over mucking up the system.</p> <p>I've been trying to create a &quot;processing your request&quot; type progress bar that will pretty much inform the user to chill out and let the report run its course.&nbsp; (I'm also going to disable the buttons when I get that far).</p> <p>I've gotten the bar to run, but for the life of me&nbsp;cannot call the ProcessReport() from the javascript and its driving me nuts.</p> <p>Javascript:</p> <p>function html_Submit_Progress(pThis) <br> { if(pThis.nodeName=='INPUT') { <br> &nbsp;&nbsp; pThis.value='...Loading...' } <br> else { <br> &nbsp;&nbsp; pThis.innerHTML='...Loading...' }; <br> <br> html_ShowElement('AjaxLoading'); // There's another function to show the element, thats not an issue, so I didnt include it //<br> window.setTimeout('&#36;x(&quot;AjaxLoading&quot;).innerHTML = &#36;x(&quot;AjaxLoading&quot;).innerHTML', 100); }</p> <p>???? Here's where I'm hitting the ceiling.&nbsp; What do I call here to run the ProcessReport() c# function in the code behind?</p> <p>I've tried __doPostBack and &lt;% ProcessReport(); %&gt; (which unfortunately runs the process as soon as it's encountered so thats not good) and neither have seemed to run it for some reason.</p> <p>my Form info:<br> &lt;form id=&quot;wwv_flow&quot; action=&quot;wwv_flow.accept&quot; method=&quot;post&quot; name=&quot;wwv_flow&quot; runat=&quot;server&quot;&gt;</p> <p>Any help would be greatly appreciated.</p> <p>&nbsp;Thanks<br> <br> Frank</p> 2007-09-18T14:16:15-04:001914412http://forums.asp.net/p/1159888/1914412.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>There is really no way to do that, as client-side processing and server-side processing are 2 different things.</p> <p>I posted this little control a while back that works asynchroneously and might help you out:<br> &nbsp;&nbsp;&nbsp;&nbsp; <a href="http://forums.asp.net/1108756/ShowPost.aspx">http://forums.asp.net/1108756/ShowPost.aspx</a></p> <p>Or you might take a look here for an AJAX solution:<br> &nbsp;&nbsp;&nbsp;&nbsp; <a href="http://ajax.asp.net/docs/tutorials/PartialPageUpdateTutorials.aspx"> http://ajax.asp.net/docs/tutorials/PartialPageUpdateTutorials.aspx</a></p> <p>NC...<br> </p> 2007-09-18T14:24:00-04:001914682http://forums.asp.net/p/1159888/1914682.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>For the button that generates the report (in Page_Load):</p> <pre class="prettyprint">Button1.Attributes.Add(&quot;onclick&quot;, &quot;this.style.display = 'none'; alert('Hold your horses!');&quot;)</pre>&nbsp;<br> Hope this helps!&nbsp; Please mark the helpful post(s) as <strong>Answer</strong>. 2007-09-18T15:57:32-04:001916850http://forums.asp.net/p/1159888/1916850.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>NC, </p> <p>&nbsp;Thanks for the iinfo on the control and the Ajax info.&nbsp; I have no idea what that is yet, I've seen references to it all over but figured it was a paid for control.</p> <p>I'll let you know how it works out.</p> <p>Thanks again,</p> <p><br> Frank</p> 2007-09-19T17:04:43-04:001916901http://forums.asp.net/p/1159888/1916901.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>&nbsp;</p> <p>Looking at the control I have one question before I try it.&nbsp; I think i've tried something similar to this, but not sure.</p> <p>When the &quot;process report&quot; button is clicked, a crystal report is generated and saved to the server as a PDF.&nbsp; Then the process redirects the user to that file which loads into an adobe acrobat reader that is displayed outside of the browser (in browser display is turned off).</p> <p>So I'm not sure if this will work for what I'm trying.&nbsp; </p> <p>Right now what happens is that the progress bar pops up when the button is clicked, but after the redirect I can't seem to get it to stop.</p> <p>What I've thought to do is create the file, store the value in a hidden field and on the post back, if that field is populated, turn off the progress bar and then do a javascript redirect.&nbsp; Which, I would expect, would do the same thing.</p> <p>Not sure how well that would work though, im not as familar with javascript, so not sure how well that would work.</p> <p>Frank</p> 2007-09-19T17:27:11-04:001916963http://forums.asp.net/p/1159888/1916963.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>I doubt if the control will work for that, as it works asynchroneously on a page object.</p> <p>While the Page object is doing the server-side processing, the control is visible. As soon as the server-side processing is completed, the control is hidden.</p> <p>You'd have to have to place it into the PDF doc that you are redirecting to. This might work however.</p> <p>Instead of redirecting to the PDF doc, redirect to this dummy page which only contains an IFrame to display the PDF doc.</p> <p>Call by:<br> &nbsp;Response.Redirect(&quot;PdfDisplayer.aspx?PdfDoc=&quot; &#43; pdfDocUrl);</p> <p>///////////////////////////<br> // aspx file:<br> ///////////////////////////</p> <p>&lt;%@ Page language=&quot;c#&quot; Codebehind=&quot;PdfDisplayer.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;Tester.PdfDisplayer&quot; %&gt;<br> &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt;<br> &lt;HTML&gt;<br> &nbsp;&lt;HEAD&gt;<br> &nbsp;&nbsp;&lt;title&gt;PDF Displayer&lt;/title&gt;<br> &nbsp;&nbsp;&lt;meta content=&quot;Microsoft Visual Studio .NET 7.1&quot; name=&quot;GENERATOR&quot;&gt;<br> &nbsp;&nbsp;&lt;meta content=&quot;C#&quot; name=&quot;CODE_LANGUAGE&quot;&gt;<br> &nbsp;&nbsp;&lt;meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;&gt;<br> &nbsp;&nbsp;&lt;meta content=&quot;<a href="http://schemas.microsoft.com/intellisense/ie5">http://schemas.microsoft.com/intellisense/ie5</a>&quot; name=&quot;vs_targetSchema&quot;&gt;<br> &nbsp;&nbsp;&lt;base target=&quot;_self&quot;&gt;<br> &nbsp;&lt;/HEAD&gt;<br> &nbsp;&lt;body bottommargin=&quot;0&quot; topmargin=&quot;0&quot;&gt;<br> &nbsp;&nbsp;&lt;form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&lt;iframe id=&quot;displayIFrame&quot; frameborder=&quot;0&quot; height=&quot;100%&quot; runat=&quot;server&quot; src=&quot;&quot; width=&quot;100%&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&lt;/iframe&gt;<br> &nbsp;&nbsp;&lt;/form&gt;<br> &nbsp;&lt;/body&gt;<br> &lt;/HTML&gt;</p> <p>///////////////////////////<br> // aspx.cs file:<br> ///////////////////////////</p> <p>protected System.Web.UI.HtmlControls.HtmlGenericControl displayIFrame;</p> <p>private void Page_Load(object sender, System.EventArgs e)<br> {<br> &nbsp;string frameSource = this.Request[&quot;PdfDoc&quot;];<br> &nbsp;this.displayIFrame.Attributes.Add(&quot;src&quot;, frameSource);<br> }</p> <p>NC...<br> </p> 2007-09-19T18:08:33-04:001917128http://forums.asp.net/p/1159888/1917128.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Here is an old .NET 1.1 function I wrote a while back which disables a Button after it is clicked (only if it passes client-side validation). The server-side code for the button will still run though and the button will re-enable after the postback completes. It is overloaded to allow the passing of a message which will override the message that exists on the button. Here is the function:</p> <pre class="prettyprint">private void DisableButtonOnClientClick(ref Button control, string message) { control.Attributes[&quot;onclick&quot;] = &quot;if(typeof(Page_ClientValidate)=='undefined'||(typeof(Page_ClientValidate)=='function'&amp;&amp;Page_ClientValidate())){this.disabled=true;&quot; &#43; (message != null ? &quot;this.value='&quot; &#43; message &#43; &quot;';&quot; : &quot;&quot;) &#43; GetPostBackEventReference(control) &#43; &quot;;return false;}&quot;; } private void DisableButtonOnClientClick(ref Button control) { DisableButtonOnClientClick(ref control, null); }</pre> <p><br>Just call this function on each page load for any button you want it attached to like this:</p> <pre class="prettyprint">DisableButtonOnClientClick(<span class="kwd">ref this</span>.Button1, <span class="st">"...Loading..."</span>);</pre> 2007-09-19T19:30:29-04:001917146http://forums.asp.net/p/1159888/1917146.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Doesn't disabling the button cause the server-side event handler to not fire?</p> 2007-09-19T19:41:50-04:001917150http://forums.asp.net/p/1159888/1917150.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Normally it does. That is why I came up with this solution awhile back. It will disable the button client-side after it is clicked (ONLY if client-side validation passes) but it will still fire the server-side button event. It will remain disabled until the button's server side code completes and the page completes its post back.</p> 2007-09-19T19:45:42-04:001917157http://forums.asp.net/p/1159888/1917157.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>I wrote a similar function in .NET 2.0 which accounts for validation groups. If someone wants it I will dig it up and post it as well.</p> 2007-09-19T19:48:48-04:001917165http://forums.asp.net/p/1159888/1917165.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Based on my own experience, Im not so sure disabling the submit button is a good idea.&nbsp; It has caused so many unexpected problems in the past.</p> <p>What if you have validation on your form onsubmit?</p> 2007-09-19T19:54:54-04:001917186http://forums.asp.net/p/1159888/1917186.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>The button is only disabled if client-side validation passes. If client-side validation passes then the button is disabled and the server-side button click event is fired. The disabled status (along with the textual change on the button's value property) will go away after the server-side code runs and the page finishes its postback.</p> 2007-09-19T20:01:39-04:001917191http://forums.asp.net/p/1159888/1917191.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>No need to repeat yourself.&nbsp; I'd love to see an in-production example of this...</p> 2007-09-19T20:04:18-04:001917200http://forums.asp.net/p/1159888/1917200.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>&nbsp;Unfortunately I cannot provide links to my company's internal systems [:)]<br> </p> 2007-09-19T20:09:08-04:001917498http://forums.asp.net/p/1159888/1917498.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>James,</p> <p>I can't see how that will work as<br> &nbsp;1. You're disabling it AFTER the server-side button event, so it is not going to take affect until AFTER the next button click.<br> &nbsp;2. What is the use of checking for client-side validation AFTER the server-side event has happened, as it would have already happened for the server-side event to fire.</p> <p>This will work, though I agree with Josh that disabling buttons could cause problems.</p> <p>private void Page_Load(object sender, System.EventArgs e)<br> {<br> &nbsp;StringBuilder javaScript = new StringBuilder();<br> &nbsp;javaScript.Append(&quot;if ( typeof(Page_ClientValidate) == 'function' )&quot;);<br> &nbsp;javaScript.Append(&quot;{ &quot;);<br> &nbsp;javaScript.Append(&quot;&nbsp;if ( Page_ClientValidate() == false )&quot;);<br> &nbsp;javaScript.Append(&quot;&nbsp;&nbsp;return false;&quot;);<br> &nbsp;javaScript.Append(&quot;} &quot;);<br> &nbsp;javaScript.Append(&quot;this.value = 'Please wait...';&quot;);<br> &nbsp;javaScript.Append(&quot;this.disabled = true;&quot;);<br> &nbsp;javaScript.Append(Page.GetPostBackEventReference(Button1, string.Empty));<br> &nbsp;javaScript.Append(&quot;;&quot;);</p> <p>&nbsp;Button1.Attributes.Add(&quot;onclick&quot;, javaScript.ToString());<br> }</p> <p>private void Button1_Click(object sender, System.EventArgs e)<br> {<br> &nbsp;// Emulate a lengthy process...<br> &nbsp;TimeSpan waitTime = new TimeSpan(0, 0, 0, 10);<br> &nbsp;System.Threading.Thread.Sleep(waitTime);</p> <p>&nbsp;Response.Write(&quot;Button1_Click fired&lt;br&gt;&quot;);<br> }</p> <p>NC...<br> </p> 2007-09-20T00:04:25-04:001918446http://forums.asp.net/p/1159888/1918446.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>The button is not disabled AFTER the server-side button event. It is disabled BEFORE the server-side button event. When the button is clicked, here is the order of operation for this function:</p> <ol> <li>If client-side validation DOES NOT exist or if client-side validation DOES exist AND client-side validation passes, then... </li><li>Disable the button client-side </li><li>Change button text </li><li>DO POSTBACK which executes the server-side button event code </li></ol> <p>The button will remain disabled until the server-side code post back is complete.</p> <p>I've included a very basis .NET 1.1 example below. You can test it out easily. I put a 2.5 second sleep in the button server-side code to demonstrate the temporary disabling. You can keep or remove the RequiredFieldValidator on the page as it works with or without client-side validation.</p> <span><u>ASPX</u></span> <pre class="prettyprint">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;title&gt;WebForm1&lt;/title&gt; &lt;meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio .NET 7.1&quot;&gt; &lt;meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;&gt; &lt;meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;&gt; &lt;meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;&gt; &lt;/HEAD&gt; &lt;body&gt; &lt;form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; &lt;asp:TextBox id=&quot;TextBox1&quot; runat=&quot;server&quot; /&gt; &lt;asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Submit&quot; /&gt; &lt;asp:RequiredFieldValidator id=&quot;RequiredFieldValidator1&quot; runat=&quot;server&quot; ErrorMessage=&quot;Field is required.&quot; ControlToValidate=&quot;TextBox1&quot; /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt;</pre> <br><br> <span><u>ASPX.cs</u></span> <pre class="prettyprint"><span class="kwd">using</span> System;<br><span class="kwd">using</span> System.Collections;<br><span class="kwd">using</span> System.ComponentModel;<br><span class="kwd">using</span> System.Data;<br><span class="kwd">using</span> System.Drawing;<br><span class="kwd">using</span> System.Web;<br><span class="kwd">using</span> System.Web.SessionState;<br><span class="kwd">using</span> System.Web.UI;<br><span class="kwd">using</span> System.Web.UI.WebControls;<br><span class="kwd">using</span> System.Web.UI.HtmlControls;<br><span class="kwd">namespace</span> TestSolution<br>{<br> <span class="cmt">/// &lt;summary&gt;<br> /// Summary description for WebForm1.<br> /// &lt;/summary&gt;</span> <span class="kwd">public class</span> WebForm1 : System.Web.UI.Page<br> {<br> <span class="kwd">protected</span> System.Web.UI.WebControls.TextBox TextBox1;<br> <span class="kwd">protected</span> System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;<br> <span class="kwd">protected</span> System.Web.UI.WebControls.Button Button1;<br> <br> <span class="kwd">private void</span> Page_Load(<span class="kwd">object</span> sender, System.EventArgs e)<br> {<br> DisableButtonOnClientClick(<span class="kwd">ref this</span>.Button1, <span class="st">"...Loading..."</span>);<br> }<br> <span class="kwd">private void</span> DisableButtonOnClientClick(<span class="kwd">ref</span> Button control, <span class="kwd">string</span> message)<br> {<br> control.Attributes[<span class="st">"onclick"</span>] = <span class="st">"if(typeof(Page_ClientValidate)=='undefined'||(typeof(Page_ClientValidate)=='function'&amp;&amp;Page_ClientValidate())){this.disabled=true;"</span> + (message != <span class="kwd">null</span> ? <span class="st">"this.value='"</span> + message + <span class="st">"';"</span> : <span class="st">""</span>) + GetPostBackEventReference(control) + <span class="st">";return false;}"</span>;<br> }<br> <span class="kwd">private void</span> DisableButtonOnClientClick(<span class="kwd">ref</span> Button control)<br> {<br> DisableButtonOnClientClick(<span class="kwd">ref</span> control, <span class="kwd">null</span>);<br> }<br> <span class="kwd">#region</span><span class="kwdt"> Web Form Designer generated code</span> <span class="kwd">override protected void</span> OnInit(EventArgs e)<br> {<br> <span class="cmt">//<br> // CODEGEN: This call is required by the ASP.NET Web Form Designer.<br> //</span> InitializeComponent(); <span class="kwd">base</span>.OnInit(e);<br> }<br> <br> <span class="cmt">/// &lt;summary&gt;<br> /// Required method for Designer support - do not modify<br> /// the contents of this method with the code editor.<br> /// &lt;/summary&gt;</span> <span class="kwd">private void</span> InitializeComponent()<br> { <br> <span class="kwd">this</span>.Button1.Click += <span class="kwd">new</span> System.EventHandler(<span class="kwd">this</span>.Button1_Click);<br> <span class="kwd">this</span>.Load += <span class="kwd">new</span> System.EventHandler(<span class="kwd">this</span>.Page_Load);<br> }<br> <span class="kwd">#endregion<br> private void</span> Button1_Click(<span class="kwd">object</span> sender, System.EventArgs e)<br> {<br> <span class="kwd">if</span> (Page.IsValid)<br> {<br> System.Threading.Thread.Sleep(2500);<br> }<br> }<br> }<br>}</pre><br> 2007-09-20T13:08:48-04:001918489http://forums.asp.net/p/1159888/1918489.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Sorry, in your original post I missed the call to DisableButtonOnClientClick in the Page_Load event handler which will make your code operate almost identical to what I posted.</p> <p>NC...<br> </p> 2007-09-20T13:30:56-04:001918514http://forums.asp.net/p/1159888/1918514.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Just like I already mentioned....</p> <p>What if you have your own&nbsp;custom validation on the form onsubmit?&nbsp; If that validation fails, you are left there with a disabled button and no way to submit the form.</p> 2007-09-20T13:42:51-04:001918568http://forums.asp.net/p/1159888/1918568.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Ho hum. Here is the same example page with a custom validator on it. If custom validation fails it does not leave the button disabled. Whether the custom validation is client-side or server-side it doesn't matter. Are you even testing the code or should I just stop providing examples?</p> <pre class="prettyprint">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;title&gt;WebForm1&lt;/title&gt; &lt;meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio .NET 7.1&quot;&gt; &lt;meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;&gt; &lt;meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;&gt; &lt;meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;&gt; &lt;script type=&quot;text/javascript&quot;&gt; function doCustomValidation(sender,args) { args.IsValid = (document.getElementById('&lt;%=TextBox1.ClientID%&gt;').value != ''); } &lt;/script&gt; &lt;/HEAD&gt; &lt;body&gt; &lt;form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; &lt;asp:TextBox id=&quot;TextBox1&quot; runat=&quot;server&quot; /&gt; &lt;asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Submit&quot; /&gt; &lt;asp:RequiredFieldValidator id=&quot;RequiredFieldValidator1&quot; runat=&quot;server&quot; ErrorMessage=&quot;Field is required.&quot; ControlToValidate=&quot;TextBox1&quot; /&gt; &lt;asp:CustomValidator ID=&quot;CustomValidator1&quot; Runat=&quot;server&quot; ErrorMessage=&quot;Custom validation failed.&quot; ClientValidationFunction=&quot;doCustomValidation&quot; /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt;</pre><br> 2007-09-20T14:06:00-04:001918574http://forums.asp.net/p/1159888/1918574.aspx/1?Re+Javascript+calling+c+functionRe: Javascript calling c# function <p>Dude, I don't need to test the code&nbsp;becuase, as mentioned before, I have already been down this path!&nbsp;</p> <p>I am not talking about this CustomValidator bullcrap.&nbsp; I am talking about hard coding your own form onsubmit validation.&nbsp; Not with any .NET validators or anything like that, that is all rubbish.&nbsp; In several of my web apps, I have my own validation that I put on the form onsubmit that does many different&nbsp;things.&nbsp; Your code will break that, guaranteed.&nbsp; I can say that with confidence withotu testing.&nbsp; Perhaps you should test your code with that and see for yourself!</p> <p>I am done arguing with you about this.&nbsp; It is a bad idea to disable the submit button.&nbsp; Period.</p> <p>Regards...</p> 2007-09-20T14:10:41-04:00