1

I want to change my Checkbox states when I write any thing in a textbox so I did

<asp:CheckBox ID="chkIceChest" Text="Ice Chest" runat="server" />
<asp:TextBox ID="txtIceChest" Text="$ cost" runat="server" CssClass="moch-cost" AutoPostBack="true" OnTextChanged="CalculateStand" />

and

protected void CalculateStand(object s, EventArgs e)
{
     double total = 0, cost = 0;
     if (txtIceChest.Text.Length > 0 && txtIceChest.Text != "$ cost")
     {
         cost = Convert.ToDouble(txtIceChest.Text.Replace("$", ""));
         total += cost;
         if (cost > 0) chkIceChest.Checked = true;
     }
}

but it is not working !!!! why ?

6
  • it will work when your textbox lose focus... Commented Jun 12, 2013 at 16:26
  • 6
    I really think you should look at using client side script for this. Do you really want to post back to the server every time the textbox value changes just to update something in the UI? Madness my friend, madness. Commented Jun 12, 2013 at 16:27
  • @AmitSingh I know but it is not working Commented Jun 12, 2013 at 16:27
  • @RichAndrews yes I save some data and calculate others Commented Jun 12, 2013 at 16:29
  • 1
    @RichAndrews, I agreed with you in so much that I went ahead and provided the OP that solution. I've been maintaining some old projects here at work lately and somebody used an UpdatePanel to do something like this. :( Commented Jun 12, 2013 at 16:39

1 Answer 1

4

How about a client-side approach? I mean, to be perfectly honest, UI operations like this are best fit there. First, you'll need to change your markup a little:

<asp:CheckBox ID="chkIceChest"
              ClientIDMode="Static"
              Text="Ice
              Chest" runat="server" />
<asp:TextBox ID="txtIceChest"
             ClientIDMode="Static"
             Text="$ cost"
             runat="server"
             CssClass="moch-cost" />

and now you'll need a little JavaScript, just throw this on your ASPX page:

<script type="text/javascript">
    $('#txtIceChest').blur(function() {
        var total = 0;
        var cost = 0;
        var val = $('#txtIceChest').val();
        var chkIceChest = $('#chkIceChest');

        if (val && val.length > 0 && val != "$ cost") {
            cost = val.replace('$', '');
            total += cost;
            if (cost > 0) {
                chkIceChest.attr('checked', 'checked');
            }
        }
    });
</script>

but this approach would happen in the browser and real-time for you. Also, the value would get posted back just as if the user had checked the check box.

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

8 Comments

So this function will work on textchange or only when page is loading ?
@kartal, currently it works on blur. Like your current code did. However, if you want it on text changed then just use change instead of blur.
It is not working for me . did you miss anything because I am not good in javascript
@kartal: you need to download and link the jQuery JavaScript file for this code to work.
I already have it <script type="text/javascript" src="code.jquery.com/jquery-1.8.3.min.js"></script>
|

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.