facebook  linkedin  Twitter  skype  Rss googlePlus

Some Useful ASP.Net Code Utilities

Jan 03 2004
4056


1. Clear Form Fields

Ever wanted to clear your fields in the form without having to set the text properties or selected items? Well below I have simple method to do this. The
only real catch to this method is your fields have to be in a container. I usually put all my fields in table and set the properties for an ID and the runat
(e.g.

).





Public Sub ClearFields(ByVal parent As System.Web.UI.Control)
Dim c As Control
For Each c In parent.Controls
If c.GetType() Is GetType(TextBox) Then
'is it a textbox?
Dim t As TextBox = c
t.Text = ""
ElseIf c.GetType() Is GetType(HtmlControls.HtmlInputHidden) Then
'is it a hidden text?
Dim h As HtmlControls.HtmlInputHidden = c
h.Value = ""
ElseIf c.GetType() Is GetType(DropDownList) Then
'is it a dropdown list?
Dim d As DropDownList = c
d.ClearSelection()
ElseIf c.GetType() Is GetType(ListBox) Then
'is it a listbox?
Dim l As ListBox = c
l.ClearSelection()
ElseIf c.GetType() Is GetType(RadioButtonList) Then
'is it a radiobutton list?
Dim rl As RadioButtonList = c
rl.ClearSelection()
ElseIf c.GetType() Is GetType(CheckBox) Then
'is it a checkbox?
Dim chk As CheckBox = c
chk.Checked = False
ElseIf c.GetType() Is GetType(CheckBoxList) Then
'is it a checkbox list?
Dim cl As CheckBoxList = c
cl.ClearSelection()
End If
If c.HasControls Then
ClearFields(c)
End If
Next
End Sub





As you can see it is quite easy to modify this method for other controls. It also loops checks to see if there are any child controls so that you could
potentially use a control higher up in the hierarchy. As you can see in the second part of the IF statement you can even use Htmlcontrols.


ElseIf c.GetType() Is GetType(HtmlControls.HtmlInputHidden) Then
'is it a hidden text?
Dim h As HtmlControls.HtmlInputHidden = c
h.Value = ""





I find this a very useful tool and have put it in my Utilities assembly. My utilities assembly is used for those methods I can use over and over again
by separate applications or even just separate pages of the same site. A modification of this could be to clear out Placeholders and Panels.


2. Get Delimited String From Array/ArrayList


This next one is a simple way of getting a delimited string from an array or arraylist. The first piece of code uses an arraylist. This method basically takes
in the arraylist and transforms it to an array. Then it passes it to the second method which returns a string with your delimiter.





Public Function GetDelimStringFromArray(ByVal StringArray As ArrayList, _
ByVal Delimiter As String) As String
Dim newArray As Array = StringArray.ToArray
Return GetDelimStringFromArray(newArray, Delimiter)
End Function




This method is where the work is really done. You just take in the array and loop through each value appending that value to the end of the string and then
placing the delimiter at the end of that. If you don't like having the trailing delimiter you can always chop it off within the method or outside the method.

NOTE: this must be single dimension string array.




Public Function GetDelimStringFromArray(ByVal StringArray As Array, _
ByVal Delimiter As String) As String
Dim tmp As String
Dim newString As New StringBuilder
For Each tmp In StringArray
If tmp.Length > 0 Then
newString.Append(Delimiter)
newString.Append(tmp)
Else
newString.Append(tmp)
End If
Next
Return newString.ToString
End Function




 


3. Find a Control



This one is one that I have seen several people ask about on lists. How do I find a control? Well it is really simple actually. Below is the code to show you
how you can pass it a method the parent control and a the control name you want to find. You could also change this to look for a specific type of control as
well.





Public Function FindControl(ByVal parent As Control, _
ByVal ControlToFind As String) As Control
Dim c As Control
For Each c In parent.Controls
If c.ID = ControlToFind Then
Return c
Else
If c.HasControls Then
FindControl(c, ControlToFind)
End If
End If
Next
End Function




 

4. Hide Tables



This last one here is how I hide and show tables when I am using a wizard type view on my sites. By passing in the parent control and the Table that should
be showing you can hide all the others. This one should be updated and I might soon but right now here it is. Simple as can be.





Public Sub HideTables(ByVal parent As Control, _
ByVal TableToShow As String)
Dim c As Control
For Each c In parent.Controls
If c.ID <> TableToShow Then
If c.GetType Is GetType(Table) Then
c.Visible = False
End If
End If
Next
End Sub





These are just a few methods I use in my utilities assembly. I hope they are some use to you. These are the simple ones that I find myself using quite a bit.
I am not one that likes to type the same things over and over and over, and well you get the idea. Object Oriented programming has fit my style of coding and
I love it.



So until next time. Cya.

About the Author, Staff

You can write for Santry.com, just email us at [email protected] for more information.


blog comments powered by Disqus

Blog Archives

2018 (1)
2016 (1)
2015 (3)
2014 (85)
2013 (15)
2012 (59)
2011 (17)
2010 (2)
2009 (1)
2008 (5)
2007 (22)
2006 (87)
2005 (78)
2004 (69)
2003 (36)
2002 (1)
2001 (16)
2000 (10)
1999 (11)
1997 (1)

Listen to Pat's Podcast

Be sure to check out my podcast for tips on building your digital strategy.

Disclaimer

Any blog posts here are solely the opinions and views of Patrick Santry only, and the authors of the posts. Comments on the posts are opinions of the commenter, and not Patrick Santry.

 

Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.

Popular Articles