Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique onMouseOver of listItem's

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hi all...disclaimer: Im quite new to .net...I've been looking around, and I cannot find anyone that can solve this problem for me...

I have a RadioButtonList..ie


Code:
Code:
<asp:Table Runat=server ID=CatTable BorderWidth=1 BorderColor=#000033>
 <asp:TableRow>
  <asp:TableCell>
   <asp:RadioButtonList Runat=server ID=radlist>
    <asp:ListItem value="Ad services"></asp:ListItem>
    <asp:ListItem value="Gen Maint"></asp:ListItem>
   </asp:RadioButtonList>
  </asp:TableCell>
 </asp:TableRow>
</asp:Table>
Now, here is the thing...I need to get a unique onMouseOver event fired for each list Item. I am passing my javascript function different parameters in each listitem....Everyone has said to "just do" this...


Code:
Code:
        Me.ListItem1.Attributes.Add("OnMouseOver", _
                        "show('DivAdminServices');")
        Me.ListItem2.Attributes.Add("OnMouseOver", _
                         "show('DivArtwork');")
Now, naturally that isn't going to work because the "ID" attribute isn't available for each listItem! If I wanted to run one JS function for the entire list, it would be no problem...but, that won't do me any good.

(Basically, I need to show/hide a div when someone rolls over a radiobutton list item.)

Is there any way to do this?

Use your resources, you're on the internet!
 
Can't you just add the attribute for each relevant item of the RadioButtonList? e.g.
Code:
RadioButtonList1.Items.Item(0).Attributes.Add
("OnMouseOver", "show('DivAdminServices');")

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I think that might work, but there is a problem I need to solve before I can even try that....


Don't want to get off topic too much here, but I get an error if I try to put each asp:listItem in my asp:tableCell unless i put the opening and closing <asp:RadioButtonList> tags within the same cell, which kind of makes the entire purpose of my radio button list moot...how does one get around this? Should I be using a "normal" HTML table instead? This is what my code currently looks like...


Code:
                        <asp:Table Height="100%" Runat="server" ID="TableCat" BorderWidth="1" BorderColor="#000000" Width="100%" CellSpacing="1" cellpadding="4"> 
                            <asp:TableRow> 
                                <asp:TableCell CssClass="head" ColumnSpan="3"> 
                                    Select a catetogry: 
                                </asp:TableCell> 
                            </asp:TableRow> 
                            <asp:TableRow> 
                                <asp:TableCell CssClass="smShadeLite" ID="cellAdminServices" Width="50%" Runat="server"> 
                            <asp:RadioButtonList ID=catList Runat=server > 
                                    <asp:ListItem  Value="chkAdminServices"/> 
                                     
                                    Admin Services 
                                </asp:TableCell> 
                                <asp:TableCell CssClass="smShadeLite" ID="cellJanitorial" Width="50%" Runat="server"> 
                                    <asp:ListItem value="chkJanitorial" /> 
                                    Janitorial 
                                </asp:TableCell> 
                            </asp:TableRow> 
                            <asp:TableRow>


Use your resources, you're on the internet!
 
You could just use a HTML Table as opposed to a Server control table if you aren't going to be doing anything with the table (i.e. it is just to position the items how you want them).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Alright, I got that table/control issue sorted....

I added the code you suggested...

my C/B: (only on first element of the list to experiment
Code:
        Me.catlist.Items.Item(0).Attributes.Add("OnMouseOver", _
                "show('DivAdminServices');")


my HTML
Code:
						<asp:RadioButtonList Width="100%" Height="300" ID="catlist" Runat="server" CellSpacing="1" CellPadding="4" RepeatColumns="2" BorderColor="#cccccc" BorderWidth="0" CssClass="table">
							<asp:listItem Value="Admin Services" />
							<asp:ListItem Value="Janitorial" />
							<asp:ListItem Value="Artwork / Signage / Whiteboards" />
							<asp:ListItem Value="Landscaping" />
							<asp:ListItem Value="Audio / Visual" />
							<asp:ListItem Value="Lights" />
							<asp:ListItem Value="Cafe / Vending" />
							<asp:ListItem Value="Moves" />
							<asp:ListItem Value="Doors" />
							<asp:ListItem Value="Parking" />
							<asp:ListItem Value="Furniture" />
							<asp:ListItem Value="General Maintenance" />
							<asp:ListItem Value="Utilities" />
							<asp:ListItem Value="HVAC" />
						</asp:RadioButtonList>

However no event gets rendered when I view source on the page....Is that not the correct syntax? What am I missing here?

Use your resources, you're on the internet!
 
This is a known bug (and quite a painful one, too) w/ the .NET Framework 1.x.

Actually, MS says that the behavior is "by design". I'm at a loss as to why, though:

ListItem attributes simply don't take when used on RadioButtonLists or DropDownLists. They do work on other controls that use ListItems (really, I don't know why).

Here is a blog post from someone who created their own solution. They are using a DropDownList, so you'll have a little bit of work to do (download their code and modify it):

Another solution would be to derive a class from ListItem, override the Render method, adding support for the Attributes collection rendering, and then use that class whenever specifiying ListItems. I think I also saw something like this on Google once, so you could find it again if you looked.

Good luck.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks for pointing that out paul - I hadn't realised that it didn't actually appply the attribute (I'd just looked through the Item property and found the Attributes and the corresponding Add method - and had no reason to assume it wouldn't work!!!)

I'm at a loss as to why
You're not the only one! This behavior is by design seems to be an alternative for "we haven't implemented this feature yet" in this case!


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
This is a known bug (and quite a painful one, too) w/ the .NET Framework 1.x.
wow, that sucks.

Thanks for the help though guys, time to explore some other methods....If anyone has any other ideas to circumvent this please let them be known as Im sure Im not the only person in the world who would benefit from solutions pertaining to this particular issue.

Thanks again, guys.

Use your resources, you're on the internet!
 
Alright, this is my attempt at this..

Another solution would be to derive a class from ListItem, override the Render method, adding support for the Attributes collection rendering, and then use that class whenever specifiying ListItems. I think I also saw something like this on Google once, so you could find it again if you looked.

Am I anywhere even near the ballpark? Any help appreciated

Code:
    Public Class clsRadioButton
        Inherits RadioButtonList

        Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)

            writer.AddAttribute("onMouseOver", "show(d);")


            MyBase.AddAttributesToRender(writer)
        End Sub

    End Class

Use your resources, you're on the internet!
 
RhythmAddict112,

Check out the blog post that I linked to earlier. He links to his source code.

There's a fully functional derived dropdownlist there that could easily be turned into a radiobuttonlist.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
link,
Whats up? Sorry I should have mentioned I did look at that and it does certainly contain source for the drop down list...

My page is in vb.net, can I use this code and include it even though it is c#?

Sorry about the noob q's :-/

Use your resources, you're on the internet!
 
My page is in vb.net, can I use this code and include it even though it is c#?
No but it would be easy to convert to VB - it's only the syntax that is different.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Got it...I just converted this... (Below) Only one thing, after I put this code into my CB, how would I implement it on my page?

Code:
Namespace Madgeek.Web.UI.WebControls
  #region Imports
	Imports System
	Imports System.Web
	Imports System.Web.UI
	Imports System.Web.UI.WebControls
  #End Region
 
 
	Public Class DropDownList
	 Inherits System.Web.UI.WebControls.DropDownList
		Protected Overrides  Sub RenderContents(ByVal writer As HtmlTextWriter)
			If Not Items Is Nothing Then
				Dim selected As Boolean =  False 
 
				Dim listItem As ListItem
				For Each listItem In Items
					writer.WriteBeginTag("option")
 
					If listItem.Selected Then
						If selected Then
							 							Throw New HttpException("Cannot multiselect in DropDownList.") 'TODO: this should be language independent
						End If
						selected = True
						writer.WriteAttribute("selected", "selected", False)
					End If
					writer.WriteAttribute("value", listItem.Value, True)
 
					listItem.Attributes.Render(writer)  					 
					writer.Write(">"c)
					HttpUtility.HtmlEncode(listItem.Text, writer)
 
					writer.WriteEndTag("option")
					writer.WriteLine()
				Next
			End If
		End Sub
	End Class
End Namespace

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel ([URL unfurl="true"]http://www.KamalPatel.net)[/URL] 
'----------------------------------------------------------------

Use your resources, you're on the internet!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top