0

I have got a problem with making textbox empty using JavaScript function.

If checkbox is unchecked the asp:Textbox should make disabled and empty. It happens, but in code behind all the time is visible previous value which was entered before in that textbox.

<asp:RadioButton ID="radio_fx_no" runat="server"  Text="NO" GroupName="optradio1" onclick="CheckBoxChangedDisableFx(this);" />
                 
<asp:RadioButton ID="radio_fx" runat="server"  Text="YES" GroupName="optradio1" onclick="CheckBoxChangedAbleFx(this);" />
              
<asp:TextBox ID="txt_fx" ClientIDMode="Static" runat="server" Enabled="false" />
          
<script>
function CheckBoxChangedAbleFx(checkbox) {
    if (checkbox.checked == true) {
        document.getElementById('<%= txt_fx.ClientID %>').disabled = false;
    }
}
function CheckBoxChangedDisableFx(checkbox) {
    if (checkbox.checked == true) {
        document.getElementById('<%= txt_fx.ClientID %>').value = "";
        document.getElementById('<%= txt_fx.ClientID %>').disabled = true;
    }
}
</script>

Any ideas what is wrong in my code?

4
  • onclick handle server side event, you need to define OnClientClick Commented Aug 7, 2017 at 6:46
  • How about OnClientClick="CheckBoxChangedAbleFx(this);"? Your intention is empty textbox in client-side, right? Commented Aug 7, 2017 at 6:48
  • Client and server side when i want to save all of changes. But i dont want to reload page after every change of checkbox. I receive empty textbox on client-side but not on server. Commented Aug 7, 2017 at 6:51
  • I assumed you're already have empty textbox in client-side context, now it's time to show server-side code you have to make textbox value is empty either by AJAX or postback. Commented Aug 7, 2017 at 7:00

2 Answers 2

1

This problem is called the Cacheing the browser data. You can try three things in this case: 1) Clear value of textbox in pageload event at every postback:

if (IsPostBack)
    {
        txt_fx.text = "";
    }

and in your aspx code make text="":

<asp:TextBox ID="txt_fx" Text="".../>

2) Set AutoComplete property of textbox to off for html input

or disable autocompletetype property of asp textbox

<asp:TextBox ID="txt_fx" autocompletetype="disabled".../>

3) or Set AutoComplete of your FORM tag of your page to "off"

If you want to clear actual value of textbox on server side without postback then you need to use AJAX.

Hope it will help you..!!

Sign up to request clarification or add additional context in comments.

Comments

0

in .cs file,Page_Load method,plus such code:

if(!Page.IsPostBack)
{
    radio_fx.Attributes.Add("onClick", "return CheckBoxChangedState(this,false);");
    radio_fx_no.Attributes.Add("onClick", "return CheckBoxChangedState(this,true);");
}

and in .aspx file,change as follows:

<script type="text/javascript">

    function CheckBoxChangedState(checkbox, state) {
        if (checkbox.checked == true) {
            document.getElementById('<%= txt_fx.ClientID %>').value = "";
                document.getElementById('<%= txt_fx.ClientID %>').disabled = state;
            }
        }

<asp:RadioButton ID="radio_fx_no" runat="server" AutoPostBack="false"  Text="NO" GroupName="optradio1" />
<asp:RadioButton ID="radio_fx" runat="server" AutoPostBack="false"  Text="YES" GroupName="optradio1"  />

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.