25

I am able to get the IDs of all the controls of a page and also their type, in the page when i print it it shows

myPhoneExtTxt Type:System.Web.UI.HtmlControls.HtmlInputText

this is generated based on this code

    foreach (Control c in page)
    {
        if (c.ID != null)
        {
            controlList.Add(c.ID +" Type:"+ c.GetType());
        }
    }

But now i need to check its type and access the text in it if its of type HtmlInput and i am not quite sure how to do that.

Like

if(c.GetType() == (some htmlInput))
{
   some htmlInput.Text = "This should be the new text";
}

how can i do this, i think you get the idea?.

2 Answers 2

56

This should be all you need if I get what you're asking:

if (c is TextBox)
{
  ((TextBox)c).Text = "This should be the new text";
}

If your primary goal is to just set some text:

if (c is ITextControl)
{
   ((ITextControl)c).Text = "This should be the new text";
}

In order to support a hidden field as well:

string someTextToSet = "this should be the new text";
if (c is ITextControl)
{
   ((ITextControl)c).Text = someTextToSet;
}
else if (c is HtmlInputControl)
{
   ((HtmlInputControl)c).Value = someTextToSet;
}
else if (c is HiddenField)
{
   ((HiddenField)c).Value = someTextToSet;
}

Additional controls/interfaces would have to be added to the logic.

5
  • does this include if the input type is Hidden? Commented Jul 12, 2012 at 19:28
  • Unfortunately, no. HiddenFields are nasty little bastards in that they do not inherit from much of anything useful and must be accounted for directly. I have edited my answer to include support. Commented Jul 12, 2012 at 19:33
  • Also consider using the as operator in type checks like these. Commented May 21, 2015 at 6:12
  • 1
    @JeppeStigNielsen I'm curious how you would implement that when testing how to perform a one-liner versus N-possible conversions? Would you simple define N holders beforehand, then try to assign and check null for each one? Commented May 21, 2015 at 12:39
  • It is of course in itself a code smell if we need a prioritized list of types like here. But I agree it can be hard to use as with that else-if chain. You could factor it out into a method and use return instead of the else. For example: var textControl = c as ITextControl; if (textControl != null) { textControl.Text = x; return; } var htmlInputControl = c as HtmlInputControl; if (htmlInputControl != null) { htmlInputControl.Value = x; return; } etc. Commented May 25, 2015 at 18:35
-1
private Entry GetFocusedTextBox(Layout parent)
{
  
    foreach (var control in parent.Children)
    {
        if(control == null) continue;

        if (control is Entry && (control as Entry).IsFocused )
            return control as Entry;
        else if (control is Layout)
        {
            var result = GetFocusedTextBox((control as Layout)); 
            if (result != null) return result;
        }
    }
    return null;
}
1
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community? Commented Jul 31, 2023 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.