I’m looking to get the “.Text” value of any webcontrol, regardless of what type it is (TextBox, DropDownList, CheckboxList, etc.).
I’d hope there is a simple way to do this, but if I just try to find it based on the name and as a WebControl,
foreach (string field_name in field_names_use)
{
WebControl match_list = Page.FindControl(field_name) as WebControl;
if (match_list != null && survey_vals.ContainsKey(field_name) == false)
{
survey_vals.Add(field_name, match_list.Text);
}
}
This doesn’t work. I’d rather not check for DropDownList, TextBox, CheckboxList, etc and just grab “controlID.Text”, much like JavaScript can grab any value with “document.getElementById(“controlID”).value”.
No, there is no function like that as TextBox has a .Text whereas the other controls do not. What you will have to do is write an extension method that checks the type and returns watever is appropriate.
public static string GetText (this Control ctrl)
{
//
}
then inside there use ctrl.getType() or type(ctrl) to get the control type. then do an if/switch statment. if type.Contains(“TextBox”) return ctrl.Text. If its a DropDownList return ctrl.SelectedItem.Text; etc
Then on any control you can use textbox1.GetText(); or ddl1.GetText();
Thanks Nightstalker. It makes sense. Crappy, but makes sense.
I also searched the web and found that I can sort through Property Types and Names of each control, since they are objects, and return the value, based on the concept of Reflection.
Not all web controls even have a Text property: HiddenField, for instance, has a Value property instead, and most list-based controls will have a SelectedText property.
For those that do, they implement the ITextControl interface. You can therefore use something like:
string text;
if (ctl is ITextControl)
text = ((ITextControl)ctl).Text;
// do something with 'text'...
But, as stated, that won’t help you with controls like HiddenField, in which case you’re better off going with the extension method.
Thanks. True, it’s there - but the human factor is invaluable, esp when you’re asking a question and don’t really know where to start. Hitting the F3 key is still pretty far off from how those neurons are able to build connections through a few key words and phrases.
(English Language + Brain) > (.NET + MSDN Tutorial)
Most of the input controls are marked with an [ControlValueProperty(“PropertyName”)] attribute… so if you want to generalize the code to handle more controls what you can do is check if the control type is marked with the attribute get the PropertyName from that attribute and get the value of that property by reflection.
I’ve personally found the reference libraries more useful than anything. (That, and right-clicking on a type in VS and selecting “Go to definition”.) You can trace a control’s ancestry and check out the interfaces they implement. I spent about half a day doing that (and another couple doing it again) and now I have a fairly clear idea of most server controls’ makeup.
That’s how it starts. You’re presented with a problem, and you methodically explore the nature of that problem until you have a solution. Tutorials are good too, they are often a first port of call when investigating new topics (good example for me was the 4guysfromrolla series on ASP.NET’s membership/role/profile providers) and getting a frame of reference, and after that it’s best to stick to the hard reference material.
They key is taking a bit of time to develop your problem-solving skills and learning to utilise existing resources. This is what I try to encourage because it beats having to rely on SitePoint or some other forum to help you every time you get stuck. (Not accusing the OP of this, but I’ve noticed a distinct pattern here that worries me a bit.)
I agree with Serenarules on this one. You can use that for all Text controls only. You should also check. Lists like checkbox list, dropdownlists etc will also implement their own interface which you can use in the same way as the ITextControl.
public static string GetText (this Control ctrl)
{
if (ctrl is ITextControl)
{ return ((ITextControl)ctrl).Text; }
else if... etc
}
What do you do for list controls that allow more than one selected value? The ASP.NET framework in some cases (and poorly in my opinion) just grabs the first selected value it finds and ignores the rest.
You would probably have to return a delimited list, but what do you use as a delimiting character and what do you do if a text value contains that delimiting character?