我能够获取页面所有控件的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";
}
我怎么能这样做,我想你明白了吗?。
答案 0 :(得分:35)
如果我得到你的要求,这应该就是你所需要的:
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;
}
必须将其他控件/接口添加到逻辑中。