如何使用javascript和asp.net放置警告框?

时间:2017-08-23 12:47:25

标签: javascript c# asp.net

我只是想知道如何使用javascript和asp.net放置一个警告框。基本上我已经创建了一个网页,管理员将填写一个表单,然后在单击提交按钮后,应该会弹出一个警告框,其中会显示一条消息,说明上传成功或上传失败。

我尝试使用此代码(我只包含了javascript代码和asp提交按钮):

<script>
    function UploadStatusSuccessful()
    {
        alert("Upload Successful!");
    }
</script>

<script>
    function UploadStatusFail()
    {
        alert("Upload Failed!");
    }
</script>

<asp:Button ID="Button1" runat="server" CssClass="submitButton" Text="Save 
Item" OnClick="Button1_Click"/>

这是代码隐藏的代码:

protected void Button1_Click(object sender, EventArgs e)
{

    try
    {
        int item_brandId = 
        ConnectionClassBrands.GetIdByBrand(itemBrand.Text);
        string item_model = itemModel.Text;
        double item_price = Convert.ToDouble(itemPrice.Text);
        string item_image1 = Session["PicturePath1"].ToString();
        string item_image2 = Session["PicturePath2"].ToString();
        string item_description = itemDescription.Text;
        string item_necktype = itemNeckType.Text;
        string item_body = itemBody.Text;
        string item_fretboard = itemFretboard.Text;
        string item_fret = itemFret.Text;
        string item_bridge = itemBridge.Text;
        string item_neckpickup = itemNeckPickup.Text;
        string item_bridgepickup = itemBridgePickup.Text;
        string item_hardwarecolor = itemHardwareColor.Text;

        if (itemType1.Checked)
        {
            int item_type = 
            ConnectionClassBrands.GetIdByType(itemType1.Text);
            ItemType = item_type;
        }
        else if (itemType2.Checked)
        {
            int item_type = 
            ConnectionClassBrands.GetIdByType(itemType2.Text);
            ItemType = item_type;
        }

        var item = new instrumentItem
        {
            typeId = ItemType,
            brandId = item_brandId,
            model = item_model,
            price = item_price,
            itemimage1 = item_image1,
            itemimage2 = item_image2,
            description = item_description,
            necktype = item_necktype,
            body = item_body,
            fretboard = item_fretboard,
            fret = item_fret,
            bridge = item_bridge,
            neckpickup = item_neckpickup,
            bridgepickup = item_bridgepickup,
            hardwarecolor = item_hardwarecolor
        };

        ConnectionClassGuitarItems.AddStringInstrumentItems(item);

        Button1.Attributes.Add("onclick", "JavaScript:return 
        UploadStatusSuccessful()");

        ClearTextFields2();

    }
    catch (Exception)
    {
        Button1.Attributes.Add("onclick", "JavaScript:return 
        UploadStatusFail()");
    }


}

我尝试使用Button1.Attrbute.Add,但没有显示警告框。请提供有关如何实现此目标的解决方案或实际示例。如果我错过了错误的语法,请通知我,以便我可以立即纠正。

2 个答案:

答案 0 :(得分:0)

您可以在后面的代码中使用ScriptManager.RegisterStartupScript来显示如下所示的提醒消息。您需要Scriptmanager控制您的aspx页面。这是推荐的方式

 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), 
          "err_msg", 
          "alert('Upload failed!');",
          true);

否则你可以简单地做到如下

Response.Write("<script>alert('Upload failed!');</script>");

scriptmanager控件的链接

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

答案 1 :(得分:0)

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>UploadStatusSuccessful();</script>", false);