2013-12-08 59 views
0

我從代碼隱藏中生成一些標籤,文本框等,我需要更新標籤時,在文本框中按下一個鍵,我一直在看這個: How do I make a Textbox Postback on KeyUp? 並試圖將其轉換爲動態 - 但我無法得到這個工作。asp.net textbox - 更新標籤onkeyup

這是我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); 
+0

你不能使用'javascript'或'J-Query'嗎? – Bharadwaj

+0

好吧,如果我知道如何去做,它應該是可能的。:-) – Mathias

+0

如果你想在'TextBox'上回發,你可以試試這個代碼,'txtQuantity.AutoPostBack = true;' – Bharadwaj

回答

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的代碼將相同。

您可以嘗試在function中加入alert(1);來檢查它是否符合要求。