Disabled asp.net button disables validation for button

后端 未结 3 1120
暖寄归人
暖寄归人 2021-01-21 17:11

I have an aspx page that contains a checkbox, and a button. The button is disabled by default until the user checks the checkbox. It looks like when I add the attribute enable

相关标签:
3条回答
  • 2021-01-21 17:46

    I think it would work if you enabled/disabled the button on the checkBox server-side event rather than client side since the associated client-side validation code will be generated to run for ValidationGroup of the button

    0 讨论(0)
  • 2021-01-21 17:50

    Asp.net is stupid because when a control is disabled, there's all sorts of stuff that won't happen with it.

    In this case, the js code to cause client side validation will not be created for a disabled button.

    If you try to manually add in the clientside js code from the code behind,

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        registerButton.OnClientClick = String.Format("javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('{0}', '', true, '', '', false, false))", registerButton.UniqueID)
    End Sub
    

    It will not work because asp.net will not render all the attributes for a disabled control.

    There are two solutions then. Either disable the control with JS at client side or add the javascript to perform a postback with validation at client side.

    What I did for my program was to add code to create js code to disable the button at the client side:

        If btnSubmit.Enabled = False Then
            'Asp.net will not render all the attributes for a control that is disabled.  Even if you
            'try to set the OnClientClick attribute manually in code behind for the control, it will 
            'not work.  Asp.net will not render the attribute.  This is a workaround for this problem.
            btnSubmit.Enabled = True
    
            strJavaScript &= "btnSubmit.disabled = true;"
    
        End If
    
        ClientScript.RegisterStartupScript(Page.GetType(), "myStartupScript", strJavaScript, True)
    

    Note that btnSubmit is a javascript variable that is already defined in my client side code.

    0 讨论(0)
  • 2021-01-21 17:51

    It seems like the validation should be attached to the checkbox, not the button.

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