473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

setting attributes to controls

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?

Nov 18 '05 #1
3 1612
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" <ja****@magicalia.com> wrote in message
news:#6*************@TK2MSFTNGP09.phx.gbl...
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?

Nov 18 '05 #2
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" <ks******@takempis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <ja****@magicalia.com> wrote in message
news:#6*************@TK2MSFTNGP09.phx.gbl...
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?


Nov 18 '05 #3
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" <ja****@magicalia.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
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" <ks******@takempis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <ja****@magicalia.com> wrote in message
news:#6*************@TK2MSFTNGP09.phx.gbl...
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?



Nov 18 '05 #4

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

Similar topics

1
by: Rob Meade | last post by:
Hi all, I have a loop in my code which builds the controls on the page. I at one stage need to add some hidden input controls dynamically, I have achieved this, and I have set their...
5
by: Russell Smallwood | last post by:
Hello all, Why can't I wire up an event during the "Raise PostBackEvent" stage of a postback? Just in case this is the wrong question, the situation: deep in the bowls of some code-behind...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
3
by: Will Lastname | last post by:
I am trying to manipulate the textbox properties in my datagrid for when I fire the Edit/Update from the EditCommandColumn. I have set a few attributes such as: Dim tbPhone As TextBox =...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
2
by: davidr | last post by:
I'm reposting hoping to get a reply, my last one got bumped down because I posted in the late day. Hi, I have in my behind code a user control with the following in Page Load: ...
1
by: LG | last post by:
I want to add attributes to a custom dropdownlist and I found some code on Internet that doesn't work after post back. First time the control has the attributes, but after postback the attributes...
1
by: Asif | last post by:
Hi All, I did follow the MSDN article (http://msdn2.microsoft.com/en-us/ library/ms379585(VS.80).aspx ) for overriding Master Page properties by setting page title and other Meta information(...
3
by: pbd22 | last post by:
Hi. How do I add the runat=server attribute on a buttonfield link dynamically? thanks!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.