Checking for the control type

前端 未结 1 1585
心在旅途
心在旅途 2021-01-03 21:42

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


        
相关标签:
1条回答
  • 2021-01-03 22:16

    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.

    0 讨论(0)
提交回复
热议问题