1

I want to use Javascript Alert function in my ASP.NET page.

For example like this;

Response.Write("<script language=javascript>alert('ERROR');</script>);

But, this doesn't work.

I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);

But i don't want use it because it's working with PostBack

How can i do that without PostBack?

EDIT: For example for using;

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}
7
  • what are you really trying to do? Client validation? there are much better methods. Server validation? you normally send the error message back to your view / aspx page and show it Commented Apr 19, 2011 at 7:31
  • What do you mean by "without postback"? What should trigger the alert box? Commented Apr 19, 2011 at 7:33
  • Server validation. I want to use it with my database connection try-catch blocke.. Commented Apr 19, 2011 at 7:34
  • But if you want to do server validation, you need to run code on the server, right? That means you have to transfer control back to the server, i.e. you need either a postback (which you apparently don't want to use) or AJAX. Do you use AJAX? Commented Apr 19, 2011 at 7:36
  • @Heinzi I update my question. Please look at. Commented Apr 19, 2011 at 7:49

4 Answers 4

3

See a note from MSDN:

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

So, I think ClientScriptManager.RegisterStartupScript method is what you need:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    " ", 
    @"<script language=javascript>alert('ERROR');</script>", 
    true
);
Sign up to request clarification or add additional context in comments.

Comments

1

In your code you forgot the quotation mark. I just tried it in a sample page like this:

Response.Write("<script language=javascript>alert('ERROR');</script>");

and it worked. Where did you place the Response.Write in your code? Could you give some more details? What do you want to do?

Comments

0

For displaying an alert message to the User, in a webpage i have a code have a look at this

public void UserMsgBox(string sMsg)
{
StringBuilder sb = new StringBuilder();
System.Web.UI.Control oFormObject = null;
sMsg = sMsg.Replace("'", "\\'");
sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
sb = new StringBuilder();
sb.Append(sMsg);
foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
    oFormObject = oFormObject_loopVariable;
    if (oFormObject is HtmlForm) {
        break; // TODO: might not be correct. Was : Exit For
    }
}
oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
}

Comments

0

try using RegisterStartupscript for registering the script. Refer : http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

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.