Reputation: 193
My ASP.Net webform contains a number of textboxes. I want to add "onfocus" and "onmouseover" attributes to ALL textboxes. I can do this using the following code:
TextBox1.Attributes.Add("onfocus", "javascript:this.select();");
TextBox1.Attributes.Add("onmouseover", "javascript:this.select();");
TextBox2.Attributes.Add("onfocus", "javascript:this.select();");
TextBox2.Attributes.Add("onmouseover", "javascript:this.select();");
TextBox3.Attributes.Add("onfocus", "javascript:this.select();");
TextBox3.Attributes.Add("onmouseover", "javascript:this.select();");
But since the no. of TextBoxes are more then 35, I am thinking of using a loop to do so. I tried the following code:
if (!IsPostBack)
{
foreach (Control ctl in Controls)
{
if (ctl is TextBox)
ctl.Attributes.Add("onmouseover", "javascript:this.select();");
}
}
But I am getting the following error:
'System.Web.UI.Control' does not contain a definition for 'Attributes' and no extension method 'Attributes' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)
What am I doing wrong?
Upvotes: 1
Views: 6353
Reputation: 460340
You could use Linq's OfType
. It returns all controls of a given type.
I suggest to add all of your TextBoxes
into a container control, for example a Panel
or PlaceHolder
:
foreach(var txt in txtPanel.Controls.OfType<TextBox>())
{
txt.Attributes.Add("onfocus", "javascript:this.select();");
txt.Attributes.Add("onmouseover", "javascript:this.select();");
}
Ensure that you've added using System.Linq
.
Here's another similar approach which looks into all panels in the current page:
this.Form.Controls.OfType<Panel>()
.SelectMany(p => p.Controls.OfType<TextBox>());
foreach (TextBox txt in allTextBoxes){}
Note that you can also filter the Panels
or TextBoxes
with Enumerable.Where
(f.e. if the panels' id's start all with txtPanel_
).
Upvotes: 2
Reputation: 116538
You have to cast it to a TextBox. ctl is still a Control, which does not have the Attributes property.
((TextBox)ctl).Attributes.Add("onmouseover", "javascript:this.select();");
Upvotes: 0
Reputation: 30932
The type of ctl is still Control. In order for it to become a TextBox, you can cast it like this:
((TextBox)ctl).Attributes.Add("onmouseover", "javascript:this.select();");
or use the as
operator:
(ctl as TextBox).Attributes.Add("onmouseover", "javascript:this.select();");
The difference between those two, is that as
will return a null
reference if the cast is not successfull, and the cast will throw an exception, but since you already check using the is
operator, just use the one that looks better to you.
However, if you use multiple assignments, I would extract the cast to a variable, like:
if (ctl is TextBox)
{
TextBox textBox = (TextBox) ctl;
textBox.Attributes.Add("onmouseover", "javascript:this.select();")
.....
}
Upvotes: 1
Reputation: 26766
The ctl object is just a webcontrol and although you're checking it's a TextBox, you're not casting it as one before using it...
Try...
if (!IsPostBack)
{
foreach (Control ctl in Controls)
{
if (ctl is TextBox)
((TextBox)ctl).Attributes.Add("onmouseover", "javascript:this.select();");
}
}
Upvotes: 1