检查控件类型 [英] Checking for the control type
本文介绍了检查控件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够得到一个页面,也是他们的类型的所有控件的ID,在页面中,当我打印出来就说明
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
这是基于产生这个代码
foreach (Control c in page)
{
if (c.ID != null)
{
controlList.Add(c.ID +" Type:"+ c.GetType());
}
}
但现在我需要检查它的类型和访问文本,如果类型HtmlInput它的,我不太清楚如何做到这一点。
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.
如同
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?.
推荐答案
这应该是你所需要的,如果我得到你的要求:
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.
这篇关于检查控件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文