c#使用foreach遍历textbox的问题
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSys...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control aa in Page.Controls)
{
if (aa is TextBox )
{
TextBox tb = (TextBox )aa;
tb.Text = "hello world!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "alert(hello)");
}
}
}
}
<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>
这是我的代码,但是为什么不能给压面上的textbox成功遍历并赋值了,谁帮我看下啊。 展开
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control aa in Page.Controls)
{
if (aa is TextBox )
{
TextBox tb = (TextBox )aa;
tb.Text = "hello world!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "alert(hello)");
}
}
}
}
<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>
这是我的代码,但是为什么不能给压面上的textbox成功遍历并赋值了,谁帮我看下啊。 展开
3个回答
展开全部
在页面控件的外层 写一个 form 把这些控件包进去
<form id="form1" runat="server">。。。</form>
然后遍历的时候用
foreach (Control aa in form1.Controls)
{
}
就好了
<form id="form1" runat="server">。。。</form>
然后遍历的时候用
foreach (Control aa in form1.Controls)
{
}
就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
终于出来了 。。。 没有写过这样的代码
protected void Button1_Click1(object sender, EventArgs e)
{
int nPageControls = Page.Controls.Count;
for (int i = 0; i < nPageControls; i++)
{
foreach (System.Web.UI.Control aa in Page.Controls[i].Controls)
{
if ( aa is TextBox)
{
TextBox tb = (TextBox)aa;
tb.Text = "hello world!";
//Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "alert(hello)");
}
}
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
int nPageControls = Page.Controls.Count;
for (int i = 0; i < nPageControls; i++)
{
foreach (System.Web.UI.Control aa in Page.Controls[i].Controls)
{
if ( aa is TextBox)
{
TextBox tb = (TextBox)aa;
tb.Text = "hello world!";
//Page.ClientScript.RegisterStartupScript(this.GetType(), "1", "alert(hello)");
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没按按钮吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询