我正在用C#.NET编写一个网页。在javascript中有一个名为GetElementsByTagName的函数...这对从.aspx页面调用的javascript很有用。我的问题是,有什么方法可以从我的C#代码隐藏中获得这种功能吗?
-
那些好奇的场景:我使用了asp:repeater来生成很多按钮,现在我实际上是在尝试创建一个单击所有按钮的按钮。我尝试将所有按钮存储在列表中,但是在每次回发时都会清除列表,所以我想我可以尝试上面的方法。
答案 0 :(得分:3)
试试这个:
foreach (Control ctl in myRepeater.Controls)
{
if (ctl is Button)
{
((Button)ctl).Click();
}
}
... HTH
答案 1 :(得分:2)
FindControl(),或遍历页面上的控件...
For each ctl as Control in Me.Controls
If ctl.Name = whatYouWant Then
do stuff
Next 'ctl
- 如果您要创建控件,则应设置其ID
Dim ctl as New Control()
ctl.ID = "blah1"
等...
答案 2 :(得分:1)
好吧,您可以使用页面的FindControl方法找到控件,但是Repeater元素的名称由.net生成。
顺便说一句,如果你真的想,你可以在页面的ViewState中存储按钮列表(或者可能是它们的名字列表)。
答案 3 :(得分:0)
每当您进行任何回发时,都会重新创建所有,包括数据绑定控件。
如果您的列表消失了,那么按钮控件也是如此。当然,除非您重新创建它们,否则您应该重新创建列表。
答案 4 :(得分:0)
通过点击它们,我不确切地知道你的意思。但是,以下代码对您有何影响?我不知道,我还没有测试......
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in GetControlsByType(this, typeof(TextBox)))
{
//Do something?
}
}
public static System.Collections.Generic.List<Control> GetControlsByType(Control ctrl, Type t)
{
System.Collections.Generic.List<Control> cntrls = new System.Collections.Generic.List<Control>();
foreach (Control child in ctrl.Controls)
{
if (t == child.GetType())
cntrls.Add(child);
cntrls.AddRange(GetControlsByType(child, t));
}
return cntrls;
}
答案 5 :(得分:0)
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:Button runat="server" ID="Button1" Text="I was NOT changed" />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
ASPX.CS:
using System;
using System.Data;
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)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column"));
DataRow dr = null;
for (Int32 i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["column"] = "";
dt.Rows.Add(dr);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
foreach (RepeaterItem ri in this.Repeater1.Controls)
{
foreach (Control c in ri.Controls)
{
Button b = new Button();
try
{
b = (Button)c;
}
catch (Exception exc)
{
}
b.Text = "I was found and changed";
}
}
}
}
答案 6 :(得分:0)
或者我自己代码的变体,只更改ASPX.CS:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
#region Fill Repeater1 with some dummy data
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column"));
DataRow dr = null;
for (Int32 i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["column"] = "";
dt.Rows.Add(dr);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
#endregion
foreach (Button b in this.FindButtonsInRepeater(ref this.Repeater1))
{
b.Text = "I was found and changed";
}
}
private List<Button> FindButtonsInRepeater(ref Repeater repeater)
{
List<Button> buttonsFound = new List<Button>();
foreach (RepeaterItem ri in repeater.Controls)
{
foreach (Control c in ri.Controls)
{
try
{
buttonsFound.Add((Button)c);
}
catch (Exception exc)
{
}
}
}
return buttonsFound;
}
}