Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
it will work when your textbox lose focus... – Amit Singh Jun 12 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. – Rich Andrews Jun 12 at 16:27
@AmitSingh I know but it is not working – kartal Jun 12 at 16:27
@RichAndrews yes I save some data and calculate others – kartal Jun 12 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. :( – Michael Perrenoud Jun 12 at 16:39
show 1 more comment

1 Answer

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.

share|improve this answer
So this function will work on textchange or only when page is loading ? – kartal Jun 12 at 17:07
@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. – Michael Perrenoud Jun 12 at 17:08
It is not working for me . did you miss anything because I am not good in javascript – kartal Jun 12 at 17:14
@kartal: you need to download and link the jQuery JavaScript file for this code to work. – Michael Perrenoud Jun 12 at 17:17
I already have it <script type="text/javascript" src="code.jquery.com/jquery-1.8.3.min.js"></script>; – kartal Jun 12 at 17:18
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.