Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Bespoke Software
    • Database Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft 365
      • Enabling the Multi Factor Authentication Application
      • Office 365 DNS Settings Generator
    • IT Hardware
    • Social Media Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Blocking Blank Senders
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Japanese
      • Translator English-Portuguese
      • Translator English-Spanish
      • Translator English-Turkish
    • Portfolio
    • Regulatory
    • Team
      • Chester Copperpot
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
English (EN-GB)English (EN-US)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)

Easily Getting HTML PostBack Data in ASP.NET

Shared modules we use to return sanitised input data from user forms without the need for Session State, complex PostBack handling and without errors in dropdown lists

Flexible, powerful and secure

There have been a few instances in application development phases where we have needed to get post data earlier than the normal life cycle of the controls. While not a major coding issue to work around, we wanted to make it as small a module as possible.

When combined with a switching function, you can tell the application to pull the data from a data source or the postback depending on situation.

We have used this in production environments in a few cases, and it is used liberally within our Ousia CMS application;

  • Creating a fully updatable asp:GridView control, almost simulating an Excel sheet
  • Setting custom form asp:DropDown selected values without errors
  • Removing session state from applications to reduce payload
  • Early application updating (before controls are fully created)
  • Sanatise your data, returns the right data type.

We will set the code out below, and then go into some detail after.

LoaderASP.NET

ASP.NET

    Public Shared Function GetPostBackValue(ByVal r As HttpRequest, c As Control) As String        Dim ret As String = ""        If Not IsNothing(r.Form) Then If Not IsNothing(r.Form.Item(c.UniqueID)) Then ret = r.Form.Item(c.UniqueID)        Return ret    End Function    Public Shared Function GetPostBackValueString(ByVal r As HttpRequest, c As String) As String        Dim ret As String = ""        If Not IsNothing(r.Form) Then If Not IsNothing(r.Form.Item(c)) Then ret = r.Form.Item(c)        Return ret    End Function    Public Shared Function GetPostBackDate(ByVal r As HttpRequest, c As Control) As String        Dim ret As String = ""        If Not IsNothing(r.Form) Then If Not IsNothing(r.Form.Item(c.UniqueID)) Then ret = r.Form.Item(c.UniqueID)        Dim retS As String = ""        If IsDate(ret) Then retS = ret        Return retS    End Function    Public Shared Function GetPostBackDateString(ByVal r As HttpRequest, c As String) As String        Dim ret As String = ""        If Not IsNothing(r.Form) Then If Not IsNothing(r.Form.Item(c)) Then ret = r.Form.Item(c)        Dim retS As String = ""        If IsDate(ret) Then retS = ret        Return retS    End Function    Public Shared Function GetPostBackCheck(ByVal r As HttpRequest, c As Control) As Boolean        Dim ret As String = ""        If Not IsNothing(r.Form) Then If Not IsNothing(r.Form.Item(c.UniqueID)) Then ret = r.Form.Item(c.UniqueID)        Dim retS As Boolean = False        If ret = "on" Then retS = True        Return retS    End Function    Public Shared Function GetPostBackCheckString(ByVal r As HttpRequest, c As String) As Boolean        Dim ret As String = ""        If Not IsNothing(r.Form) Then            If Not IsNothing(r.Form.Item(c)) Then                ret = r.Form.Item(c).ToString            End If        End If        Dim retS As Boolean = False        If ret = "on" Or ret = "True" Or ret = "true" Or ret = "1" Then retS = True        Return retS    End Function    Public Shared Function UpdateValueSwitch(u As Boolean, d As String, p As String) As String        Dim ret As String = ""        If u = True Then            If p = "" Then                ret = d            Else                ret = p            End If        Else            ret = d        End If        Return ret    End Function    Public Shared Function ClearInt(v As String) As Int64        Dim i As Int64 = 0        If IsNumeric(v) Then i = v        Return i    End Function    Public Shared Function ClearDou(v As String) As Double        Dim i As Double = 0        If IsNumeric(v) Then i = v        Return i    End Function    Public Shared Function SQLStr(v As String) As String        Dim i As String = "NULL"        If Not IsNothing(v) Then If v <> "" Then i = "N'" + Replace(v, "'", "''") + "'"        Return i    End Function    Public Shared Function SQLInt(v As String) As String        Dim i As String = "NULL"        If Not IsNothing(v) Then If IsNumeric(v) = True Then i = Replace(v, "'", "''")        Return i    End Function    Public Shared Function SQLDate(v As String) As String        Dim i As String = "NULL"        If Not IsNothing(v) Then If IsDate(v) = True Then i = "'" + Date.Parse(v).ToString("yyyy-MM-dd HH:mm:ss") + "'"        Return i    End Function    Public Shared Function SQLBit(v As String) As String        Dim i As String = "0"        If Not IsNothing(v) Then If v = "True" Then i = "1"        Return i    End Function

What it is doing

We know that the majority of people come here to get some examples and then adapt it to there needs, but those of you who want to work out what it is doing keep reading.

GetPostBackValue, GetPostBackCheck and GetPostBackDate will return the relevant value from the values passed in, which are the request (contains all posted information), and the control (the form value). The check and date versions simply check the datatype provided is relevant.

ClearInt, ClearDou, ClearDate return the relevant datatype from a string, used as a basic switch to assign data that needs to be a set type.

SQLStr, SQLInt, SQLBit and SQLDate are used as modifiers to return strings for dynamic SQL preventing any potential SQL injection attacks.

UpdateValueSwitch simply sets a return value dependant on the true or false provided, with the d (default) value overriding the p (postback) value when the p value is blank.

Set an asp:TextBox value to a number

MyTextbox.Text = ClearDou(UpdateValueSwitch(IsPostBack,dr.Item(1).ToString, GetPostBackValue(Request,MyTextbox)))

Pull a date from an asp:TextBox and update SQL

Dim u As String = SQLDate(GetPostBackDate(Request, TextBoxDate))Using mi As New SqlConnection(str)    mi.Open()    Using com As New SqlCommand("EXEC [UpdSproc] " + u + "", mi)        com.ExecuteNonQuery()    End UsingEnd Using

Author

Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+442392064871info@claytabase.co.ukClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader