phil 的个人资料Phil Harvey's .NET Space日志列表 工具 帮助

日志


5月16日

Talking about Article: Integrating an ASP.NET e-commerce system with PayPal's Instant Payment Notification (IPN)

This article demonstrates how to validate PayPal IPN requests in asp.net. My only comment is once you have received the validation response from PayPal you need to check it equals "VERIFIED". Otherwise, it might have returned "INVALID", which probably means someones messing about you.

Quote

Article: Integrating an ASP.NET e-commerce system with PayPal's Instant Payment Notification (IPN)
PayPal IPN is an example of business to business system communication via HTTP. Essentially, IPN enables your system to receive real time notification of a payment from PayPal.The primary requirement for IPN to work is to accept a HTTP post from PayPal containing information on a transaction, and then to return the same HTTP post back to PayPal (as a receipt) with an additional element appended.Receiving information is done using the form collection: Request.Form. Posting back programmatically is the part that this article covers.In the world of ASP and COM, the standard approach was to use MSXML to send HTTP posts programmatically. While this approach is still available for a .NET based system because all COM components can be accessed via .NET COM interop, it is strictly not a pure .NET solution.In the world of .NET, the desired functionality can be achieved using the classes System.Net.HttpWebRequest and System.Net.HttpWebResponse. These classes enable you to interact directly with the HTTP protocol.
1月5日

Get the remote callers IP address in a web method

For the purpose of logging, auditing and extra security is can be very useful to know the remote parties IP address when a web method is called in a .NET Web Service.

The remote address can be obtained from the UserHostAddress property of the HttpRequest object. This is accessible via the HttpContext object, which is available as a base property of any WebService class. In any web method just call Me.Context.Request.UserHostAddress.

A web method to echo the remote callers IP address

Imports System.Web.Services

<WebService(Description:="Echo Requestor's IP address")> _
Public Class EchoIP
    Inherits System.Web.Services.WebService

    <WebMethod(description:="Echo Requestor's IP address")
    Public Function echo() As String
        Return Me.Context.Request.UserHostAddress
    End Function
End
Class

1月4日

User friendly forms in ASP.NET

Many web pages contain a very simple form containing just a text box and a button. For example Google's front page. When a user encounters such a simple form, they expect to be able to quickly make an entry and submit the form. The user certainly does not want to click or tab into the textbox, type, then click the button with the mouse. They want to type and press enter.

Implementing this type of functionality in ASP.NET is not entirely obvious. The trick is to use the TextChanged event on the textbox. The name of this event is misleading, because it will only postback when the user presses the enter key while the textbox has focus.

You will want pressing enter or clicking the button to perform the same action, so make your code look something like the following and you'll be sorted.

Submit a form with a button click or an enter key press

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Me.DoSubmit
End Sub

Private Sub txtCriteria_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCriteria.TextChanged
    Me.DoSubmit()
End Sub

Private Sub DoSubmit()
    ' Perform the form action...
End Sub

You may also want to give default focus to the textbox when the page loads so the user can type straight away. Add a property similar to the following to the body tag of the ASP.NET page. 

Give default focus to a textbox on an ASP.NET page

<body onload="document.Form1.txtCriteria.focus()">

12月5日

Force page structure with inheritance

I've been doing a fair bit of ASP.NET work lately. I like ASP.NET but I have issues with the Visual Studio IDE so I prefer to build my sites at runtime, adding structure and components via code. The fine details of what the page actually looks like can be dictated via CSS style sheets.

As a design methodology it works as long as your code structure makes sense. A good object-orientated structure goes a long way.

One of the techniques you can employ using this method is page inheritance in order to enforce a standard page structure throughout your entire site. In this example I will show how to use page inheritance to display a standard header and footer on every page.

Firstly you need to make a base class which draws the page structure and overwrites some methods to enforce the structure.

Public MustInherit Class BasePage
    Inherits
System.Web.UI.Page

    ' The page body goes into this table cell.
    Private _pageBodyCell As New
TableCell()

    ' Override the Controls property so it returns
    ' the page body table cell.
    Public Overrides ReadOnly Property Controls() As
ControlCollection
        Get
            Return Me
._pageBodyCell.Controls
        End
Get
    End
Property

    ' Use constructer to draw the page structure.
    Sub New
()
        ' The page table structure.
        Dim pageTable As New
Table()

        ' Create page header.
        pageTable.CssClass = "pageTable"

        Dim pageHeaderRow As New
TableRow()
        pageTable.Controls.Add(pageHeaderRow)
        Dim pageHeaderCell As New
TableCell()
        pageHeaderCell.CssClass = "headerCell"
        pageHeaderRow.Controls.Add(pageHeaderCell)

        ' Draw page header.
        Me
.drawPageHeader(pageHeaderCell)

        ' Create page body.
        Dim pageBodyRow As New
TableRow()
        pageTable.Controls.Add(pageBodyRow)
        _pageBodyCell.CssClass = "bodyCell"
        pageBodyRow.Controls.Add(_pageBodyCell)

        ' Create page footer.
        Dim pageFooterRow As New
TableRow()
        pageTable.Controls.Add(pageFooterRow)
        Dim pageFooterCell As New
TableCell()
        pageFooterCell.CssClass = "footerCell"
        pageFooterRow.Controls.Add(pageFooterCell)

        ' Draw page footer.
        Me
.drawPageFooter(pageFooterCell)

        ' Add table to page.
        MyBase
.Controls.Add(pageTable)
    End
Sub

    Private Sub drawPageHeader(ByVal header As
TableCell)
        Dim headerLabel As New
Label()
        headerLabel.Text = "My ASP.NET Website"
        headerLabel.CssClass = "headerLabel"
        header.Controls.Add(headerLabel)
    End
Sub

    Private Sub drawPageFooter(ByVal footer As
TableCell)
        Dim footerLabel As New
Label()
        footerLabel.Text = "Copyright etc"
        footerLabel.CssClass = "footerLabel"
        footer.Controls.Add(footerLabel)
    End
Sub

End
Class

For each web form you create make it inherit BasePage and the structure will be applied to it.

Public Class MyPage
    Inherits
 BasePage

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase
.Load

    Dim bodyBlock As New
Literal()
    bodyBlock.Text = "My body"
    Me
.Controls.Add(bodyBlock)

    End
Sub

End
Class