2012-12-11 24 views
0

我有一個名爲UserControl.ascx的Web用戶控件和一個JavaScript函數'GetValues()'以返回某個值嵌入在該ascx頁面中。我已經將ascx頁面編譯爲一個dll,並將其用於另一個web應用程序。但現在問題出現在我嘗試從Web應用程序調用用戶控件JavaScript函數時,JS Debugger顯示'GetValues'未定義 錯誤。有什麼辦法解決這個問題嗎?從消費應用程序調用用戶控件JavaScript函數JavaScript

在ASCX文件:

<%@ 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文件:

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

回答

0

可以定義從服務器代碼的功能,這樣並添加此上Page_PreRender例如

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

     } 
+0

沒有,我需要從Web應用程序JavaScript塊中調用該函數。可能嗎 ? – Senan

+0

你可以發佈多一點你的代碼嗎?你如何將你的控件包含在網頁中(註冊從aspx或LoadControl代碼中取消)? –

+0

使用註冊指令。 %@ Register TagPrefix =「ASP」Assembly =「App_Web_iceimage.ascx.cdcab7d2」Namespace =「ASP」%> – Senan