This is my user control example.ascx
这是我的用户控件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>
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脚本函数。错误是:
Java script function toDo is undefined. It has really exhausted me :(
Java脚本函数toDo未定义。它真的让我疲惫不堪:(
UPDATE***
UPDATE ***
My usercontrols is added to page by code. Here is the server side code to add the usercontrol to the page:
我的usercontrols按代码添加到页面。以下是将usercontrol添加到页面的服务器端代码:
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);
}
And this is the error I got:
这是我得到的错误:
2 个解决方案
#1
1
I am not sure what you are doing! Here's my markup for user control which has javascript function toDo() aswell:
我不确定你在做什么!这是我的用户控件标记,它具有javascript函数toDo()以及:
<%@ 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>
and here's my implementation page:
这是我的实施页面:
<%@ 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>
And when I press any key in the text box, I receive message from the toDo() function:
当我按下文本框中的任意键时,我会收到来自toDo()函数的消息:
So, there's nothing wrong with the code you provided.
所以,您提供的代码没有任何问题。
#2
0
The issue was that my function within the user control was not present when my user control was added! I add the same java script function to the head section of my master page and it worked. I still don't know the reason of that exception though, But i got what i wanted.
问题是当我添加用户控件时,我的用户控件中的函数不存在!我将相同的java脚本函数添加到我的母版页的head部分,它工作正常。我仍然不知道这个例外的原因,但我得到了我想要的东西。
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication3.Site" %>
<!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>
<link rel="Stylesheet" href="style.css" />
<script type="text/javascript">
function toDo() {
alert("HAHAH");
};
</script>
</head>
#1
1
I am not sure what you are doing! Here's my markup for user control which has javascript function toDo() aswell:
我不确定你在做什么!这是我的用户控件标记,它具有javascript函数toDo()以及:
<%@ 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>
and here's my implementation page:
这是我的实施页面:
<%@ 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>
And when I press any key in the text box, I receive message from the toDo() function:
当我按下文本框中的任意键时,我会收到来自toDo()函数的消息:
So, there's nothing wrong with the code you provided.
所以,您提供的代码没有任何问题。
#2
0
The issue was that my function within the user control was not present when my user control was added! I add the same java script function to the head section of my master page and it worked. I still don't know the reason of that exception though, But i got what i wanted.
问题是当我添加用户控件时,我的用户控件中的函数不存在!我将相同的java脚本函数添加到我的母版页的head部分,它工作正常。我仍然不知道这个例外的原因,但我得到了我想要的东西。
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication3.Site" %>
<!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>
<link rel="Stylesheet" href="style.css" />
<script type="text/javascript">
function toDo() {
alert("HAHAH");
};
</script>
</head>