【发布时间】:2014-05-24 11:55:53
【问题描述】:
我有一个如下的小应用程序。
在此材料费率和采购订单总计是只读字段。我想通过公式(Total=Rate*Qty)根据材料数量的变化来计算总数。
我在 Material Quantity 的 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's 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>
【问题讨论】:
-
在更新面板中使用触发器并为 event=TextChanged 触发
-
为什么要使用updatePanel?为什么要把 txtMQty 设为空?
-
@KrunalPatil 我使用了触发器。
-
@AbdurRahim 我使用了 updatepanel,因为我不希望每次文本更改都往返于服务器。
-
检查我的新答案。我希望这能解决您的问题。
标签: c# asp.net updatepanel autopostback