我有一个小应用程序,如下所示。
在此“物料费率”和“采购订单总计”中为只读字段。我想通过公式(Total = Rate * Qty)计算每个物料数量变化的总数。
我在“物料数量”上进行了编码,TextChanged()
并更改AuotoPostBack
为True
我尝试的代码如下:
protected void txtMQty_TextChanged(object sender, EventArgs e)
{
checkTotal();
}
//I am saving Rate in ViewState["Rate"] and retrieving here.
private void checkTotal()
{
Rate = Convert.ToInt32(ViewState["Rate"]);
txtMQty.Text = string.Empty;
if (Rate > 0 && txtMQty.Text == string.Empty)
{
txtMRate.Text = Rate.ToString();
txtTotal.Text = Rate.ToString();
}
Regex reg = new Regex("[^0-9]+$");
var str = txtMQty.Text.ToString();
str = reg.Replace(str, string.Empty);
if (str.Length > 0)
{
var qty = Convert.ToInt32(txtMQty.Text.ToString());
int total = (Rate* qty);
txtTotal.Text = total.ToString();
}
}
我还使用UpdatePanel避免往返。我的问题是,当我输入Quantity时txtMQty
,TextChaged()
事件应该触发但不触发。没有得到什么问题。
我的.aspx页面如下。
<tr>
<td class="auto-style3">
Material Quantity</td>
<td class="auto-style4">
<asp:TextBox ID="txtMQty" runat="server" Width="87px" AutoPostBack="True" OnTextChanged="txtMQty_TextChanged"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RFVMQty" runat="server" ForeColor="Red" Display="Dynamic" ControlToValidate="txtMQty" ErrorMessage="Please provide Material Quantity" ValidationGroup="CreateVal"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="REVMQty" runat="server" ForeColor="Red" Display="Dynamic" ControlToValidate="txtMQty" ErrorMessage="Please provide proper Quantity in number format" ValidationExpression="^\d+$" ValidationGroup="CreateVal"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<tr>
<td class="auto-style3">Material Rate</td>
<td class="auto-style4">
<asp:UpdatePanel ID="UpdateRate" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMQty" EventName="TextChanged" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtMRate" runat="server" Width="87px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td></td>
</tr>
<tr>
<td>
PO Total
</td>
<td class="auto-style4">
<asp:UpdatePanel ID="UpdateTotal" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMQty" EventName="TextChanged" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtTotal" runat="server" Width="87px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
您需要在导致事件的文本框中启用AutoPostBack。
<asp:TextBox ID="txtMQty" runat="server" AutoPostBack="True"></asp:TextBox>
编辑:
对不起,您还需要分配事件:
<asp:TextBox ID="txtMQty" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句