[英]Add Javascript to an usercontrol in c#
I have an UserControl
, it is a ToolTip
. 我有一个
UserControl
,它是一个ToolTip
。
. 。 ascx :
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 :
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. 鉴于我有很多需要工具提示的控件,因此我决定在用户控件的基础上添加一个
Event
(它将显示和隐藏工具提示)。 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. 在控件的基本页面加载上,我有一个属性将事件“ OnMouseOver”抛出给控件。
But it don't work, I am very confuse why... 但这不起作用,我非常困惑为什么...
尝试这个:
Attributes.Add("OnMouseOver", "javascript:alert('d')");
Although a UserControl
has an Attributes
property, it doesn't actually do anything. 尽管
UserControl
具有Attributes
属性,但实际上不执行任何操作。 Instead, you need to put the onmouseover
attribute on an element inside your .ascx markup. 相反,您需要将
onmouseover
属性放在.ascx标记内的元素上。 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: 如果需要动态添加属性,请使
<div>
为runat="server"
控件,以便您可以从代码隐藏文件访问其Attributes
属性:
dhtmltooltip.Attributes.Add("onmouseover", "alert('d')");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.