开发者

Javascript Alert in ASP.NET

开发者 https://www.devze.com 2023-02-27 03:24 出处:网络
I want to use Javascript Alert function in my ASP.NET page. For example like this; Response.Write(\"<script language=javascript>alert(\'ERROR\');</script>);

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", muste开发者_运维知识库riadi);
    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>");
}


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
);


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?


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()));
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号