0

here is my code

ASPX Code:

<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Insert" />

C#:

public void MsgBox(String MessageToDisplay)
    {
        Label lblForMsg = new Label();
        lblForMsg.Text = "<script language='javascript'>window.alert('" + MessageToDisplay + "')</script>";
        Page.Controls.Add(lblForMsg);
    }

protected void Button4_Click(object sender, EventArgs e)
    {
SqlCommand com = new SqlCommand("insert into employe values(@id,@pass)", con);

            SqlParameter obj1 = new SqlParameter("@Id", DbType.StringFixedLength);
            obj1.Value = TextBox4.Text;
            com.Parameters.Add(obj1);

            SqlParameter obj2 = new SqlParameter("@pass", DbType.StringFixedLength);
            obj2.Value = TextBox5.Text;
            com.Parameters.Add(obj2);    

            com.ExecuteNonQuery();

            con.Close();

            MsgBox("Account Created");

            if (Session["regis"] == null)
            {
                Response.Redirect("Profile.aspx");
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
      }

i want that when i click on button4, msgbox show, after that when i click ok on msgbox then it check the condition and response to the page.

i am using visual studio 2010,asp.net/c#

2
  • You call MsgBox but then you redirect to other pages. The user's browser will just go to the other page and never see the message. Commented Apr 4, 2014 at 15:13
  • @JBrooks yes u r right Commented Apr 4, 2014 at 15:17

4 Answers 4

1

To display a javascript alert message you should use RegisterClientScriptBlock

public static void ShowMessageAndRedirect(string message, string lpRedirectPage) 
{                
   string cleanMessage = MessageToDisplay.Replace("'", "\'");                               
   Page page = HttpContext.Current.CurrentHandler as Page; 
   string script = string.Format("alert('{0}');", cleanMessage);
   script += " window.location.href='" + lpRedirectPage+ "';"
   if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) 
   {
       page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
   } 
} 

Similar question here: JavaScript: Alert.Show(message) From ASP.NET Code-behind

 if (Session["regis"] == null)
 {
     ShowMessageAndRedirect("Account Created","Profile.aspx");
 }
 else
 {
    ShowMessageAndRedirect("Account Created","Login.aspx");
  }
Sign up to request clarification or add additional context in comments.

6 Comments

adding ScriptManager.GetCurrent(page).IsInAsyncPostBack check would make this usable for an ajax postback if you RegisterClientScriptBlock on ScriptManager instead of page.ClientScript if true
@Rick S how to call that msgbox
Show("Account Created");
@RickS i am using that but msgbox was not showing and it response on another page
Because you are calling Response.Redirect. I will update my answer to include a client redirect. You need to take out your Response.Redirect code.
|
0

I think if you want to display "Account Created to the User then do the following =

Response.Write("window.alert('" + MessageToDisplay + "');");

Comments

0

In your server-side code, you're attempting to add the message box, but immediately after that, either way your method ends with a Response.Redirect - so you're sending the user off to a new page (even if one of those pages is the same URL as the page you came from).

If you want the message box to show if the process was successful (your "account created" scenario), then whichever page you redirect to needs to include that javascript message box when it renders.

Comments

0

Since you like to use MsgBox.show() like that of windows form. This class will help you (vb.net)

Imports Microsoft.VisualBasic
Public Class MsgBox
Public Shared Sub Show(ByRef page As Page, ByVal strMsg As String)
    Try
        Dim lbl As New Label
        lbl.Text = "<script language='javascript'>" & Environment.NewLine _
                    & "window.alert(" & "'" & strMsg & "'" & ")</script>"

        page.Controls.Add(lbl)
    Catch ex As Exception

    End Try
End Sub 
End Class

From within aspx.page, it can be called like this

 Msgbox.show(Me,"Alert something")    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.