0

I have a Web user control named UserControl.ascx and a JavaScript function 'GetValues()' to return some value is embedded in that ascx page. I've compiled the ascx page into a dll and used that in another web application.But now the problem is when I tried to call the user control JavaScript function from web application, JS Debugger showing 'GetValues' is undefined error. Is there any way solve this issue ?

in ASCX File:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ICEImage.ascx.cs" Inherits="ICEImage" %>
   <div>                 
         <asp:TextBox ID="txtValue" runat="server" BackColor="#FFFFC0" ReadOnly="True" Width="150px" 
   </div>
<script type="text/javascript">
function GetValue()
{
  return parseInt(document.getElementById('<%=txtValue.ClientID%>').value);
}
</script>

ASPX File:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="ASP" Assembly="App_Web_iceimage.ascx.cdcab7d2" Namespace="ASP" %>

<!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>Sample Page</title>
</head>
<body style="background-color: #383838">
    <form id="form1" runat="server">
    <ASP:iceimage_ascx ID="ICEImage1" runat="server"></ASP:iceimage_ascx>  
    <button id="btnSave" style="width: 85px; height: 29px" type="button" onclick="GetValue()">Save</button>          
    </form>
</body>  
</html>

1 Answer 1

0

You can define your function from server code like this and add this on Page_PreRender for example

if (!Page.ClientScript.IsClientScriptBlockRegistered("FunctionGetValue"))
        {
            string script = @"function GetValue() {
                   return parseInt(document.getElementById('"+txtValue.ClientID+"').value);
                   }";
            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "FunctionGetValue", script, true);

        }
6
  • No,I need to call the function from web applications JavaScript block. Is it possible ? Commented Dec 11, 2012 at 10:46
  • Could you post a little bit more of your code? And how do you include your control in web page (Register derective from aspx or LoadControl from code)? Commented Dec 11, 2012 at 10:54
  • Using Register Directive. %@ Register TagPrefix="ASP" Assembly="App_Web_iceimage.ascx.cdcab7d2" Namespace="ASP" %> Commented Dec 11, 2012 at 10:57
  • But my example how to declare function in ascx control. You can call this function from your web application and it return value, because it will be present on page. Commented Dec 11, 2012 at 11:06
  • What the reason to use script block from ascx? Commented Dec 11, 2012 at 11:27

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.