Reputation: 464
I am writing a webpage in C# .NET. In javascript there is a function called GetElementsByTagName... this is nice for javascript invoked from the .aspx page. My question is, is there any way I can have this kind of functionality from my C# code-behind?
--
The scenario for those curious: I used an asp:repeater to generate a lot of buttons, and now I'm essentially trying to make a button that clicks them all. I tried storing all the buttons in a list as I created them, but the list is getting cleared during every postback, so I thought I could try the above method.
Upvotes: 1
Views: 2971
Reputation: 19489
FindControl(), or iterate through the controls on the page...
For each ctl as Control in Me.Controls
If ctl.Name = whatYouWant Then
do stuff
Next 'ctl
--If you are creating the controls, you should be setting their ID's
Dim ctl as New Control()
ctl.ID = "blah1"
etc...
Upvotes: 2
Reputation: 21727
Or a variation of my own code, only changing the 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;
}
}
Upvotes: 0
Reputation: 21727
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";
}
}
}
}
Upvotes: 0
Reputation: 416149
Whenever you do any postback, everything is recreated, including your databound controls.
If your list is gone, so are the button controls. Unless, of course, you've recreated them, and in that case you should have recreated the list as well.
Upvotes: 0
Reputation: 12431
I don't know exactly what you mean by clicks them all. But how would this following code work for you? I don't know, I haven't tested...
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;
}
Upvotes: 0
Reputation: 4164
Try this:
foreach (Control ctl in myRepeater.Controls)
{
if (ctl is Button)
{
((Button)ctl).Click();
}
}
HTH...
Upvotes: 3
Reputation: 32997
Well, you can find controls with the page's FindControl method, but Repeater elements have names generated by .net.
As an aside, if you really want to, you could store the list of buttons in your page's ViewState (or perhaps a list of their names).
Upvotes: 1