2013-03-27 50 views
-1

我在JavaScript中有大約10到12個函數。當控制獲得主要2或3個功能使用的焦點和其他功能時,我想在事件中調用其中的2個或3個。在asp.net網站中添加javascript函數的簡單方法

我有這個虛擬鍵盤的所有功能...我想添加虛擬鍵盤作爲母版頁(我已經創建了使用HTML表單的Java腳本虛擬鍵盤)。

我已經將該代碼添加到母版頁中,並且我的小鍵盤正在工作,但它僅適用於簡單的HTML控件(即沒有runat =「server」屬性)。當我添加runat =「server」時它不起作用。

我不知道如何從內容或子頁面調用這個函數:

我的代碼如下: - (此代碼爲JavaScript,我在母版頁添加...這是部分的代碼(不完全))

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %> 

<!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 id="Head1" runat="server"> 
    <title></title> 
    <script language="javascript" type="text/javascript"> 

     function setId(el) { 
      id = el; 
      //alert(el); 
      document.getElementById(id).focus(); 
      reset(); 
      return 0; 
     } 

     function restoreCode(evt) { 
      ButtonUp(kcode); 
     } 

     function writeKeyPressed(evt) { 
      InsertChar('k', kcode); 

      return false; 
     }; 

     function InsertChar(mode, c) { 
      document.getElementById(id).focus(); 
     } 

     function ButtonDown(c) { 
      reset(); 
     } 

     function ButtonUp(c) { 
      //codes 
      resets(); 
     } 

     function Shift() { 
      //codes 
     } 
    </script> 

當我從內容頁是具有代碼runat="server"屬性,那麼它會工作HTML控件調用它。

<input id="txt_pwd" type="password" onfocus="setId('txt_pwd');" /> 

當我從內容頁是具有代碼runat="server"屬性,那麼它將無法工作的ASP控制調用它。

<input id="txt_pwd" type="password" onfocus="setId('txt_pwd');" runat="server" /> 

請任何人都可以告訴我解決方案,我將從內容頁面使用服務器端控件調用我的功能(在母版頁上)。

+1

你需要一些更多的澄清,得到幫助:請包括你如何稱呼它的例子。你得到什麼錯誤? 「它不會起作用」是什麼意思?另外,你如何使用沒有runat =「server」的asp控件? – MikeSmithDev 2013-03-27 12:56:18

+0

請詳細說明。 – 2013-03-27 13:16:51

+0

@MikeSmithDev ..sorry它是隻有html控制沒有runat =「服務器」...並且它不會工作意味着...當我按下我的虛擬鍵盤上的鍵寫入文本框,這是不是發生在這種情況下... – nik 2013-03-27 13:45:53

回答

0

當您使用runat="server"時,它不起作用,因爲.NET更改了控件的ID。因此,在runat="server"的情況下,您的JavaScript正在尋找錯誤的ID。解決這個問題的一種方法是告訴.NET不要使用ClientIDMode更改ID。

<input id="txt_pwd" type="password" clientidmode="Static" onfocus="setId('txt_pwd');" runat="server" /> 

還有其他的方法來獲取客戶端ID生成的,如果你不使用靜態模式,因爲你不會希望使用中繼器中,或者如果有機會的ID可能是重複的。在這種情況下,你可以使用.ClientID,如:

document.getElementById('<%= txt_pwd.ClientID %>'); 

Reading on ClientID

一種更簡單的方法可能只是使用onfocus="setId(this);"然後

function setId(el) { 
    id = el.id; 
相關問題