问题描述
这是我的用户控件example.ascx
This is my user control example.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="add.ascx.cs" Inherits="WebApplication3.add" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<link rel="Stylesheet" href="style.css" />
<script type="text/javascript">
function toDo()
{
//Do something
};
</script>
<asp:TextBox ID="txt_name" onkeydown="toDo()" runat="server" CssClass="input_txt"> </asp:TextBox>
当我加载页面并将用户控件添加到页面时,我根本无法调用Java脚本函数.错误是:
When i load my page and add my user control to the page, I can not call the java script function at all.The error is:
Java脚本函数toDo是未定义的.这真的让我筋疲力尽:(
Java script function toDo is undefined. It has really exhausted me :(
更新***
我的用户控件按代码添加到页面.这是将用户控件添加到页面的服务器端代码:
My usercontrols is added to page by code. Here is the server side code to add the usercontrol to the page:
protected void lnk_new_Click(object sender, EventArgs e)
{
add add_view = LoadControl("add.ascx") as add;
Panel pnl_view = (Panel)ContentPlaceHolder1.FindControl("pnl_view");
pnl_view.Controls.Clear();
pnl_view.Controls.Add(add_view);
}
这是我得到的错误:
推荐答案
我不确定您在做什么!这是我为具有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.
这篇关于如何从用户控件调用Java脚本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!