Skip to content Skip to sidebar Skip to footer

How Can I Display A Messagebox In Asp.net?

I want to show a message box on the successful save of any item. I googled it and tried different solutions, but none of them worked. Here is the code I am using: try { con.Ope

Solution 1:

@freelancer If you are using ScriptManager then try this code for message..

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, GetType(), 
                      "ServerControlScript", script, true);

Solution 2:

Make a method of MsgBox in your page.


publicvoidMsgBox(String ex, Page pg,Object obj) 
{
    strings="<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Typecstype= obj.GetType();
    ClientScriptManagercs= pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
}

and when you want to use msgbox just put this line

MsgBox("! your message !", this.Page, this);

Solution 3:

just try this, it works fine in my browser:

your response writing code should be

Response.Write("<script>alert('login successful');</script>");

Hope this works

Solution 4:

you can use clientscript. MSDN : Clientscript

StringscriptText="alert('sdsd');";
    ClientScript.RegisterOnSubmitStatement(this.GetType(), 
        "ConfirmSubmit", scriptText);

try this

ClientScript.RegisterStartupScript(this.GetType(), "JSScript", scriptText); 



ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", scriptText); //use this 

Solution 5:

This code will help you add a MsgBox in your asp.net file. You can change the function definition to your requirements. Hope this helps!

protectedvoidAddstaff_Click(object sender, EventArgs e)    
    {   
 if (intClassCapcity < intCurrentstaffNumber)     
                {                  
            MsgBox("Record cannot be added because max seats available for the " + (string)Session["course_name"] + " training has been reached");    
        }    
else    
    {   
            sqlClassList.Insert();    
    }    
}

privatevoidMsgBox(string sMessage)    
    {    
        string msg = "<script language=\"javascript\">";    
        msg += "alert('" + sMessage + "');";    
        msg += "</script>";    
        Response.Write(msg);    
    }

Post a Comment for "How Can I Display A Messagebox In Asp.net?"