0

My UserControl has a TextBox, that is subscribed to OnTextChanged event. However, since a lots of business logic and integrations happens on server, I want to disable a form while postback is being performed with some client-side javascript and I'm not sure how to achieve it the right way.

Can I solve this with ClientScriptManager.GetPostBackEventReference?

Edit: as my question seems to be misunderstood:

TextBox is subscribed to event OnTextChanged="tb_TextChanged" which will result on client in onchange=__doPostBack('tb') so I want to inject my javascript disableForm() to onchange DOM event. I know how to implement disableForm(), the question is how to inject my javascript properly?

2 Answers 2

1

I would suggest you use the javascript onblur event and check if the field value is changed. If so, you can use jQuery like the below to disable the form elements.

To disable a form element such as a text input or a button (with a made-up id: #elm):

$("#elm").attr("disabled", "disabled");

To enable a disabled form element:

$("#elm").removeAttr("disabled");
Sign up to request clarification or add additional context in comments.

3 Comments

Well, this is not exactly the issue. I know how I will disable the form, but I need to do the postback as well. TextBox is subscribed to event OnTextChanged="tb_TextChanged" which will result on client in onchange=__doPostBack('tb') so I want to inject my javascript disableForm() to onchange DOM event. The question is how to do that properly?
You can use Button1.Attributes.Add("onblur", "disableForm()");
Actually it is the "onchange" the one that I need and I was really surprised, when adding tb.Attributes.Add("onchange", "LockForm();ShowLoadingMessage();"); on Page_Load, the __doPostBack was just appended to my javacript in DOM property "onchange", so the ting works perfectly. I would expect that ASP.NET would just override whatever is in "onchange" with __doPostback. Thanks.
0

You should be able to do this with some simple JavaScript:

<script type="text/javascript">
    disableFormFields = function(){
        if (document.all || document.getElementById){
            for (i = 0; i < document.forms[0].elements.length; i++){
                var el = document.forms[0].elements[i];
                if (el){
                    el.disabled = true;
                }
            } 
        }
    }
</script>
<asp:TextBox ID="TextBox1" runat="server" onchange="disableFormFields();" OnTextChanged="TextBox1_TextChanged" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.