c# - How to use for loop to scan all controls in ASP .NET page? -
foreach (control ctrl in page.controls) { if (ctrl textbox) { if (((textbox)(ctrl)).text == "") { helpcalss.messagebox("please fill empty fields", this); return; } } }
i'm using asp.net , have inserting page texboxes , need check if texboxes in page empty , if need show message box empty textbox
here a article on it, below modified version collects controls given property
public list<control> listcontrolcollections(page page, string propertyname) { list<control> controllist = new list<control>(); addcontrols(page.form.controls, controllist, propertyname); return controllist; } private void addcontrols(controlcollection controlcollection, list<control> controllist, string propertyname) { foreach (control c in controlcollection) { propertyinfo propertytofind = c.gettype().getproperty(propertyname); if (propertytofind != null) { controllist.add(c); } if (c.hascontrols()) { addcontrols(c.controls, controllist, propertyname); } } }
to use it:
list<control> controllist = listcontrolcollections("text"); (i=0; < controllist.length; i++) { if (controllist[i].text == string.empty) { // logic here } else { // logic here } }
Comments
Post a Comment