开发者

Server side variables in javascript

开发者 https://www.devze.com 2023-01-07 05:39 出处:网络
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 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开发者_StackOverflow 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)


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.


This helped me fix the error. "The Controls collection cannot be modified because the control contains code blocks"

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.


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() %>)...
0

精彩评论

暂无评论...
验证码 换一张
取 消