By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Enumerate Control Attributes?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.