I have a 3 textboxes on a page quantity, price and total . When the quantity and price are entered by the user the total is being calculated direct开发者_Go百科ly by using javascript. Below is the code:
function quantity(textvalue) {
var qty = textvalue.value;
var price = document.getElementById('<%=Price.ClientID %>').value;
var Total = Math.round(qty * price * 100) / 100;
document.getElementById('<%=Total.ClientID %>').value = Total;
}
function Price(textvalue) {
var price = textvalue.value;
var qty = document.getElementById('<%=Quantity.ClientID %>').value;
var Total = Math.round(qty * price * 100) / 100;
document.getElementById('<%=Total.ClientID %>').value = Total;
}
<asp:TextBox ID="Quantity" runat="server" onchange="javascript: quantity(this);" CausesValidation="True"></asp:TextBox>
<asp:TextBox ID="Price" runat="server" onchange="javascript: Price(this);" ></asp:TextBox>
<asp:TextBox ID="txtLineItemTotal" runat="server" Width="120px" MaxLength="14" ReadOnly="true"
BackColor="Silver" BorderWidth="2px" style="font-weight: 700">0.00</asp:TextBox>
I also need to validate the value calculated in the total textbox and it should not exceed 100000000000.00.
I am using the below function to achieve this
function TotalChanged() {
var lineitemtotal = document.getElementById('<%=Total.ClientID %>').value;
alert("entered function");
if (lineitemtotal >= 100000000000.00) {
alert("Total cannot exceed 100000000000.00,please re-enter");
}
else
return false;
}
And on Page Load
Total.Attributes.Add("onchange", "{return TotalChanged()};");
But it is not entering the function, please let me know what am I missing...or if I am using the wrong event...trying it for the past 1 day....:(
You shouldn't need the javascript:
in the onchange.
<asp:TextBox ID="Quantity" runat="server" onchange="javascript: quantity(this);" CausesValidation="True"></asp:TextBox>
will render to
<input type="text" id="Quality" name="Quality" onchange="javascript: quantity(this);"/>
Simon
For starters, if you're using ASP.NET 4.0, you'll want to use ScriptManager or ClientScriptManager's .RegisterExpandoAttribute method as demonstrated below using ClientScriptManager:
Page.ClientScript.RegisterExpandoAttribute(controlId, attributeName, attributeValue, false);
The final boolean argument is for the "encode" parameter. You must set this to false. As a surprise new feature in ASP.NET 4, all of your attribute values are HTML Encoded. Great for security, but really bad for event handlers / scripts emitted from the server side.
UPDATE:
In your specific case, the RegisterExpandoAttribute would be implemented as:
Page.ClientScript.RegisterExpandoAttribute(Total.UniqueID, "onchange", "return TotalChanged();", false);
Note the absence of the curly brackets encapsulating my handler. Those curly brackets might be causing your problem if you're not on ASP.NET 4 but your method still isn't being called. You're effectively wrapping your event handler in an object literal.
Good luck. If this doesn't fix your problem provide some more info and I'll try to offer other ideas.
B
精彩评论