views:

554

answers:

5

I am just getting into more client-side stuff in ASP.NET using Javascript, and there's something that's bothering me that hopefully somebody can explain. Why is it that intellisense doesn't show the all of the attributes/properties of a .NET control? For example, a ListItem in a RadioButtonListControl:

<asp:ListItem Value="1" Text="Yes" onclick="alert('TEST1');" />
<asp:ListItem Value="0" Text="No" onclick="alert('TEST2');" />

Intellisense doesn't show the onclick property (or is it called attribute?) of the ListItem, but it sure works. Why doesn't it show? Or am I relying on Intellisense too much? :-) Or should I be declaring this stuff in code-behind?

+2  A: 

It depends a lot of times on the control you are working with and the attribute. I know that ASP button controls will show the onclick and the onclientclick attributes in intellisense. It may be that ASP.NET doesn't fully support the onClick attribute for the listitem (as opposed to say the selectedindexchanged attribute on the listbox/dropdownlist/etc controls)

TheTXI
Hmmm... so should I not use them? I've tested it and it work, but if I might run into unexpected results in the future I don't want to mess with it. I love working with ASP.NET, and I'm just starting to get into Javascript and it really rocks, but for some reason it seems the two don't play as well as they should.
Mike C.
I should add a disclaimer to my above comment: I'm fully aware that my own lack of experience could be the problem.
Mike C.
John Saunders summed it up with a more authoritative answer. If it works for you, it will work for you, but I would not recommend doing something which is not actually supported.
TheTXI
I think your answer is more directed towards the basic issue - Intellisense, while @John's answer is more general in nature. I have attempted to do the same in my answer. +1
Cerebrus
+1  A: 

The ListItem class has no onclick property.

It appears that ListItem implements the IAttributesAccessor interface, and also has an Attributes collection. This is documented as:

Gets a collection of attribute name and value pairs for the ListItem that are not directly supported by the class.

The attributes you put on the tag this way are rendered when the control is rendered. The details of this differ by control. I experimented with a page containing the following:

<asp:DropDownList ID="_ddl1" runat="server" >
    <asp:ListItem Text="Item 1" Value="Item1" onClick="foox();" oncluck="bar(this);" />
</asp:DropDownList>
<asp:ListBox ID="_listBox1" runat="server">
    <asp:ListItem Text="Item 1" Value="Item1" onClick="foox();" oncluck="bar(this);" />
</asp:ListBox>
<asp:RadioButtonList ID="_radioList1" runat="server">
    <asp:ListItem Text="Item 1" Value="Item1" onClick="foox();" oncluck="bar(this);" />
</asp:RadioButtonList>

In the case of the DropDownList and ListBox, both attributes are rendered on the <Option> element. In the case of the RadioButtonList, the onclick attribute is rendered on the <input type="radio"> element, but the unrecognized oncluck element is rendered on the enclosing <span> element.

Note that the oncluck event is never fired, apparently.

;-)

John Saunders
Apparently the listitem implements the IChicken interface and was forced to implement the "oncluck" event :)
TheTXI
"Note that the oncluck event is never fired, apparently" haha!
Andreas Grech
@Dreas Grech: I actually tried it, but nothing happened. ;-)
John Saunders
A: 

The reason you don't see it in intellisense is because "onclick" is not ASP.Net. You'll notice that if you do the following:

<asp:ListItem Value="1" Text="Yes" Secret="Yes" onclick="alert('TEST1');" />
<asp:ListItem Value="0" Text="No" Secret="No" onclick="alert('TEST2');" />

If you look at what is rendered to html you will see the "Secret" attribute. So while ASP.Net will render whatever attributes you put in there it will only provide intellisense for ASP.Net attributes.

AS far as coding practices, I've seen and used the "onclick" too many times to count. So while a purist might have a problem with the practice I think it is fine to use.

brendan
+2  A: 

The issue is that intellisense for Web server controls does not display client side events and only lists events that are raised on the server. If you were to use an HTML server control for the same purpose you would see the (client-side JS) events in Intellisense.

Another issue to consider is that the onclick event isn't supported for option elements (atleast not in IE, though Firefox supports it fine). You should instead handle the onchange client side event. An example :

<select id="htmlserverselect" runat="server" onchange="alert(this.value);">
  <option value="1">Yes</option>
  <option value="2">No</option>
</select>
Cerebrus
Is that necessarily true? I noted in my answer that the ASP button's intellisense has the onClientClick show up. That event isn't raised on the server? Am I confused about that perhaps?
TheTXI
Yes, that would be true, because the OnClientClick is a "server side property" (whatever that is!) that makes sure that the control renders with the onclick attribute set to the specified value. It's a convenience measure available to a special web server control, simply because buttons most often require some client code to run before form submission.
Cerebrus
That makes sense then. When you said that it only lists events that are raised on the server that didn't seem quite right since OnClientClick doesn't actually run -on- the server.
TheTXI
Thanks for the info!
Mike C.
It seems like a valid point that I should be able to see both server side and client side events using intellisense on web server controls. If I can do it, why not tell me I can do it? Isn't that the point of intellisense?
Mike C.
The "intelli" part of intellisense has always been the topic of much debate. Just how much intelligence makes sense ? ;-) Thanks for accepting the answer!
Cerebrus