tags:

views:

236

answers:

5

I have several functions that are written in JS that do error checking on my site. On one instance I want to disable two of the checks if a certain instance in the DB is true.

I have something like this on the aspx.cs

if (this.exemptcheck == true) {
  // could set a variable to pass or whatever is optimal here
}

Then in JS I have a function like this in my .aspx

if (NotANumberCheck(item.GetMember("TotalSpace").Value, 'Space must be Numeric') == false) { return false; }

I don't want this check to run if this.examptcheck returns true. What is the optimal way to do this? I have heard of putting variables in non-visible fields but that doesn't seem ideal. Any suggestions would be appreciated.

Thanks

A: 

You could write a script section in the page which sets a variable to true/false and then check for that variable in the JS later.

casperOne
+2  A: 

In the aspx, set a member variable called ExemptCheck. Later, in the JS, do this:

var exemptCheck = <% Response.Write(ExemptCheck.ToString()) %>;

This will give you a JS variable with the same value as the C# variable.

Aric TenEyck
I have this in my aspx.cs private void pageSetup(EventArgs args) { Boolean exemptCheck; if (dss.ExemptCheck == true) { exemptCheck = true; } }I put the following in my JS function onInsert(item) { var exemptCheck = <% Response.Write(exemptCheck.ToString()); %>; }This gives me the following error Compiler Error Message: CS0103: The name 'exemptCheck' does not exist in the current contextIs this a scope issue or am i doing something else wrong?
Splashlin
It's a scope issue. exemptCheck needs to be a member variable of the entire page object, not just the pageSetup function; the code between the <% %> is executed during the page render function, long after your pageSetup function has exited.
Aric TenEyck
A: 

You may Response.Write() the javascript code only when this.examptcheck is true.

Humberto
A: 

Just register your js function only if your condition is true, you don't need to check a variable:

C# code:

if (this.exemptcheck == true) {
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "KEY", "/* code of your JS function that handle the true case */", true);
}
tanathos
A: 

I ended up using the following command to set the value in the aspx.cs code

Page.ClientScript.RegisterStartupScript(this.GetType(), "", "var stackedUnknownExemptCheck = true;", true);

Splashlin