how to use javascript alert in C# web forms?
im using this:

Button.attributes.Add("onClick","javascript:alert('data saved successfully')");

but its not working.
i searched for it on internet but im getting tutorials etc for windows forms.

make sure "Button" is the name of your button that you are adding the attribute to.

Example:

<asp:Button id="btnSubmit" .... runat="server" />

btnSubmit.attributes.Add("onClick","javascript:alert('data saved successfully')");

ive used the correct button id. rest of my code including code for javascript alert is working well without any error.but im not getting any alerts.

check to see if it is actually being added. You can do this by viewing the source of your document.

Search for your "id" and see if the onclick attribute is correctly put on the submit button.

Button1.Attributes.Add("onclick", "javascript:alert('data save successfully')"); The above code is working fine in my application, put this code in ur Page Load and see if it works.

Please do the following

In Aspx page write the following

<script language="javascript">
 function MyAlert(var msgStr)
{
  alert(msgStr);
}
</script>

In aspx.cs page Button.attributes.Add("onClick","MyAlert('myMessage');");

how to use javascript alert in C# web forms?
im using this:

Button.attributes.Add("onClick","javascript:alert('data saved successfully')");

but its not working.
i searched for it on internet but im getting tutorials etc for windows forms.

This article has been dead for over six months. Start a new discussion instead.