How to find out the host address of the caller of webmethod

by Andrew Jackson 13. October 2009 04:58
On the server inside the webservice method call, you have access to the HttpContext object. Using HttpContext.Current, you have access to a number of objects (including the request object).

From the request object you can find out information, including the host address from which the request comes from.

If you need to map this address to a hostname , you can use UserHostName property instead

' Example web method
<WebMethod()> _
Public Function TestMessage( ) as String

Trace.WriteLine("GetMessage called from : " + HttpContext.Current.Request.UserHostAddress + " " + HttpContext.Current.Request.UserHostName )

Return "Hello"

End Function

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development | VB.net

Quick and dirty asp.net DropDownList selection required validation

by Andrew Jackson 23. October 2006 12:00

I needed to do the usual thing, present a drop down list with the first item as a fake one being something like "-- Choose a Category --"

I jumped straight in and started thinking about custom validation etc etc and lost the will to live very quickly - considering this was just supposed to be a quick knock up project.

Then I found this little snippet, using the RequiredFieldValidator you can specify an InitialValue, put this to be your "-- Choose a Category --" and your away, it won't validate unless it's something other than that!

OK, it's not the most elegant solution, I'd prefer value, or even item being 0 comparison but it does in a push and worth remembering.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

Dynamic User Controls on an ASP.net Page

by Andrew Jackson 23. January 2005 10:03

Spent quite a lot of time today getting to grips with dynamically placing user controls on a page at run time.  I wanted a series of buttons which were determined by database entries and these buttons needed to have code to display a page when clicked.  If your wondering how to do this yourself you need to know about Page.LoadControl and AddHandler;

  • Create a sub for the click handler with the usual ByVal sender As System.Object, ByVal e As System.EventArgs parameters.
  • Use controlvariable = Page.LoadControl to load an ascx into a variable (defined to be the type of the user control)
  • Assign the controlvariable.id to something useful
  • Do a me.controls.add controlvariable to add it to the page (or a me.container.controls.add if you've got your page divided up into tables or the like)
  • Do an AddHandler ctype(me.controls(controlindex), control type).Clicked, AddressOf nameofclickhandler

The click handlers sender property will have the control (as type object so you have to cast it again) so you can inspect the ID and find out which button was clicked and act accordingly.  For my example I made a custom control which had a few basic properties of a button object exposed plus an additional one for the page name associated with the button.  That way it was instantly available from the object passed into the click event handler and i could do a response.redirect with it.

A lot of work casting back and forth for a newbie to this but it works, would recommend putting these controls into a table/div/whatever block so you can keep track of the control indexes, also found it useful to make a quick hash table to store the control objects and their ID's for easy access to each control.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

ASP.net Training

by Andrew Jackson 23. January 2005 10:02

Spent the last week on an ASP.net training course with Pygmalion.
Good course, even though I knew a little of asp.net before I started I learnt a lot and went away confident at writing apps from scratch rather than cobbling together bits.

A big thing that came out of it for me was a renewed determination to fully embrace Option Strict in all my .net code, being from a VB background I'd enjoyed years of clumsy programming letting the compiler work it out for me.  Spent the weekend prototyping a system for work and stuck Option Strict on and forced myself.  It was annoying at first but I soon found myself making informed decisions on what my code was doing rather than just object.mystical property to object.mystical property and keeping my fingers crossed.

Oh, and the best thing about the course.. great lunches every day, and some interesting people on the course from a wide background, all asking questions that I found useful as well.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

Allowing HTML to be entered into text boxes on ASP.net

by Andrew Jackson 2. November 2004 09:43

ASP.net adds very handy functionality by default, in that it throws an error if your form contains any html on it's submission.  Handy for html injection techniques, but not if your writing an entry form for techies that requires it.

To disable this validation add the following code to your aspx pages, just remember you've done it and the consequences of doing so.

<%@ Page Language="VB" validateRequest="false" CodeBehind= etc etc etc %>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Andrew Jackson

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008,2009 Andrew K. Jackson

RecentComments

Comment RSS