textbox – update label onkeyup

I’m generating some labels, textboxes and so on from my codebehind, and i need to update a label when a key is pressed in the textbox, i’ve been looking at this: How do I make a Textbox Postback on KeyUp?
And tried to convert it to be dynamic – but i can’t get this to work.

This is in my code:

the header:

<script type="text/javascript">
    function RefreshUpdatePanel(id) {
        __doPostBack(id, '');

the body:

<div style="width:800px; margin:0 auto;">
        <asp:Panel ID="testpanel" runat="server"></asp:Panel>
        <asp:ScriptManager runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="update" runat="server">
                <asp:Panel ID="pnlProdKits" runat="server"></asp:Panel>


and my codebehind:

TextBox txtQuantity = new TextBox() { Width = 30, Text = "0", MaxLength = 4, ID = prod.Number };
txtQuantity.Style["text-align"] = "right";
txtQuantity.TextChanged += delegate(object o, EventArgs e) { lblPriceTotal.Text = "new text!"; };
                txtQuantity.Attributes.Add("onkeyup", string.Format("RefreshUpdatePanel(ContentPlaceHolder1_{0});", txtQuantity.ClientID));

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = txtQuantity.ClientID;

and then i add the textbox to the first panel:


the label is added to the panel inside the update panel:


1 Solution

As discussed,

You can try the following script,

<script type="text/javascript">

function RefreshUpdatePanel(){
//Find all textboxes which you want to get the values by their `ClientID` property. Like,
var a = $('#' + '<%= txtQuantity.ClientID %>').val();
var b = $('#' + '<%= txtQuantity2.ClientID %>').val();

//Since the value will be in string, you need to convert it to numeric for mathematical operations

var sum = parseFloat(a) + parseFloat(b);

//Then find the label and assign the result

$('#'+'<%= Label1.ClientID %>').val(sum.toFixed(2));



Rest of the code for adding onkeyup/onkeydown will be same.

You can try just adding alert(1); in the function to check if it hits it or not.