Link to home
Start Free TrialLog in
Avatar of crich
crich

asked on

Display variable on asp.net page

I am trying to show text on the webpage if the value of edit = 1 but i get an error:

Name 'edit' is not declared.

with this code:

<% If edit = 1 Then %>
EDIT MODE
<% End If  %>

from a previous question, from ImSoLost

Why not do this server side?  something like this

dim edit as integer

If Edit = 1 then
     label1.text = "whatever you want to say on the page"
Else
    label1.text = "Whatever you say for this condition"
End If

Avatar of Andre412
Andre412
Flag of United Kingdom of Great Britain and Northern Ireland image

you must instantiate all variables on the form

close to the top you should have something like

<% dim edit as integer or boolean or whatever %>

later in the form you need to assign a value to it

<% if case1 then edit = value else edit = othervalue %>

then again perform a test on it for your text

<% if edit = 1 then %>
some text
<%end if%>

alternatively

<% if edit = 1 then response.wrtie("Some text") %>
Why not do this server side?  something like this

dim edit as integer

If Edit = 1 then
     label1.text = "whatever you want to say on the page"
Else
    label1.text = "Whatever you say for this condition"
End If
because he will then be limited on formating, he wont be able to dynamically change font styles colours etc and what if his page is dynamically built anyway with no fised label position or size?
you can still change all of that with server side code.  It may not be the way he is used to doing it but it can still be done just the same.  The question was really directed at crich though.  
Avatar of crich
crich

ASKER

I get the following error:

Name 'edit' is not declared

with this code:

Sub Page_Load (s as object, e as eventargs)
        Dim objChkUsername As String = Session("chkusername")
 
        If IsDBNull(objChkUsername)  Or objChkUsername=" "  Then
              edit = 0
        Else
              edit = 1
        End If
            
    End Sub
change it to this

Sub Page_Load (s as object, e as eventargs)
        Dim objChkUsername As String = Session("chkusername")
        Dim Edit as Integer

        If IsDBNull(objChkUsername)  Or objChkUsername=" "  Then
              edit = 0
        Else
              edit = 1
        End If
         
    End Sub
Avatar of crich

ASKER

this is how i am trying to use the edit variable and where the code says the error is:

<% if edit = 0 then %> EDIT <% end if %>
Avatar of crich

ASKER

Sorry - yes i have also tried :

Sub Page_Load (s as object, e as eventargs)
        Dim edit as Integer
        Dim objChkUsername As String = Session("chkusername")
 
        If IsDBNull(objChkUsername) Or objChkUsername=" "  Then
              edit = 0
        Else
              edit = 1
        End If
            
    End Sub
Avatar of crich

ASKER

I also tried doing this the server side way you suggest but didn't know how to use the label1.text tag on the webpage without having to put it in a text box.
ASKER CERTIFIED SOLUTION
Avatar of Fred Goodwin
Fred Goodwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is this edit variable used for and where are you setting its value?
Avatar of crich

ASKER

Ok. The text now appears on the page but doesn't seem to be responding to the conditios that determine whether it should be shown. i have a log out script:

<script runat="server">
Sub Page_Load(s as Object, e as eventArgs)
Dim objChkUsername As String = Session("chkusername")
If Not IsDBNull(objChkUsername)  Or objChkUsername<>" "  Then
Session("chkusername")= ""
Response.Redirect("index.aspx")
Else
Response.Redirect("index.aspx")
End If
End Sub
</script>

When this is run my session variable should be empty so why does it seem like edit still equals 1??
if you want to ensure that the session is empty you can do a
session.abandon

Did you hard code Edit = 1 somewhere else?
it is prob better code practice to remove the session object then set it to an empty string, you then don't need to declare set or check a second string value, but why are you redirecting to login regardless of the result of the if statement

Sub Page_Load(s as Object, e as eventArgs)
        'line below is all on one line
        If Not IsNothing(Session("chkusername")) Then objChkUsername = Session("chkusername") Else Session.Remove("chkusername")
Response.Redirect("index.aspx")
End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crich

ASKER

Thank you for the answers. I split the points as the combination of working through all the possibilites has lead to the right solution.

Thanks,
Richard