简体   繁体   English

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

[英]How to put an alert box using javascript and asp.net?

I just want to know how to put an alert box using javascript and asp.net. 我只想知道如何使用javascript和asp.net放置警报框。 Basically I have created a webpage where the admin will fill up a form, and then after clicking the submit button, there should be an alert box that will popup with a message that says upload successful or upload failed. 基本上,我创建了一个网页,管理员将在其中填写一张表格,然后单击“提交”按钮后,应该有一个警告框,该对话框将弹出一条消息,提示上传成功或上传失败。

I tried doing it with this code(I only included the javascript code and the asp submit button): 我尝试使用此代码(我只包括了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"/>

and here is the code-behind: 下面是代码:

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()");
    }


}

I tried doing the Button1.Attrbute.Add but no alert box is showing. 我尝试做Button1.Attrbute.Add,但没有显示警告框。 Please provide solutions or actual examples on how to achieve this. 请提供解决方案或实际示例,以了解如何实现这一目标。 Also kindly inform me if there are wrong syntax that i missed so that i can correct it immediately. 如果我错过了错误的语法,也请通知我,以便我可以立即更正。

You can use ScriptManager.RegisterStartupScript in code behind to show alert message like below . 您可以在后面的代码中使用ScriptManager.RegisterStartupScript来显示如下警告消息。 You need to Scriptmanager control to your aspx page . 您需要Scriptmanager控件来访问aspx页面。 This is the recommended way 这是推荐的方法

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

Otherwise you can simple do like below 否则,您可以像下面这样简单地做

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

Link for scriptmanager control 脚本管理器控件的链接

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

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

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

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