Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling code behind proceure from Javascript function. Possible? 1

Status
Not open for further replies.

Alcar

Programmer
Sep 10, 2001
595
US
How can I call a code behind procedure from a Javascript function?
Thanks in advance... Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
Set the arguments in the form - __EVENTTARGET and __EVENTARGUMENT - and then submit it - this will route to the object you specify - mind you, it has to be set up to handle it, do you mean call a method belonging to your page class?
 
The most effective way I've found to do this is to set the value of a hidden variable in the form, and then evaluate that value on page_load, and take appropriate action:

js:
document.theForm.fireCode.value = 1;

~~~~

code-behind:
if request.form("fireCode") = "1" then
'fire code
end if
penny1.gif
penny1.gif
 
I was hoping not to submit the form, but to verify some information on the back end and giving two possible results:

error - User needs to check the information in the form.
ok - submits the form.

Therefore the server function needs to be called before the form is submit.

Or any other suggestions. I really don't want to access the DB from the Javascript, but giving a certain field value I need to check if that field already exists on the DB. If it does, then return error, else go ahead and submit the information.
Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
ok better way:

I have a button that:
checks if a certain textbox value exists in the database. If the value doesn't exists then it continues with other code.

My problem comes when the value exists in the db. I need to pop a message, possibly through javascript.
If I add a javascript function with an alert call to the button, this will be called before it checks the db. How can I make it call a javascript function after running some code?

*fried brain for dinner*
*again*
Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
Hey Daren I just finished wrapping my brain around sending exceptions back from webservices. What I ended up with was this

Catch e As SoapException
Dim sb As New StringBuilder()
sb.Append(&quot;<Script Language=Javascript>&quot;)
sb.Append(&quot;alert('&quot;)
sb.Append(e.Detail.Attributes(&quot;errormsg&quot;).Value)
sb.Append(&quot;')&quot;)
sb.Append(&quot;</Script>&quot;)

RegisterStartupScript(&quot;error&quot;, sb.ToString)
sb = Nothing
End Try


I am feeling lazy so I just posted this as an example of having a message box on a certain condition. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I see. But this just writes the script but doesn't execute it, which is my only problem... Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
I know thats the beauty of it. When you page is posted back it now has that javascript bit in it. So when the browser parses the new page a popup appears. When the next postback happens as long as the code that wrote the javascript isn't ran then the popup will no longer appear. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
So what you are telling me is that if in my ButtonClick event I check the DB for the item and find it I create my own exception and throw it. I then catch it, and add the script on the fly and reload the page... very interesting. This might do it. I'll let you know!

oh AnyHoo... Thanks *throws candy towards Mark* Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Works like a charm Mark.
This is how I arranged it, thanks to your code:

Code:
try
{
 if(!AccountExists())
 {
   SaveAccount();
   Response.Redirect(&quot;NextPage.aspx&quot;);
 }
 else
 {
   Exception AccountExistsException = new Exception(&quot;Account already exists!&quot;);
   throw AccountExistsException;
 }
}
catch(Exception ex)
{
 StringBuilder sb = new StringBuilder();
 sb.Append(&quot;<Script Language=Javascript>&quot;);
 sb.Append(&quot;alert('&quot;);
 sb.Append(ex.Message.ToString());
 sb.Append(&quot;')&quot;);
 sb.Append(&quot;</Script>&quot;);
 RegisterStartupScript(&quot;error&quot;, sb.ToString());
 sb = null;
}
Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Glad I could help but I think I can do even better. As i mentioned I was just feeling very lazy when I first posted. In your case you don't need to and shouldn't use an exception. I was doing so because I was getting the error back from a web service.
In your case this is just unnecessary overhead. Skip the exception bit and just put the code you have in the catch exception bit into under your else statment. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
The nice thing about it was that I could use it for other exceptions I can throw from the AccountExists() and SaveAccount() functions, and they will all appear as an alert message.
Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Tru though I might be inclined to implement it thru a sub rather than exception handler. It's about the same I guess. Glad it works for U. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top