1

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>

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 script function toDo is undefined. It has really exhausted me :(

UPDATE***

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);

    }

And this is the error I got: My screenshot of error

4
  • The issue is probably because your user control is not visible when page first loads. Make sure your toDo function is actually rendered by checking the page source. Commented Aug 3, 2013 at 21:12
  • ho to do that? any help? Commented Aug 4, 2013 at 7:02
  • I tested it on another project and it worked. but in my project it does not work. Commented Aug 4, 2013 at 7:13
  • You should do a little research before posting... check this: stackoverflow.com/a/15988806/690329 Commented Aug 4, 2013 at 15:51

2 Answers 2

1

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>

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:

enter image description here

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

Sign up to request clarification or add additional context in comments.

1 Comment

no it is not working , I updated my question. please take a look
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.

<%@ 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>

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.