How to Safely Get WebForm Control Value

So I created the nifty little Function below:

Private Function GetWebFormValue(ByRef WebForm As System.Collections.Specialized.NameValueCollection, _
                                   ByVal ControlName As String) As String
  Try
    Return WebForm(ControlName).ToString
  Catch
    Return String.Empty
  End Try
End Function

Here is how to use it:

pstrTemp = GetWebFormValue(Request.Form, "txtZipCode")

If you have an easier, better way to do this same thing, please let me know. Best of all, this will not cause an exception!!! 

Tip Submitted By: David McCarter

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s