Solved

Display variable on asp.net page

Posted on 2004-09-22
16
10,303 Views
Last Modified: 2011-10-03
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
Comment
Question by:crich
  • 6
  • 5
  • 5
16 Comments
 
LVL 4

Expert Comment

by:Andre412
ID: 12122648
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
 
LVL 7

Expert Comment

by:imsolost
ID: 12122811
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
 
LVL 4

Expert Comment

by:Andre412
ID: 12123100
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 7

Expert Comment

by:imsolost
ID: 12123148
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
 

Author Comment

by:crich
ID: 12123270
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
 
LVL 7

Expert Comment

by:imsolost
ID: 12123336
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
 

Author Comment

by:crich
ID: 12123339
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
 

Author Comment

by:crich
ID: 12123387
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
 

Author Comment

by:crich
ID: 12123403
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
 
LVL 7

Accepted Solution

by:
imsolost earned 50 total points
ID: 12123505
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
 
LVL 4

Expert Comment

by:Andre412
ID: 12123613
what is this edit variable used for and where are you setting its value?
0
 

Author Comment

by:crich
ID: 12123636
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
 
LVL 7

Expert Comment

by:imsolost
ID: 12123776
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
 
LVL 4

Expert Comment

by:Andre412
ID: 12123841
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
 
LVL 4

Assisted Solution

by:Andre412
Andre412 earned 25 total points
ID: 12123861
forgot to add the following declaration

Dim objChkUsername As String = "NoUser"
0
 

Author Comment

by:crich
ID: 12123965
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

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question