我在我的文本框中声明了一个jquery:
<asp:TextBox ID="txtProductPrice" runat="server" onkeyup ="javascript:this.value=Comma(this.value);" class="form-control" MaxLength ="6"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" Display="Dynamic" runat="server"
ErrorMessage="Product Price is required." ControlToValidate="txtProductPrice" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server"
ErrorMessage="Enter a valid price" Display="Dynamic" ControlToValidate="txtProductPrice"
ForeColor="Red" ValidationExpression=^[,0-9]*$></asp:RegularExpressionValidator>
这是jquery:
<script type="text/javascript" language="javascript">
function Comma(Num) {
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>
以下代码无效。实际上它显示了产品添加的标签,但实际上没有添加。我在这里失踪了什么?
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (uploadProductPhoto.PostedFile != null)
{
SaveProductPhoto();
ShoppingCart k = new ShoppingCart()
{
ProductName = txtProductName.Text,
ProductImage = "~/ProductImages/" + uploadProductPhoto.FileName,
ProductPrice = txtProductPrice.Text,
ProductDescription = txtProductDescription.Text,
CategoryID = Convert.ToInt32(ddlCategory.SelectedValue),
TotalProducts = Convert.ToInt32(txtProductQuantity.Text)
};
k.AddNewProduct();
ClearText();
Label2.Text = "Product Added!";
//Response.Redirect("AddNewProduct.aspx?alert=success");
}
else
{
Response.Write("<script>alert('Please upload photo');</script>");
}
}
当我删除我的asptextbox中的onkeyup代码时,代码工作正常。产品被添加到DB。
db中的产品是varchar。
这是我的存储过程:
ALTER PROCEDURE [dbo].[SP_AddNewProduct]
(
@ProductName varchar(300),
@ProductPrice varchar(500),
@ProductImage varchar(500),
@ProductDescription varchar(1000),
@CategoryID int,
@ProductQuantity int
)
AS
BEGIN
BEGIN TRY
Insert into products
values
(@ProductName,
@ProductDescription,
@ProductPrice,
@ProductImage,
@CategoryID,
@ProductQuantity
)
END TRY
BEGIN CATCH
PRINT ('Error Occured')
END CATCH
END