1

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

1
  • Side-note: It is typically discouraged to check boolean values explicitly for being equal to true or false. A boolean value is already a boolean value. At least in C# and JS, it should not be a problem though. Commented Jul 10, 2009 at 15:04

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 context Is this a scope issue or am i doing something else wrong?
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.
0

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);
}

Comments

0

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.

Comments

0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.