foreach(Control ctrl in Page.Controls) { switch(ctrl.GetType.ToString()) { case "System.Web.UI.TextBox":break; case "System.Web.UI.Lable":break; .... }
}
如果页面严谨的话,貌似可以当XML文件来解析。没细想,不知道可否。
因为value和id属性的顺序可能不一样 所以需要再嵌套一次正则式将其分别取出来 如下我写的 测试通过 using System.Text.RegularExpressions; protected void GetButtonElems(string str) { RegexOptions op = RegexOptions.IgnoreCase | RegexOptions.Singleline; Regex reg = new Regex("(<input[^>]*type=['\"]?button['\"]?[^>]*>)+", op); MatchCollection mcs = reg.Matches(str); foreach (Match mc in mcs) { if (mc.Success) { string strOut = ""; Group g = mc.Groups[1]; Response.Write(g.Value); Regex reg2 = new Regex("id=['\"]?([^'\"\\s]*)", op); if (reg2.Match(g.Value).Success) { strOut += "id=" + reg2.Match(g.Value).Groups[1] + " "; } else { strOut += "id is null "; } reg2 = new Regex("value=['\"]?([^'\"\\s]*)", op); if (reg2.Match(g.Value).Success) { strOut += "text=" + reg2.Match(g.Value).Groups[1] + "<br> "; } else { strOut += "text is null <br>"; } Response.Write(strOut); } else { Response.Write("no button element"); } } }输出到页面时会将button绘在页面上
{
var showStr='';
var elems=document.getElementsByTag('input');
if(elems==null) {alert('no input element!');}
else
{
for(var i=0;i<elems.length;i++)
{
if(elems[i].type='button')
{
showStr+=i+':id='+elems[i].id+',text='+elems[i].value+'<br>';
}
}
document.write(showStr);
}
}
{ switch(ctrl.GetType.ToString())
{
case "System.Web.UI.TextBox":break;
case "System.Web.UI.Lable":break;
....
}
}
using System.Text.RegularExpressions;
protected void GetButtonElems(string str)
{
RegexOptions op = RegexOptions.IgnoreCase | RegexOptions.Singleline;
Regex reg = new Regex("(<input[^>]*type=['\"]?button['\"]?[^>]*>)+", op);
MatchCollection mcs = reg.Matches(str);
foreach (Match mc in mcs)
{
if (mc.Success)
{
string strOut = "";
Group g = mc.Groups[1];
Response.Write(g.Value);
Regex reg2 = new Regex("id=['\"]?([^'\"\\s]*)", op);
if (reg2.Match(g.Value).Success)
{
strOut += "id=" + reg2.Match(g.Value).Groups[1] + " ";
}
else
{
strOut += "id is null ";
}
reg2 = new Regex("value=['\"]?([^'\"\\s]*)", op);
if (reg2.Match(g.Value).Success)
{
strOut += "text=" + reg2.Match(g.Value).Groups[1] + "<br> ";
}
else
{
strOut += "text is null <br>";
}
Response.Write(strOut);
}
else
{
Response.Write("no button element");
}
}
}输出到页面时会将button绘在页面上