我能够获取页面所有控件的 ID 及其类型,当我打印它时,它会在页面中显示
myPhoneExtTxt Type:System.Web.UI.HtmlControls.HtmlInputText
这是根据这段代码生成的
foreach (Control c in page)
{
if (c.ID != null)
{
controlList.Add(c.ID +" Type:"+ c.GetType());
}
}
但现在我需要检查其类型并访问其中的文本(如果其类型为 HtmlInput),但我不太确定该怎么做。
喜欢
if(c.GetType() == (some htmlInput))
{
some htmlInput.Text = "This should be the new text";
}
我该怎么做,我想你明白了?.
如果我明白你的要求,这应该就是你所需要的:
if (c is TextBox)
{
((TextBox)c).Text = "This should be the new text";
}
如果您的主要目标只是设置一些文本:
if (c is ITextControl)
{
((ITextControl)c).Text = "This should be the new text";
}
为了支持隐藏字段:
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;
}
必须将额外的控件/接口添加到逻辑中。
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;
}