asp.net文本框 - 更新标签onkeyup

时间:2013-12-08 10:44:12

标签: c# asp.net

我正在从代码隐藏中生成一些标签,文本框等,我需要在文本框中按下某个键时更新标签,我一直在看这个:How do I make a Textbox Postback on KeyUp? 并试图将其转换为动态 - 但我不能让它工作。

这是我的asp.net/html代码:

标题:

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

身体:

<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">
            <ContentTemplate>
                <asp:Panel ID="pnlProdKits" runat="server"></asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>

    </div>

和我的代码隐藏:

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;
update.Triggers.Add(trigger);

然后我将文本框添加到第一个面板:

testpanel.Controls.Add(txtQuantity);

标签将添加到更新面板内的面板中:

pnlProdKits.Controls.Add(priceTotal);

1 个答案:

答案 0 :(得分:0)

如上所述,

您可以尝试以下脚本

<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));

}

</script>

添加onkeyup/onkeydown的其余代码将是相同的。

您可以尝试在alert(1);中添加function来检查它是否会点击它。

相关问题