且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从用户控件调用Java脚本函数

更新时间:2022-06-20 01:15:13

我不确定您在做什么!这是我为具有JavaScript功能toDo()的用户控件添加的标记:

I am not sure what you are doing! Here's my markup for user control which has javascript function toDo() aswell:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="add.ascx.cs" Inherits="add" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<link rel="Stylesheet" href="style.css" />

<script type="text/javascript">
    function toDo() {
        alert("This worked!");
    };
</script>
<asp:TextBox ID="txt_name" onkeydown="toDo()" runat="server" CssClass="input_txt">   </asp:TextBox>

这是我的实现页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register src="add.ascx" tagname="add" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>   

</head>
<body>
    <form id="form1" runat="server">
    <div>

        <uc1:add ID="WebUserControl1" runat="server" />

    </div>
    </form>
</body>
</html>

当我按文本​​框中的任何键时,我会收到来自toDo()函数的消息:

And when I press any key in the text box, I receive message from the toDo() function:

因此,您提供的代码没有错.

So, there's nothing wrong with the code you provided.