Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从.net代码隐藏页获取GetElementsByTagName功能?_C#_.net_Asp.net - Fatal编程技术网

C# 从.net代码隐藏页获取GetElementsByTagName功能?

C# 从.net代码隐藏页获取GetElementsByTagName功能?,c#,.net,asp.net,C#,.net,Asp.net,我正在用C#NET写一个网页。在javascript中有一个名为GetElementsByTagName的函数。。。这对于从.aspx页面调用的javascript来说很好。我的问题是,有没有办法从我的C#代码中获得这种功能 -- 对于那些好奇的人来说,我使用了一个asp:repeater来生成许多按钮,现在我基本上是在尝试制作一个按钮来点击它们。我尝试在创建按钮时将所有按钮存储在列表中,但每次回发时列表都会被清除,因此我想我可以尝试上述方法。FindControl(),或在页面上的控件中迭代

我正在用C#NET写一个网页。在javascript中有一个名为GetElementsByTagName的函数。。。这对于从.aspx页面调用的javascript来说很好。我的问题是,有没有办法从我的C#代码中获得这种功能

--

对于那些好奇的人来说,我使用了一个asp:repeater来生成许多按钮,现在我基本上是在尝试制作一个按钮来点击它们。我尝试在创建按钮时将所有按钮存储在列表中,但每次回发时列表都会被清除,因此我想我可以尝试上述方法。

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"
等等。

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"

等等。

您可以使用页面的FindControl方法找到控件,但是Repeater元素的名称是由.net生成的


另一方面,如果您真的愿意,您可以将按钮列表存储在页面的ViewState(或者它们的名称列表)中。

您可以使用页面的FindControl方法找到控件,但是Repeater元素的名称是由.net生成的


另一方面,如果您真的愿意,您可以将按钮列表存储在页面的ViewState(或者它们的名称列表)中。

无论何时回发,都会重新创建所有内容,包括数据绑定控件


如果你的列表不见了,按钮控件也不见了。当然,除非您已经重新创建了它们,在这种情况下,您也应该重新创建列表。

无论何时进行回发,都会重新创建所有内容,包括数据绑定控件

如果你的列表不见了,按钮控件也不见了。当然,除非您已经重新创建了它们,在这种情况下,您也应该重新创建列表。

尝试以下操作:

foreach (Control ctl in myRepeater.Controls)
{ 
  if (ctl is Button)
  {
    ((Button)ctl).Click();
  }
}
嗯…

试试这个:

foreach (Control ctl in myRepeater.Controls)
{ 
  if (ctl is Button)
  {
    ((Button)ctl).Click();
  }
}

嗯…

我不知道你所说的全部点击是什么意思。但是下面的代码如何为您工作呢?我不知道,我还没有测试

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;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
foreach(GetControlsByType(this,typeof(TextBox))中的控件)
{ 
//做点什么?
}
}
公共静态System.Collections.Generic.List GetControlsByType(控件ctrl,类型t)
{
System.Collections.Generic.List cntrls=新的System.Collections.Generic.List();
foreach(ctrl.Controls中的控件子级)
{
if(t==child.GetType())
添加(子项);
AddRange(GetControlsByType(child,t));
}
返回CNTRL;
}

我不知道你所说的全部点击是什么意思。但是下面的代码如何为您工作呢?我不知道,我还没有测试

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;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
foreach(GetControlsByType(this,typeof(TextBox))中的控件)
{ 
//做点什么?
}
}
公共静态System.Collections.Generic.List GetControlsByType(控件ctrl,类型t)
{
System.Collections.Generic.List cntrls=新的System.Collections.Generic.List();
foreach(ctrl.Controls中的控件子级)
{
if(t==child.GetType())
添加(子项);
AddRange(GetControlsByType(child,t));
}
返回CNTRL;
}
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";
            }
        }
    }
}
使用系统;
使用系统数据;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable dt=新的DataTable();
添加(新数据列(“列”));
数据行dr=null;
对于(Int32 i=0;i<10;i++)
{
dr=dt.NewRow();
dr[“列”]=“”;
dt.Rows.Add(dr);
}
this.Repeater1.DataSource=dt;
this.Repeater1.DataBind();
foreach(此.Repeater1.Controls中的RepeaterItem ri)
{
foreach(ri.控件中的控件c)
{
按钮b=新按钮();
尝试
{
b=(按钮)c;
}
捕获(异常exc)
{
}
b、 Text=“发现并更改了我”;
}
}
}
}
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";
            }
        }
    }
}
使用系统;
使用系统数据;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable dt=新的DataTable();
添加(新数据列(“列”));
数据行dr=null;
对于(Int32 i=0;i<10;i++)
{
dr=dt.NewRow();
dr[“列”]=“”;
dt.Rows.Add(dr);
}
this.Repeater1.DataSource=dt;
this.Repeater1.DataBind();
foreach(此.Repeater1.Controls中的RepeaterItem ri)
{
foreach(ri.控件中的控件c)
{
按钮b=新按钮();
尝试
{
b=(按钮)c;
}
捕获(异常exc)
{
}
b、 Text=“发现并更改了我”;
}
}
}
}

或我自己代码的变体,仅更改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;
    }
}
使用系统;
使用系统数据;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Collections.Generic;
公众党