Enumerate Control Attributes?

L

localhost

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.
 
B

Bin Song

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
 
M

MSFT

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.)
 
S

SteveC

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];
}
 
L

localhost

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.
 
M

MSFT

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.)
 
M

MSFT

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.)
 
L

localhost

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top