简体   繁体   中英

How to dynamically disable a TexBox on PostBack

Ok. Here's the background.

I have an application that is supposed to accept checks. The user has 2 radio buttons. The first radio button has a drop down list associated with it which contains the masked numbers of their previously used checking accounts.

The second radio button has three text boxes and an image of a check associated with it.

When a user hits this page, the three text boxes and the check image associated with the second radio button are disabled. Then, if the user decides he/she wants to use a new checking account, they can click the second radio button and that fires the Javascript that enables the three check boxes and the image of the check associated with that second radio button. If they click on the first radio button, it will re-disable the text boxes and hide the check image associated with the second radio button.

The problem happens when I do my server side validation. After validating all text fields, if there is a problem, I just fall out of the bottom of the code, the page posts back and the labels above the offending text fields show an error message.

One caveat: The Javascript that is supposed to fire on the OnClick event for the first radio button fires and the text fields for the second radio button are disabled. The user can click on the second radio button and the fields will enable, but this is very clunky.

Here's the enable code where I am injecting the Javascript:

Private Sub JavascriptInject()
        Dim sEnableControls As String
        Dim sDisableControls As String

        'write out the enable controls script
        sEnableControls = "<script type=""text/javascript"">function EnableControls(){ "
        sEnableControls = sEnableControls & "document.getElementById('txtRoutingNumMult').disabled=false;"
        sEnableControls = sEnableControls & "document.getElementById('txtECheckNumMult').disabled=false;"
        sEnableControls = sEnableControls & "document.getElementById('txtECHeckNameMult').disabled=false;"
        sEnableControls = sEnableControls & "document.getElementById('imgCheckMult').style.display='';"
        sEnableControls = sEnableControls & " }</script>"

        'write out the disable controls script
        sDisableControls = "<script type=""text/javascript"">function DisableControls(){ "
        sDisableControls = sDisableControls & "document.getElementById('txtRoutingNumMult').disabled=true;"
        sDisableControls = sDisableControls & "document.getElementById('txtECheckNumMult').disabled=true;"
        sDisableControls = sDisableControls & "document.getElementById('txtECHeckNameMult').disabled=true;"
        sDisableControls = sDisableControls & "document.getElementById('imgCheckMult').style.display='none';"
        sDisableControls = sDisableControls & " }</script>"

        'inject it
        ClientScript.RegisterStartupScript(Me.GetType, "EnableControls", sEnableControls)
        ClientScript.RegisterStartupScript(Me.GetType, "DisableControls", sDisableControls)

        rbECheckNew.Attributes.Add("OnClick", "EnableControls()")
        rbECheckPrev.Attributes.Add("OnClick", "DisableControls()")

    End Sub

How can I re-enable the text boxes on a failed validation postback?

You can call the following function on failed validation postback:

ClientScript.RegisterStartupScript(Me.GetType, "EnableControlsStartup", "EnableControls()", true)

Link for reference:

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM