setting attributes to controls

  • Thread starter Thread starter Janaka
  • Start date Start date
J

Janaka

I've seen and used some samples where you can set the onclick attrubute to a
Button control to get it to do some javascript a la :
btnUse.Attributes["onclick"] = "DoSomeJS()";

However when i try and get this to work on certain controls such as the
ListItem in a RadioButtonList it just doesn't do anything? Does this
attributes property only render for certain controls?
 
The Attributes Collection of a control is a collection of all HTML
attributes of that control. In the following HTML, note the attributes:

<input type="text" size="1" name="MyText">

The attributes of this HTML object are "type", "size", and "name".

All Server Controls have an Attributes collection. Whenther an attribute
performs the way you expect it to is a function of HTML and the browser, not
of ASP.Net. You can add any attribute you want to an HTML element. The
browser may or may not recognize it and interact with it. For example, you
could create an extra attribute for the text box above, and call it "foo":

<input type="text" size="1" name="MyText" foo="bar">

You wouldn't expect a browser to do anything with that, would you? So, make
sure that the attribute you add to a Server Control is a valid HTML
attribute for that HTML object.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Kevin,

yes i understand that its got to understand what the attribute should do in
HTML - i.e. only valid HTML attributes can be used.
However I find that when looking at the HTML source of the document there's
no attribute being written out to the control?

Try the following on a RadioButtonList

<asp:RadioButtonList id="MyList" runat="server">
<asp:ListItem Value="1"></asp:ListItem>
</asp:RadioButtonList>

MyList.Items[0].Attributes["onclick"] = "dosomething()";

When I try this nothing gets written to the list item?

Kevin Spencer said:
The Attributes Collection of a control is a collection of all HTML
attributes of that control. In the following HTML, note the attributes:

<input type="text" size="1" name="MyText">

The attributes of this HTML object are "type", "size", and "name".

All Server Controls have an Attributes collection. Whenther an attribute
performs the way you expect it to is a function of HTML and the browser, not
of ASP.Net. You can add any attribute you want to an HTML element. The
browser may or may not recognize it and interact with it. For example, you
could create an extra attribute for the text box above, and call it "foo":

<input type="text" size="1" name="MyText" foo="bar">

You wouldn't expect a browser to do anything with that, would you? So, make
sure that the attribute you add to a Server Control is a valid HTML
attribute for that HTML object.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Janaka said:
I've seen and used some samples where you can set the onclick attrubute
to
a
Button control to get it to do some javascript a la :
btnUse.Attributes["onclick"] = "DoSomeJS()";

However when i try and get this to work on certain controls such as the
ListItem in a RadioButtonList it just doesn't do anything? Does this
attributes property only render for certain controls?
 
Hi Janaka,

I should have looked closer at your code. In your case, the problem is that
a ListItem Control is not a Radio Button per se, but a generic ListItem
Control, whose functionality is determined by the parent Control it resides
in. Although a ListItem Control has an Attributes collection which is
inherited from WebControl, it is Read Only, probably since the parent
control is the one that actually renders the HTML for the Control. You can
only set the Selected, Value, InnerHtml, and Text properties of a ListItem
Control.

I would suggest using separate RadioButton controls instead. You can set the
Attributes of these without issue.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Janaka said:
Kevin,

yes i understand that its got to understand what the attribute should do in
HTML - i.e. only valid HTML attributes can be used.
However I find that when looking at the HTML source of the document there's
no attribute being written out to the control?

Try the following on a RadioButtonList

<asp:RadioButtonList id="MyList" runat="server">
<asp:ListItem Value="1"></asp:ListItem>
</asp:RadioButtonList>

MyList.Items[0].Attributes["onclick"] = "dosomething()";

When I try this nothing gets written to the list item?

Kevin Spencer said:
The Attributes Collection of a control is a collection of all HTML
attributes of that control. In the following HTML, note the attributes:

<input type="text" size="1" name="MyText">

The attributes of this HTML object are "type", "size", and "name".

All Server Controls have an Attributes collection. Whenther an attribute
performs the way you expect it to is a function of HTML and the browser, not
of ASP.Net. You can add any attribute you want to an HTML element. The
browser may or may not recognize it and interact with it. For example, you
could create an extra attribute for the text box above, and call it "foo":

<input type="text" size="1" name="MyText" foo="bar">

You wouldn't expect a browser to do anything with that, would you? So, make
sure that the attribute you add to a Server Control is a valid HTML
attribute for that HTML object.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
attrubute
to
a
Button control to get it to do some javascript a la :
btnUse.Attributes["onclick"] = "DoSomeJS()";

However when i try and get this to work on certain controls such as the
ListItem in a RadioButtonList it just doesn't do anything? Does this
attributes property only render for certain controls?
 

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

Back
Top