Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating ASP Labels for the Terminally Noobish 1

Status
Not open for further replies.

StevenB

IS-IT--Management
Sep 25, 2000
247
US
So, I'm attempting to write a little ASP.NET application, even though I'm a terminal newbie about such things, and NOT a programmer by nature. My programming method is basically taking code from various other places and tweaking it to death.

The application grabs some data from an Access database. It also grabs the user's IP address and displays it in a label on the page.

Oddly, I got the database connection working, but I can't get the @#()$*( IP address into the ASP label. In fact, I can't get ANYTHING into the ASP label.

I'm trying, in the page_load sub, to do this:

Code:
    Sub Page_Load()
        Dim ClientIP As String = Request.ServerVariables("REMOTE_ADDR")
        Label1.Text = ClientIP
    End Sub

I just get a blank label. I suspect that I'm missing something obvious, like the fact that I'm not allowed to do this during page_load or something.

Even if I do this, I still get a blank label:

Code:
    Sub Page_Load()
        Label1.Text = "Testing"
    End Sub

Thoughts?

Thanks!

Steve
 
If I am, I'm not intentionally.

The first line of my file says this:

Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Does the CodeFile="Default.aspx.vb" mean that I AM using the code behind file, and therefore that I have to do something else?

Truthfully, I wasn't intending on using the code behind for this mini application.

Thanks!

Steve
 
The codefile points to the place where you should be placing your code as seperating code from content is a better and recommended method. Once you've done this, add this to your code behind file:
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = Request.UserHostAddress
    End Sub



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
StevenB said:
...even though I'm a terminal newbie about such things, and NOT a programmer by nature. My programming method is basically taking code from various other places and tweaking it to death.
the proclaimed professionals do the same. it really just comes down to elegance of the modified code:)

before you jump in head first into asp.net I would recommend purchasing a beginner book or reading some online tutorials.

asp.net has some features (some would say problems: viewstate, caching, page life cycle) that are specific to itself. there are also limitations that all web languages face (statelessness, get/post, security).

it could be that Remote_Addr is empty. try this as a test
Code:
Sub Page_Load()
  if not Page.IsPostback then
     Dim ClientIP As String = Request.ServerVariables("REMOTE_ADDR")
     Label1.Text = "IP Address is " & ClientIP
  end if
End Sub
your output should be one of the following
[tt]
IP Address is
or
IP Address is 1.2.3.4
[/tt]
(fake ip of course)
also place some static text on the page as well just to make sure your page is loading correctly. something like this in the markup (within the form tags)
Code:
<p>testing the label</p>
<asp:Label id="Label1" runat="server" />

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Beautiful, it works like a charm!

For now, I'm going to ignore the whole mess around trapping the server IP versus the actual user's IP address. This is good enough for now.

So, now a follow-up question.

I'd hoped to capture this value in a variable, so that I could ultimately write it to the database when the user submits the form. Would I simply declare this as part of the page_load in the code behind file, like this?

Code:
Dim ClientIP As String = Request.ServerVariables("REMOTE_ADDR")

Thanks!

Steve
 
You wouldn't necessarily have to store it in a variable unless you are using it in multiple places or need to modify it. When you add your parameter to your database command object, simply pass the IP Address in directly.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
OK folks, thanks very much for your help. I think this gets me to where I need to be right now, although I suspect I'll have more questions once I get to the part where I need to update the database! :)

Jason, thanks for your information as well! I actually have been working through a beginner's book on ASP.NET, but I'm getting a bit ahead of myself. :D

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top