Web design and hosting, database, cloud and social media solutions that deliver business results
  • Rozwiązania biznesowe
    • Oprogramowanie
    • Projektowanie stron
      • Bezpieczeństwo witryny
      • Lokalizacja i tłumaczenie stron internetowych
    • Usługi baz danych
      • Integracja danych
      • Przybory
    • Zrobotyzowana automatyzacja procesów
    • Usługi biznesowe
      • Microsoft Azure
    • Media Społecznościowe
    • Microsoft Office
  • Akademia
    • Nasze środowisko testowe
    • Nauka projektowania baz danych
      • Używanie funkcji datownika SQL Server
      • SQL Server 2008 planu konserwacji
      • Korzystanie z funkcji serwera SQL Server
      • Korzystanie z programu SQL Server Pivot-Unpivot
      • Używanie dat serwera SQL
    • Nauka projektowania stron internetowych
      • CSS
      • ASP-NET
      • Korzystanie z JavaScript
    • Nauka w chmurze i usługi IT
      • Błąd harmonogramu zadań 2147943645
      • Żądanie SSL i generowanie pliku PFX w OpenSSL Simple Steps
  • O
    • Portfel Pracy
    • Zespół
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • 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)

Łatwe pobieranie danych HTML PostBack w ASP.NET

Współdzielone moduły, których używamy do zwracania oczyszczonych danych wejściowych z formularzy użytkowników bez potrzeby stanu sesji, złożonej obsługi PostBack i bez błędów w listach rozwijanych

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 Using
End Using

Author

Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

RSSLoginLink Polityka Cookiemapa strony

Social Media

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

Get in Touch

+442392064871info@claytabase.comClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom
Ustawienia na tej stronie są ustawione tak, że wszystkie pliki cookie. Mogą one być zmieniane na naszej polityki i ustawień strony z ciasteczkami. Dalsze korzystanie z tej strony, wyrażasz zgodę na korzystanie z plików cookie.
Ousia Logo
Logout
Ousia CMS Loader