1

I have two controls, textbox and dropdown, I want to calculate the sum of those two fields on asp button click. This is what I have: Dropdown Value

<asp:DropDownList ID="DropDownList3" runat="server" CssClass="dropdown" Width="74px">
  <asp:ListItem Text="1" Value="1" />
  <asp:ListItem Text="4" Value="4" />
  <asp:ListItem Text="5" Value="5" />
  <asp:ListItem Text="9" Value="9" />
  <asp:ListItem Text="10" Value="10" />
  <asp:ListItem Text="11" Value="11" />
  <asp:ListItem Text="12" Value="12" />
  <asp:ListItem Text="13" Value="13" />
</asp:DropDownList>

TextBox Value

<asp:TextBox ID="TextBox9" runat="server" CssClass="input-mini" /><br />
<asp:Button ID="Button3" runat="server" Text="Calculate Sum" CssClass="btn btn-minibtn-info" />
Textbox Result <asp:TextBox ID="TextBox10" runat="server" CssClass="input-mini" />

I want on button click to calculate the sum of the dropdown + textbox9 and then put the value in TextBox10. Any idea how can I achieve that?

0

2 Answers 2

4

Register to client click:

<asp:Button OnClientClick="return ButtonClick();" ... />

JS:

function ButtonClick() {
    var ddlVal = parseInt($('#<%=DropDownList3.ClientID%>').val());
    var tb9Val = parseInt($('#<%=TextBox9.ClientID%>').val());
    $('#<%=TextBox10.ClientID%>').val(ddlVal + tb9Val);

    return false;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Don't you need the ClientID property in your example? So '#<%=TextBox10.ClientID%>' instead?
One other thing, I think you'll have to convert the strings into ints via parseInt, otherwise you'll just concatenate the values.
@A-Dubb Quite right. I wrote it and didn't quality check. Thanks for the corrections!
No problem. anything I can do to help. Good answer.
1
$(function() {
            $("#<%=Button3.ClientID%>").click(function() {
            var a = parseInt($('#<%=TextBox9.ClientID%>').val());
            var b = parseInt($('#<%=DropDownList3.ClientID%>').val());
            $('#<%=TextBox10.ClientID%>').val(a + b);
            });
        });

1 Comment

You need to be using "#<%=Button3.ClientID%>"... etc. See other answer.

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.