Monday, June 09, 2008

Message Boxes, Popup in ASP.NET

Displaying Message Boxes, Popup in ASP.NET using Java Script

The following code samples can be used to display message boxes in ASP.NET webpages.

mes = chpwd.updat(txtrpwd.Text, txtuname.Text);
if (mes)
{

string script = "alert('New Password is Created');\n";
script += "var f='" + Session["flag"].ToString() + "';\n";
script += "if (f == 'L')\n";
script += "location.href='loginpage.aspx';\n";

Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", script, true);
}

----------------------------------------------------------------------------------------
Response.Write("<script>alert('New Password is created')</script>");
----------------------------------------------------------------------------------------
if (mes)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script type='text/javascript'>");
sb.Append("alert('New Password is Create');");

if (Session["flag"].ToString() == "L")
{
sb.Append("window.location = 'loginpage.aspx';");
}

sb.Append("");

Page.RegisterClientScriptBlock("mes", sb.ToString());
}
----------------------------------------------------------------------------------------

Page.ClientScript.RegisterStartupScript(this.GetType(), "myAlertBox", "alert('New Password is Created'); window.location = 'loginpage.aspx';", true);

or

ClientScript.RegisterClientScriptBlock(this.GetType(), "myAlertBox", "alert('New Password is Created'); window.location = 'loginpage.aspx';", true);

----------------------------------------------------------------------------------------

For POP Ups:

We can use Javascript function window.Open or target="_blank" for popups.

If we want to open a popup in form another popup window? we can use Java script function window.location.

------------------------------------------------------------------------

Another Method to Display Message boxes in ASP.NET

protected void btnSubmit_Click(object sender, EventArgs e)

{

string script = "alert('Are U sure? ');";

System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Test", script, true);

}

------------------------------------------------------------------------

Another Method to Display Message boxes in ASP.NET

<asp:Button ID="button1" OnClientClick="return confirm('Are you sure?');" runat="server" />

or

<asp:LinkButton
ID="ibtnDelete"
runat="server"
Text="Delete"
CommandName="Delete"
OnClientClick="javascript:return confirm('Are you sure you want to delete this class?');">
</asp:LinkButton>

No comments: