• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10331
  • Last Modified:

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

0
crich
Asked:
crich
  • 6
  • 5
  • 5
2 Solutions
 
Andre412Commented:
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") %>
0
 
Fred GoodwinVP of Software DevelopmentCommented:
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
0
 
Andre412Commented:
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?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Fred GoodwinVP of Software DevelopmentCommented:
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.  
0
 
crichAuthor Commented:
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
0
 
Fred GoodwinVP of Software DevelopmentCommented:
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
0
 
crichAuthor Commented:
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 %>
0
 
crichAuthor Commented:
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
0
 
crichAuthor Commented:
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.
0
 
Fred GoodwinVP of Software DevelopmentCommented:
in your html just do this

<asp:label runat="server" id="label1" />

Now when in your code you say
label1.text="Whatever you want to say"

The line "Whatever you want to say" will show up wherever the asp:Label control is on the page
0
 
Andre412Commented:
what is this edit variable used for and where are you setting its value?
0
 
crichAuthor Commented:
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??
0
 
Fred GoodwinVP of Software DevelopmentCommented:
if you want to ensure that the session is empty you can do a
session.abandon

Did you hard code Edit = 1 somewhere else?
0
 
Andre412Commented:
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
0
 
Andre412Commented:
forgot to add the following declaration

Dim objChkUsername As String = "NoUser"
0
 
crichAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now