472,353 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Enumerate Control Attributes?

What is the best and/or fastest way to enumerate
attributes of an HTML control?

In my page template, I have:

<body id="myBody" bottomMargin="0" leftMargin="0"
topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout"
class="Body" runat="server">

And in my code-behind I want to look for the leftMargin
attribute and change its property to "5" and remove the
class attribute.

Thanks.
Nov 18 '05 #1
7 4451
Because you can directly access the individual attributes
by name. I don't know why you want to enumerate through
the attributes.
myBody.Attributes("leftMargin") = 5
myBody.Attributes("class") = ""
Bin Song, MCP
-----Original Message-----
What is the best and/or fastest way to enumerate
attributes of an HTML control?

In my page template, I have:

<body id="myBody" bottomMargin="0" leftMargin="0"
topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout"
class="Body" runat="server">

And in my code-behind I want to look for the leftMargin
attribute and change its property to "5" and remove the
class attribute.

Thanks.
.

Nov 18 '05 #2
Hi localhost,

Thank you for using Microsoft Newsgroup Service. Based on your description.
You want to enumerate a ASP.NET HtmlControl(runat=server)'s attributes in
server side code. If my understanding of your problem is correct, here is
my suggestion:

1. As for All the controls in System.Web.UI.HtmlControls namespace, each of
them has a Attributes member which contains all the attributes of the
HtmlControl. And the Attributes member has a two sub members, one is
"keys"(can loop through by for each), the other is "Item", you can get a
certain attribute of the HtmlControl via such code:
control.Attributes.Item("width")
it returns a string value

However, since the "Item" member of the HtmlControl's "Attributes" member
is not a Collection Class, you can't enumerate all the attributes in it
using "for each" directly on the "Item". But you can loop throuth them by
using the Keys member together. For example. Suppose there is a HtmlTable
on a aspx page:
<TABLE id="tbTest" cellSpacing="1" cellPadding="1" width="312" height="200"
border="1"
runat="server" style="WIDTH: 312px; HEIGHT: 128px">
<TR>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
then, in the page's code-behind file , you can loop throuth the table's
attributes as below:

For Each key As Object In tbTest.Attributes.Keys
Response.Write("<br>" + key.ToString() + ":" +
tbTest.Attributes.Item(key).ToString())
Next
2. As or you condition, you want to get the attributes in the child control
of a UserControl(ascx control). You need to first use Control or Page
class's Find Control method to find the certain control. And then, use the
above way in 1 to loop through all of its attributes

Please try out the preceding suggestions and let me know whether they help.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
I have used this, maybe it will work for you:

IEnumerator keyEnum = controlName.Attributes.Keys.GetEnumerator();
while ( keyEnum.MoveNext() )
{
string currentKey = (string)keyEnum.Current;
string currentVal = controlName.Attributes[currentKey];
}

"localhost" <pr*******@cohort.ces> wrote in message news:<02****************************@phx.gbl>...
What is the best and/or fastest way to enumerate
attributes of an HTML control?

Nov 18 '05 #4

I need to enumerate all of the attributes, and if I find
the one I want, then I need to edit its value. If I
don't find the one I want, then I need to add it with a
default value. I can't access the individual attributes
by name if I don't know what is in there in the first
place.

-----Original Message-----
Because you can directly access the individual attributesby name. I don't know why you want to enumerate through
the attributes.
myBody.Attributes("leftMargin") = 5
myBody.Attributes("class") = ""
Bin Song, MCP
-----Original Message-----
What is the best and/or fastest way to enumerate
attributes of an HTML control?

In my page template, I have:

<body id="myBody" bottomMargin="0" leftMargin="0"
topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout"class="Body" runat="server">

And in my code-behind I want to look for the leftMargin
attribute and change its property to "5" and remove the
class attribute.

Thanks.
.

.

Nov 18 '05 #5
Hi localhost,

Thank you for using Microsoft Newsgroup Service. Based on your description.
You want to enumerate a ASP.NET HtmlControl(runat=server)'s attributes in
server side code. If my understanding of your problem is correct, here is
my suggestion:

1. As for All the controls in System.Web.UI.HtmlControls namespace, each of
them has a Attributes member which contains all the attributes of the
HtmlControl. And the Attributes member has a two sub members, one is
"keys"(can loop through by for each), the other is "Item", you can get a
certain attribute of the HtmlControl via such code:
control.Attributes.Item("width")
it returns a string value

However, since the "Item" member of the HtmlControl's "Attributes" member
is not a Collection Class, you can't enumerate all the attributes in it
using "for each" directly on the "Item". But you can loop throuth them by
using the Keys member together. For example. Suppose there is a HtmlTable
on a aspx page:
<TABLE id="tbTest" cellSpacing="1" cellPadding="1" width="312" height="200"
border="1"
runat="server" style="WIDTH: 312px; HEIGHT: 128px">
<TR>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD></TD>
</TR>
</TABLE>
then, in the page's code-behind file , you can loop throuth the table's
attributes as below:

For Each key As Object In tbTest.Attributes.Keys
Response.Write("<br>" + key.ToString() + ":" +
tbTest.Attributes.Item(key).ToString())
Next
2. As or you condition, you want to get the attributes in the child control
of a UserControl(ascx control). You need to first use Control or Page
class's Find Control method to find the certain control. And then, use the
above way in 1 to loop through all of its attributes

Please try out the preceding suggestions and let me know whether they help.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #6
Hi Localhost,

Is my suggestion helpful to you? Have you resolved your problem? Please let
me know if you have any thing unclear on it.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7
Thanks for working on this. FindControl did not work for
me because I had multiple elements nested within each
other. I did not want to use a foreach loop because of
the slowness. I used IEnumerator on the Keys collection
and that worked fine.

Thanks anyway.

-----Original Message-----
Hi localhost,

Thank you for using Microsoft Newsgroup Service. Based on your description.You want to enumerate a ASP.NET HtmlControl (runat=server)'s attributes inserver side code. If my understanding of your problem is correct, here ismy suggestion:


Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MuZZy | last post by:
Hi, I need to build a function which would return all controls which are upper in z-order relatively to given control. I need this in order to be...
7
by: Sky | last post by:
What I have currently: I have a user control called mod_container.aspx that is basically two divs -- the top a toolbar, that expands/collapse the...
4
by: Brian W | last post by:
Hi All, I know this is a dumb question, but I'll ask it anyway... How do you do client-side scripting with an ASP control? For example I...
6
by: Selden McCabe | last post by:
I have a form with a bunch of image buttons. When the user moves the mouse over a button, I want to do two things: 1. change the Imagebutton's...
3
by: | last post by:
For a given web form, I want to know what the +simplest+ way to add an attribute to all of a particular control type on that page. For example, I...
5
by: HL | last post by:
Hi, I need to enumerate windows and find the sum of the rect of all the windows of a specific application. In C++, I use the APIs - 'EnumWindows...
8
by: Dustan | last post by:
Can I make enumerate(myObject) act differently? class A(object): def __getitem__(self, item): if item 0: return self.sequence elif item < 0:...
0
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This...
2
by: cloftis | last post by:
Using VS2003, VB and MSHTML, Using an HTMLSpanElement I want to enumerate the attributes of a SPAN tag. 1 'For testing sake 2 Dim...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.