Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxlist not being picked up as control

Status
Not open for further replies.

daveonion

Programmer
Aug 21, 2002
359
GB
Hi I have wrote a simple function to clear all fields, in essence to reset the form, as below
For Each ctl As Control In parent.Controls

If ctl.Controls.Count > 0 Then
ResetControlValues(ctl)
Else
Select Case (ctl.GetType().ToString())
Case "System.Web.UI.WebControls.TextBox"
CType(ctl, TextBox).Text = ""
Case "System.Web.UI.WebControls.checkboxlist"
For Each li As ListItem In CType(ctl, CheckBoxList).Items
li.Selected = False
Next
End Select
End If
Next ctl

The problem is if there is a checkboxlist control on the form it doesn't actually include it as being a control, can anyone advise why?

Thanks
 
I would approach this differently
1. use javascript form.reset() to reset input fields on the client
2. navigate back to the page (instead of a postback) to "reset" the form. something like
Code:
<input type="button" onclick="window.location='ThisPage.aspx'"/>
both are simpler than cycling through the controls, determining the type of control, and undoing the changes previously made.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
In order to get all the controls on a page, you have to use recusion. For your situation, I would follow Jason's advice, much simpler and maintainable. There are times when you may need to recurse all controls, but this is not the time, keep it simple.
 
thanks for your replys, the problem with the complete reset is that i have various fields that will keep there values, i.e. when a new customer is found i want to reset all the previous updated fields but keep the customer details. A complete reset would lose this information,

Thanks
 
I recently wrote something similar to this to reset a group of controls, but problems arose with the amount of time it takes for this script to run. Checking through every control on the page is a lot of server load for the page to process.

In the end I simply wrote a hardcoded list to clear all the controls.

While the recursive list of controls is more maintainable in the long run, it slows down the page which is worse for the usability, and in my experience usability ranks way above maintainability of a page.

Just my thoughts on the situation.

~Ben
Occasional sparks in a darkened room
 
I agree with Ferrian, it would seem from your comments that you know which fields you want to reset and therefore there isn't really a need for the recursion. You could simply have a javascript function that uses an array of objects (or object names) and performs the reset without any need to involve the server.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top