我有一个小应用程序如下。
此物料费率和采购订单总额是只读字段。我想按公式计算总材料数量的总和(总计=费率*数量)。
我在材料数量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来避免往返。我的问题是,当我输入数量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>
答案 0 :(得分:1)
这是你的问题:
您正在使用多个更新面板。并尝试触发不在updatepanel中的文本框。触发器在更新面板中找到控件,而不是提供回发。但是您的 txtMQty 不在更新面板中。所以触发器不起作用。
如果您使用单个更新面板,您的问题将得到解决。请检查下面给出的代码。这对你有用。
<asp:updatepanel id="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMQty" EventName="TextChanged" />
</Triggers>
<ContentTemplate>
Material Quantity
<asp:TextBox ID="TextBox1" runat="server" Width="87px" AutoPostBack="True" OnTextChanged="txtMQty_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="Red" Display="Dynamic" ControlToValidate="txtMQty" ErrorMessage="Please provide Material Quantity" ValidationGroup="CreateVal"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ForeColor="Red" Display="Dynamic" ControlToValidate="txtMQty" ErrorMessage="Please provide proper Quantity in number format" ValidationExpression="^\d+$" ValidationGroup="CreateVal"></asp:RegularExpressionValidator>
<br />
<br />
Material Rate
<asp:TextBox ID="TextBox2" runat="server" Width="87px" ReadOnly="True" >5</asp:TextBox>
<br />
<br />
PO Total
<asp:TextBox ID="TextBox3" runat="server" Width="87px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:updatepanel>
这是一位有效的HTML设计师。
答案 1 :(得分:0)
您需要在导致事件的文本框上启用AutoPostBack。
<asp:TextBox ID="txtMQty" runat="server" AutoPostBack="True"></asp:TextBox>
编辑:
对不起您还需要分配事件:
<asp:TextBox ID="txtMQty" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>