?
Solved

Running a javascript function with code behind...

Posted on 2005-05-12
3
Medium Priority
?
280 Views
Last Modified: 2010-04-07
I'm running the function below that should return a true or false.   I'm calling the function from the onclick event of a button.

How do I capture the returned value in code behind?  

Again this function is attached to an onclick event.

Thanks in advance.

------------------- javascript function ------------------------------------

function ValidateQuery() {
var Flag = "";
Flag = document.Form1.txtValidHidden.value;                        
if (Flag == 0)                        
{
return confirm('No Project, Project Abbrv., Employee ID or Org. Code longer than 5 characters has been specified.  You may get too large of a result set.');                        
                        
}
else
{                  
OpenPage();
return true;
}
}
0
Comment
Question by:Weller0123
  • 2
3 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13987944
to clarify: you need to pass a value from client-side javascript to the server-side codebehind?

if this is the case, you can use the clientside function that asp.net auto-generates:

function __doPostBack(eventTarget, eventArgument) {
    if (theForm.onsubmit == null || theForm.onsubmit()) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

you can pass your variables to the __doPostBack function and catch it in the page_load

let me know if you want to do it this way, and i'll show you how
0
 
LVL 8

Accepted Solution

by:
trevorhartman earned 2000 total points
ID: 13988005
another way do to it would be to have an <input type="hidden" runat="server" id="returnValue" name="returnValue" />

then in your javascript function:

function ValidateQuery() {
var Flag = "";
Flag = document.Form1.txtValidHidden.value;                    
if (Flag == 0)                    
{
document.getElementById("returnValue").value =  confirm('No Project, Project Abbrv., Employee ID or Org. Code longer than 5 characters has been specified.  You may get too large of a result set.');          
// now tell it to do a postback
__doPostBack('','');                    
}
else
{              
OpenPage();
document.getElementById("returnValue").value =  true;
// now tell it to do a postback
__doPostBack('','');        
}
}


then in your Page_Load:

string value = returnValue.Value;


is this making sense?

-Trevor
0
 

Author Comment

by:Weller0123
ID: 13989325
I ended up doing this since I was already adding an attribute to the onclick event of an asp button:

Me.btnTotals.Attributes.Add("onclick", "return ValidateQuery()")

It's built in to the Add funtion/method that if the function returns false then the onclick event doesn't even fire.

This works perfect.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question