客户端和服务器端之间的onclick

时间:2010-08-30 14:59:48

标签: javascript asp.net

我想增强复合控件的客户端,我的方法是在JavaScript上重新创建所有方法,所以这里有一些麻烦:

  1. 我可以在客户端调用onclick事件,否则在服务器端调用?
  2. 声明table.onclick=SelectRow(event)触发了一个错误!
  3. 代码:

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

1 个答案:

答案 0 :(得分:0)

您可以为Firefox下载Firebug插件并调试javascript代码,它是一个很棒的工具,让javascript编码的生活更轻松。

你有javascript与c#代码的混合,我无法理解你的js代码在哪里以及服务器端代码在哪里。