简体   繁体   English

如何在PostBack上动态禁用TexBox

[英]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. 用户有2个单选按钮。 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. 然后,如果用户决定要使用新的支票帐户,则可以单击第二个单选按钮,并触发启用三个复选框的Javascript以及与该第二个单选按钮关联的支票图像。 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. 一个警告:应该触发第一个单选按钮的OnClick事件的Javascript,而禁用第二个单选按钮的文本字段。 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: 这是我在其中注入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) ClientScript.RegisterStartupScript(Me.GetType,“ EnableControlsStartup”,“ EnableControls()”,true)

Link for reference: 参考链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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