Mike Cole
Mike Cole

Reputation: 14743

ASP.NET Intellisense vs. Client-Side Attributes

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?

Upvotes: 5

Views: 1771

Answers (5)

Dragos
Dragos

Reputation: 1

Let’s assume that we have a pop-up message triggered by a session timeout kind of event.

Like you should have for your online banking web application.

So, the message pops up reading that you have few more minutes to complete your online experience with that specific secured website.

Usually, if you don’t do anything about that, in about the same time it took the message to pop, the application will sign you out.

On the client side you have JavaScript for timing those actions.

Now, let’s say that the button that signs you out is called “btnSignOut”.

If you want to trigger the same action from a line of JS, on timeout, you should have something like this:

if(timeout){
;
 }

That will trigger the same event as by clicking on the “btnSignOut” linkButton.

Source

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

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>

Upvotes: 2

brendan
brendan

Reputation: 29996

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.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161821

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.

;-)

Upvotes: 1

TheTXI
TheTXI

Reputation: 37905

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)

Upvotes: 2

Related Questions