views:

58

answers:

3

I suppose this isn't a huge deal, since there are other way around this issue, but I'm really curious as to the answer, since I thought this was possible to do.

I have a public property that returns a boolean in my code behind. I'd like to access this server variable in my javascript validation function, but so far, not quite getting it.

Public Property editMode() As Boolean
    Get
        If Cache("editMode") IsNot Nothing Then
            Return (DirectCast(Cache("editMode"), Boolean))
        Else
            Return False
        End If
    End Get
    Set(ByVal value As Boolean)
        Cache("editMode") = value
    End Set
End Property

function validateEdit()
{
    alert("editMode value is " + '<%#editMode()%>');
    if ('<%#editMode()%>'.toString() == "True")
    {
        alert("You are currently in edit mode. Please save or cancel changes.");
        return false;
    }
    return true;
}

I've tried a bunch of variations on this, but it's always False. In the current code the alert returns "editMode value is False"

When I use:

if ('<%#editMode()%>') ...

Then it's still always False, but it goes into the if condition, so the behaviour is as if it were always true.

One other thing to mention is that most javascript/server tag stuff I find says to use <%=editMode %>, but I can't do this because every time I use the = instead of the # I get an exception:

"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."

So I solved this by using # and saying

    Page.Header.DataBind()
    Page.Form.DataBind()

In the page load event handler.

Any ideas? Thank you in advance. :)

(Also, I usually use C#, so I might have unknowingly done something goofy in the VB part, so feel free to point that out too)

+1  A: 

First, try changing to this:

<%=editMode()%>

Not sure if that's it, but it can't hurt. Second, are you in edit mode when you first load the page? That code is going to run server side and return the result to the user.

On the user's page, they will see:

function validateEdit()
{
    alert("editMode value is " + 'False');
    if ('False'.toString() == "True")
    {
        alert("You are currently in edit mode. Please save or cancel changes.");
        return false;
    }
    return true;
}

Again, not sure if that is it, but it is important to understand that javascript is not making any calls to the server.

Stargazer712
As far as I can see it is correct on the server side. editMode changes as it should and it does not start out as true. If you read my post, I've already tried <%=editMode()%>, perhaps you can tell me what the error is from?
Brandi
You said it in your comment: editMode does not start off as true. Thus, the JavaScript is rendered as I showed you, and calling validateEdit() will *always* return the same value.
Stargazer712
I must be missing what you mean? I probably should have mentioned I know almost zip about JavaScript... I have a button that changes it on the server side, but it still returns false, even after I walk through debugging to make sure it is true. Shouldn't it then be 'True'.toString() == "True"? Honestly my best guess right now is that the # must not be evaluating the way I think it does.
Brandi
A: 

One more thing.

You do realize you are converting a string to another string with

'<%#editMode()%>'.toString() 

Right?

I think what you want is this

if ('<% =editMode.toString() %>'= 'True')...

or Better yet

if (<% =editMode.toString().ToLower() %>)...
JohnFx
Okay, as mentioned in the post, I realize that = is more widely used and I have been trying over and over again to use it, but why am I getting the exception I mentioned every time I use = instead of #?
Brandi
The point of my comment wasn't about = vs #. It was that you are converting a literal string to another string unnecessarily. Also the boolean conversion probably should happen server side instead of client side.
JohnFx
A: 

This helped me fix the error. http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blo

Moving the javascript function out of the head and into the body fixes the problem. Seems to be a few things that could cause this issue, but in my case, the most likely culprit is the AjaxToolKit.

Brandi