简体   繁体   中英

Dynamic change to item in ASP.NET C# without postback?

In ASP.NET C#, I have a checkbox and a text box. I would like to make the text box's visibility dependany upon the checkbox (ie if the box is checked, the text box is visible, and if it is not checked, the text box is hidden), but I would like it done "immediately" as opposed to through a postback. Is this possible?

You need to write a client side script in javascript that handles this behavior. If you don't know what's javascript google it.

With jquery you can do something like:

$(document).ready(function() {
  $('.mycheckbox').change(function() {
    $('.mycheckbox').is(':checked') ? $('.mytextbox').show() : $('.mytextbox').hide();
  });
});

where mycheckbox and mytextbox are classes for your inputs.

Or you can use ClientID :

<asp:CheckBox runat="server" ID="cb" />
<asp:TextBox runat="server" ID="txt" />
<script type="text/javascript">
$(document).ready(function() {
  $('#<%= cb.ClientID %>').change(function() {
    $('#<%= cb.ClientID %>').is(':checked') ? $('#<%= txt.ClientID %>').show() : $('#<%= txt.ClientID %>').hide();
  });
});
</script>

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