Running a javascript function with code behind...

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;
}
}
Weller0123Asked:
Who is Participating?
 
trevorhartmanConnect With a Mentor Commented:
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
 
trevorhartmanCommented:
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
 
Weller0123Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.