0

I have an UserControl, it is a ToolTip.

.ascx :

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucTooltip.ascx.cs" Inherits="Portail.formulaires.ucTooltip" debug="true"%>


<div id="dhtmltooltip" style="z-index:9999999999; display:inline-block;">
  <div id="dhtmltooltip_title">

  </div>
  <div id="dhtmltooltip_content">

  </div>
</div>

.cs :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Portail.formulaires
    {
        public partial class ucTooltip : System.Web.UI.UserControl
        {
           protected void Page_Load(object sender, EventArgs e)
           {
              Page.ClientScript.RegisterClientScriptInclude("Hint", Page.ResolveUrl("~/Scripts/hint.js"));
           }
        }
    }

This control is really simple, I simply add a reference and the control to the page I want to use it :

<%@ Register src="Controles/ucTooltip.ascx" tagname="ucTooltip" tagprefix="ucTT" %>
...
<ucTT:ucTooltip ID="tooltip" runat="server" />
... 

So, the control is loaded and the script is added to the page.

But I am trying to do something more elaborate. Given that I have a lot control that need a tooltip, I decided to add an Event(it will show and hide the tooltip) to my usercontrol's base. All the usercontrol is use inside my main page (The tooltip is accessible from this page).

First, I have tried something like that :

protected virtual void Page_Load(object sender, EventArgs e)
{
   Attributes.Add("OnMouseOver", "alert('d')");
}

On the control's base page load, I had an attribute to throw the event "OnMouseOver" to the control.

But it don't work, I am very confuse why...

8
  • look and see where the attribute is being added in the dom Commented Sep 3, 2013 at 15:22
  • Also put a break point on Attributes.Add("OnMouseOver", "alert('d')"); and make sure its being fired Commented Sep 3, 2013 at 15:24
  • I did the verefication ,and it is being fired. But it look like the attributes is not being added... Commented Sep 3, 2013 at 15:28
  • Try @Luis Miguel's suggestion and see if that helps Commented Sep 3, 2013 at 15:32
  • Also can you post a copy of your DOM ? Commented Sep 3, 2013 at 15:33

2 Answers 2

2

Although a UserControl has an Attributes property, it doesn't actually do anything. Instead, you need to put the onmouseover attribute on an element inside your .ascx markup. For example:

<div id="dhtmltooltip" style="z-index:9999999999; display:inline-block;" onmouseover="alert('d')">

If you need to add the attribute dynamically, then make the <div> a runat="server" control so that you can access its Attributes property from the code-behind file:

dhtmltooltip.Attributes.Add("onmouseover", "alert('d')");
Sign up to request clarification or add additional context in comments.

5 Comments

@VincP.: I'm not sure what you mean. Are you getting a compiler error?
No, I am getting an exception, it say object reference is null. I tried to find a solution, but it look like you can't access the div if you declare it in the base user control...
@VincP.: Did you add runat="server" to the <div>? Also, how did you declare it in the base user control?
yes it have runat="server", in the ascx : <div id="test" runat="server"></div> , but when I am trying to add an attribute to this Div, the exception throw because the object reference is null : test = null. It look like I can't access the div. This how I create the link to the base control : public partial class ucMemo : ucControl
Was the protected HtmlGenericControl test; field generated by the designer, or did you add it yourself? If you're inheriting user controls, could there be two different test fields?
2

try this:

Attributes.Add("OnMouseOver", "javascript:alert('d')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.