How to control ASP.NET Validator Controls Client Side validation

前端 未结 1 1208
慢半拍i
慢半拍i 2021-01-06 15:25

Below is the output of my page after it runs the below code:

I\'m using ValidatorEnable client side validation in my situation (if you know better way t

1条回答
  •  爱一瞬间的悲伤
    2021-01-06 15:44

    got it working for you... gridview:

    
            
                
                
                
                
                    
                        
                        
                                    ----
                                    Month
                                    At End
                                    At Travel
                                
                        
    
                    
                
                
                    
                        
                    
                
            
        
    

    CS:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox checkbox1 = e.Row.FindControl("checkbox1") as CheckBox;
                RequiredFieldValidator rfv = e.Row.FindControl("rfv") as RequiredFieldValidator;
                DropDownList drpPaymentMethod = (DropDownList)e.Row.FindControl("drpPaymentMethod");
                // you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element
                checkbox1.Attributes.Add("onclick", "UpdateValidator('" + checkbox1.ClientID + "','" + drpPaymentMethod.ClientID + "','" + rfv.ClientID + "');");
                if (!checkbox1.Checked)
                    drpPaymentMethod.Attributes.Add("disabled", "disabled");
            }
        }
    

    javascript:

    function UpdateValidator(chkID, drpID, validatorid) {
            //enabling the validator only if the checkbox is checked
            var enableValidator = $("#" + chkID).is(":checked");
    
            if (enableValidator)
                $('#' + drpID).removeAttr('disabled');
            else
                $('#' + drpID).attr('disabled', 'disabled');
    
            var vv = $('#' + validatorid).val();
    
            ValidatorEnable(document.getElementById(validatorid), enableValidator);
        }
    

    0 讨论(0)
提交回复
热议问题