ASP.NET如何在后台获取

SoulRed 2009-08-16 12:55:18
在.cs文件中获取某一ASP控件元素类别的所有元素集合。

并且可以用[]访问

...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
SoulRed 2009-08-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
遍历页面控件
protected void Find(Control c)
    {
      if (c.Controls != null)
      {
        foreach (Control x in c.Controls)
        {
            Type t= x.GetType();
            Find(x);
        }
      }
    }
[/Quote]
以下是微软的。。。看看先~ 一头雾水呀
C# 复制代码 
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html >

<head id="head1" runat="server">
<title>Using the Controls Collection in a Web Form</title>

<script language="c#" runat="server">

private void ChangeBtn_Click(object sender, EventArgs e)
{
foreach(Control c in Page.Controls)
{
if (c.Controls.Count > 0)
{
foreach(Control c2 in c.Controls)
{
if (c2.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
myspan.InnerHtml = ((TextBox)c2).Text;
((TextBox)c2).Text = "";
}
}
}
}
}

</script>

</head>
<body>
<form id="form1" runat="server">
<table width="80%"
border="1"
cellpadding="1"
cellspacing="1">
<tr>
<td align="center" style="width:50%;">
<asp:TextBox id="MyTextBox"
text="Type something here"
runat="server"/>
</td>
<td align="center" style="width:50%;">
<span id="myspan" runat="server"> </span>
</td>
</tr>

<tr>
<td colspan="2" align="center">
<input id="changebtn"
type="submit"
onserverclick="ChangeBtn_Click"
value="move your text"
runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
wuyq11 2009-08-16
  • 打赏
  • 举报
回复
遍历页面控件
protected void Find(Control c)
{
if (c.Controls != null)
{
foreach (Control x in c.Controls)
{
Type t= x.GetType();
Find(x);
}
}
}
  • 打赏
  • 举报
回复
不过我从来不在应用程序中使用这种手法,都是在自动化测试才使用。在应用程序中,使用FindControl就可以,没有必要使用如此“重量”的查询方法。
  • 打赏
  • 举报
回复
假如要选择的控件在GridView里边一组已经被选中的CheckBox中,并且其有一个自定义属性(Attributes)“本行主键”绑定值为“1234”,就可以写:

var chk=GridView1.GetAllControls().Where(c=> c is CheckBox).Select(c=> (CheckBox)c)
.Where(c=> c.Checked=true && c.Attributes["本行主键"]=="1234")
.FirstOrDefault();

如果返回null,则没有匹配的;否则返回第一个匹配的CheckBox。
  • 打赏
  • 举报
回复
添加以下class
public static class Extensions
{
public static IEnumerable<Control> GetAllControls(this Control ctrl)
{
foreach (Control c in ctrl.Controls)
yield return c;
foreach (Control c in ctrl.Controls)
foreach (Control cc in GetAllControls(c))
yield return cc;
}
}


这样你就可以这样使用(注意要添加对System.Linq的引用)
var res1 = this.GetAllControls().Where(c => c is TestBox);
var res2 = this.GetAllControls().Where(c => c is DropDownList && ((DropDownList)c).SelectedValue=="1234");
IHandler 2009-08-16
  • 打赏
  • 举报
回复
获取Form中的所有控件,然后遍历,使用反射来判断类型
IHandler 2009-08-16
  • 打赏
  • 举报
回复
Request.Form[]

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧