2010-08-30 276 views
1

我要增強複合控件的客戶端之間的onclick,我的做法是重新創建所有的JavaScript方法,所以在這裏我有一些麻煩:客戶端和服務器端

  1. 我可以調用客戶端onclick事件另外在服務器端?
  2. 聲明table.onclick=SelectRow(event)發生錯誤!

代碼:

function Control_Init() { 
    if(!(document.getElementById)) { return; } 
for(var i = 0; i < Controls.length; i++) { 
    var info = Controls[i]; 
    Control_Load(info); 
    } 
} 

function Control_Load(info) { 
    var leftBox = document.getElementById(info.LeftBoxID); 
    var rightBox = document.getElementById(info.RightBoxID); 
    var moveRight = document.getElementById(info.MoveRightID); 
    var moveAllRight = document.getElementById(info.MoveAllRightID); 
    var moveLeft = document.getElementById(info.MoveLeftID); 
    var moveAllLeft = document.getElementById(info.MoveAllLeftID); 
    var moveUp = document.getElementById(info.MoveUpID); 
    var moveDown = document.getElementById(info.MoveDownID); 
    if (leftBox == null) { 
     return; 
    } 
    var ControlParent = new Object(); 
    ControlParent.LeftBox = leftBox; 
    ControlParent.RightBox = rightBox; 
    ControlParent.MoveRight = moveRight; 
    ControlParent.MoveAllRight = moveAllRight; 
    ControlParent.MoveLeft = moveLeft; 
    ControlParent.MoveAllLeft = moveAllLeft; 
    ControlParent.MoveUp = moveUp; 
    ControlParent.MoveDown = moveDown; 
    ControlParent.SetEnabled = Control_SetEnabled; 
    leftBox.Parent = ControlParent; 
    //leftBox.onclick = Control_SelectRow(event); 
    rightBox.Parent = ControlParent; 
    //rightBox.onclick = Control_SelectRow(event); 
    if (moveUp != null && moveDown != null) { 
    rightBox.SetUpDownEnabled = Control_SetUpDownEnabled; 
    rightBox.onchange = rightBox.SetUpDownEnabled; 
    } else { 
     rightBox.SetUpDownEnabled = function() {}; 
    } 
    moveRight.Parent = ControlParent; 
    moveRight.DoCommand = Control_MoveRight; 
    moveRight.onclick = moveRight.DoCommand; 
    if (moveAllRight != null) { 
     moveAllRight.Parent = ControlParent; 
     moveAllRight.DoCommand = Control_MoveAllRight; 
     moveAllRight.onclick = moveAllRight.DoCommand; 
    } 
    moveLeft.Parent = ControlParent; 
    moveLeft.DoCommand =Control_MoveLeft; 
    moveLeft.onclick = moveLeft.DoCommand; 
    if (moveAllLeft != null) { 
     moveAllLeft.Parent = ControlParent; 
     moveAllLeft.DoCommand = Control_MoveAllLeft; 
     moveAllLeft.onclick = moveAllLeft.DoCommand; 
    } 
    if (moveUp != null) { 
     moveUp.Parent = ControlParent; 
     moveUp.DoCommand = Control_MoveUp; 
     moveUp.onclick = moveUp.DoCommand; 
    } 
    if (moveDown != null) { 
     moveDown.Parent = ControlParent; 
     moveDown.DoCommand = Control_MoveDown; 
     moveDown.onclick = moveDown.DoCommand; 
    } 
    if (!moveRight.disabled) { 
     ControlParent.SetEnabled(); 
     if (moveUp != null) { 
      rightBox.SetUpDownEnabled(); 
     } 
    } 
} 

function Control_SelectRow(table) { 
    var SelectedRow = table.srcElement.parentNode; 
    var cbox = table.srcElement; 
    if (cbox.checked == true) { 
     // get Row element 
     SelectedRow = SelectedRow.parentNode; 
     SelectedRow.style.backgroundColor = "Black"; //select 
     SelectedRow.style.color = "white"; //select 
    } 
    else { 
     // get Row element 
     SelectedRow = SelectedRow.parentNode; 
     SelectedRow.style.backgroundColor = "white"; //deselect 
     SelectedRow.style.color = "black"; //deselect 
    }   
} 

function Control_SetEnabled() { 
    var leftItemsEmpty = (this.LeftBox.rows.length == 0); 
    var rightItemsEmpty = (this.RightBox.rows.length == 0); 
    this.MoveRight.disabled = leftItemsEmpty; 
    if (this.MoveAllRight != null) { 
     this.MoveAllRight.disabled = leftItemsEmpty; 
    } 
    this.MoveLeft.disabled = rightItemsEmpty; 
    if (this.MoveAllLeft != null) { 
     this.MoveAllLeft.disabled = rightItemsEmpty; 
    } 
    this.RightBox.SetUpDownEnabled(); 
} 

function Control_SetUpDownEnabled() { 
} 

function Control_MoveRight() { 
    confirm("moveright"); 
    Control_MoveSelectedItems(this.Parent.LeftBox,this.Parent.RightBox); 
    //this.Parent.SetEnabled(); 
    return false; 
} 

function Control_MoveAllRight() { 
    Control_MoveAllItems(this.Parent.RightBox,this.Parent.LeftBox); 
    //this.Parent.SetEnabled(); 
    return false; 
} 

//function to add selected rows from Left Table to Right Table 
function Control_MoveSelectedItems(source,target) { 
    // delete me please 
    alert("you called me !"); 
    var LeftTable =source; 
    var RightTable =target; 
    var i = 0; 
    var j = 0; 
    var RowPresent = 0; //this variable checks if a row is already added to the Right Table 
    if (RightTable.rows.length > 0) { 
     if (RightTable.rows[0].childNodes[0].innerHTML == "Now Data Available") { 
      RightTable.deleteRow(0); 
     } 
    } 
    for (i = 0; i < LeftTable.rows.length; i++) {   
     //this code adds the selected rows to Right Table if not already added 
     if (RowPresent == 0) { 
      RightRow = RightTable.insertRow(); 
      RightRow.id = "RightRow" + i; 
      RightCell1 = RightRow.insertCell(); 
      RightCell1.align = "left"; 
      RightCell2 = RightRow.insertCell(); 
      RightCell2.align = "left"; 
      RightCell3 = RightRow.insertCell(); 
      RightCell3.align = "left"; 
      RightCell4 = RightRow.insertCell(); 
      RightCell4.align = "left"; 
      RightCell5 = RightRow.insertCell(); 
      RightCell5.align = "left"; 

      // declare textbox and button 
      var NumericField = document.createElement("input"); 
      var BtnValidate = document.createElement("input"); 
      NumericField.setAttribute("type", "text"); 
      NumericField.setAttribute("id", "NumericField"); 
      NumericField.style.width = "70px" 
      BtnValidate.setAttribute("type", "submit"); 
      BtnValidate.setAttribute("id", "btnValidate"); 
      BtnValidate.setAttribute("value", "V"); 
      BtnValidate.style.backgroundColor = "white"; 
      BtnValidate.style.color = "green"; 
      // declare right checkbox 
      var RightCB = document.createElement("input"); 
      RightCB.setAttribute("type", "checkbox"); 
      RightCB.setAttribute("id", "RightCB"); 
      RightCB.style.width = "30px" 

      RightCell1.appendChild(NumericField); 
      RightCell2.appendChild(BtnValidate); 
      RightCell3.appendChild(RightCB); 
      RightCell4.innerHTML = LeftTable.rows[i].childNodes[1].innerHTML; 
      RightCell4.style.width = LeftTable.rows[i].childNodes[1].style.width; 
      RightCell5.innerHTML = LeftTable.rows[i].childNodes[2].innerHTML; 
      RightCell5.style.width = LeftTable.rows[i].childNodes[2].style.width;   
     } 
    }  
} 
+2

如何提出關於SO的問題 - http://tinyurl.com/so-hints – Oded 2010-08-30 15:04:12

回答

0

您可以下載Firebug插件的Firefox和調試你的JavaScript代碼,這是一個偉大的工具,使生活更輕鬆的JavaScript編碼。

你有混合的JavaScript與C#代碼,我不能理解你的js代碼在哪裏,服務器端代碼在哪裏。

+0

所有代碼都是javascript,這裏的問題是如何在服務器腳本旁邊執行客戶端腳本? – Nazm 2010-08-31 13:57:07