Checking for the control type
Asked Answered
U

2

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?.

Undersized answered 12/7, 2012 at 19:23 Comment(0)
U
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.

Uranometry answered 12/7, 2012 at 19:26 Comment(5)
does this include if the input type is Hidden?Undersized
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.Uranometry
Also consider using the as operator in type checks like these.Indore
@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?Uranometry
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.Indore
P
-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;
}
Phantasmagoria answered 30/7, 2023 at 14:23 Comment(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?Nestorius

© 2022 - 2024 — McMap. All rights reserved.