繁体   English   中英

ASP.NET中的Javascript警报框

[英]Javascript alert box in asp.net

我正在使用JavaScript

(ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Product Added Successfully!')", true);)

在代码进入if / else块的一部分后显示警告框。 但是,当我向aspx页面添加脚本管理器和更新面板时,javascript功能停止工作。 因此,我使用了以下语句:

Response.Write("<script>alert('Please Upload Photo');</script>");

即使哪一个都行不通! 是否有人对这个问题有想法或解决方案。 以下是.cs文件中的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if(uploadProductPhoto.HasFile)
            {
                int x = SaveProductPhoto();
                if (x == 0)
                {
                    ShoppingCart k = new ShoppingCart
                    {
                        ProductName = txtProductName.Text,
                        ProductPrice = txtProductPrice.Text,
                        ProductImage = "~/ProductImages/" + uploadProductPhoto.FileName,
                        ProductDescription = txtProductDescription.Text,
                        CategoryID = Convert.ToInt32(ddlCategory.SelectedValue),
                        TotalProducts = Convert.ToInt32(txtProductQuantity.Text)
                    };

                    k.AddNewProduct();
                }
                ClearText();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Product Added Successfully!')", true);
                //Response.Redirect("~/Admin/AddNewProducts.aspx?alert=success");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Upload Product Photo!')", true);
                //Response.Write("<script>alert('Please Upload Photo');</script>");
            }

        }

 private int SaveProductPhoto()
        {
            if(uploadProductPhoto.PostedFile==null)
            {
                //Response.Write("<script>alert('Please upload photo');</script>");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please Upload Product Photo!')", true);
                return 1;
            }
                string filename = uploadProductPhoto.PostedFile.FileName.ToString();
                string fileExt = System.IO.Path.GetExtension(uploadProductPhoto.FileName);

                if(filename.Length>100)
                {
                    //Response.Write("<script>alert('File name should not exceed 100 characters');</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('File name should not exceed 100 characters')", true);
                    return 1;
                }
                else if(fileExt!=".jpg" && fileExt!=".jpeg" && fileExt!=".bmp" && fileExt!=".png")
                {
                    //Response.Write("<script>alert('Image file should be in .jpg, .jpeg, .png, .bmp format!');</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Image file should be in .jpg, .jpeg, .png, .bmp format!')", true);
                    return 1;
                }
                else if(uploadProductPhoto.PostedFile.ContentLength>4000000)
                {
                    //Response.Write("<script>alert('File size should not be greater than 4 mb');</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('File size should not be greater than 4 mb')", true);
                    return 1;
                }
                else
                {
                    uploadProductPhoto.SaveAs(Server.MapPath("~/ProductImages/" + filename));
                    //Response.Write("<script>alert('Product added successfully!');</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Product added successfully!')", true);
                    return 0;
                }
        }

ScriptManager.RegisterStartupScript(this,this.GetType(),“ alert”,“ alert(”产品添加成功!“);”,true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM