简体   繁体   中英

How to use Message Box in asp.net

I am trying to show a message box to the user where they can select either YES or NO from the message box; if they select Yes i want to call some function and if they select NO then i want to call another function. That is not what i have now in my my code please help. thanks Here is my code:

 protected void TEST()
    {
        string ID= ddlPr.SelectedValue;
        string PRACTICE_TYPE = DDL_TYPE.SelectedValue;

        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT count(*) from [DA2].[QPMS].[QPMS_RESPONSE] WHERE ID=@ID";
                cmd.Parameters.AddWithValue("@ID", ID);             
                con.Open();
                int result = (int)cmd.ExecuteScalar();
                if (result >=1)

                {
                    //if the user selects YES i want to call some function if user             selects No then i want to call another function.
                    string myscript = "alert ('message');";
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myscript, true);                    

                }
                con.Close();
            }
        }

    }
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Button ID="Button3" runat="server" Style="position: static" Text="Button" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" CausesValidation="False" OnClick="Button2_Click" Style="position: static; display: none" Text="Ok" />
    <asp:Button ID="Button5" runat="server" CausesValidation="False" OnClick="Button5_Click" Style="position: static; display: none" Text="Cancel" />

    <script type="text/javascript">
            function confirmProcess()
            {
                if (confirm('are you sure to continue?'))
                {
                   //if you are missing this, always use ClientID to get reference to button //control when used with master pages
                    document.getElementById('<%=Button2.ClientID%>').click();
                }
                else
                {
                    document.getElementById('<%=Button5.ClientID%>').click();
                }
                
    
            }
        </script>
    </asp:Content>

//code-behind

protected void Button5_Click(object sender, EventArgs e)
{
    Response.Write("Clicked Canceled");
}


protected void Button2_Click(object sender, EventArgs e)
{
    Response.Write("Clicked OK");
}


protected void Button1_Click(object sender, EventArgs e)
{
    String csname = "PopupScript";
    Type cstype = this.GetType();
    ClientScriptManager cs = Page.ClientScript;
    if (!cs.IsStartupScriptRegistered(cstype, csname))
    {
        String cstext = "confirmProcess();";
        cs.RegisterStartupScript(cstype, csname, cstext, true);
    }
}

ASP.Net Message Box Alert Display

I use this currently in all of my Web Applications I created a Static Method that I can call one of the 2 overloaded Methods this works 100% I just tested to confirm

    public static void ShowClientMessageDlg(string msg, string aValue, string redirect = "")
    {
        string msgFormat;
        msgFormat = string.Format(" {0}", aValue);
        msg = msg + msgFormat;
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
    }

    /// <summary>
    /// Joins a String of Values to create a Message Box
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="aValue"></param>
    /// <param name="redirect"></param>
    public static void ShowClientMessageDlg(string msg, List<string> aValue, string redirect = "")
    {
        string msgFormat;
        msgFormat = string.Format(" {0}", string.Join<string>(", ", aValue.ToArray()));
        msg = msg + msgFormat;
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
    }

As stated by Rahul in the comments, a messagebox is only available in winforms. Also, the javascript alert will only display a message. You can't execute different code blocks based on the end user response. What you want is a confirm dialog box. There's a simple example here.

From link:

<input type=button value="Try it now" 
onClick="if(confirm('Format the hard disk?'))
alert('You are very brave!');
else alert('A wise decision!')">

For asp, just create a different function to call rather than the alert.

If you wanted an alternative to javascript you could also make use of the AjaxToolkit ConfirmButton Extender . It's based on a button click so you would have to "cheat" a little to use it. Call the MyButton.Click() method to activate the confirm dialog. You probably could hide the button by setting Display: none; .

Overall, I think the javascript is still probably your best option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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