Łatwe pobieranie danych HTML PostBack w ASP.NET
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.
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.