BruceCheng
asked on
button.Attributes.Add does not work after I added Validation control on the page
I use
btnSave.Attributes.Add("on click", "ShowMessage()")
to link my web control button to a JavaScript function. It works well until I added a Validation control into the page. After that, even the all of the validations passed, my ShowMessage() still does not get triggered. When checking the view source carefully, I found that the btnSave button have 2 onClick events defined in the tag. In this case, only the 1st onClick event (the validation one) got triggered.
Does anyone know how to fix this problem and let me be able to run ShowMessage() JavaScript function?
Thanks a lot.
Here is the VB code behind:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents btnSave As System.Web.UI.WebControls. Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls. TextBox
Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls. RequiredFi eldValidat or
Protected WithEvents Label1 As System.Web.UI.WebControls. Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
btnSave.Attributes.Add("on click", "ShowMessage()")
End Sub
End Class
Here is the HTML of the ASPX page:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx. vb" Inherits="AspxTest2.WebFor m1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScri pt" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function ShowMessage()
{
alert("test message");
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 51px; POSITION: absolute; TOP: 37px" runat="server" Width="236px" Height="16px">Simple Test</asp:Label>
<asp:Button id="btnSave" style="Z-INDEX: 102; LEFT: 59px; POSITION: absolute; TOP: 110px" runat="server" Text="Save"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 59px; POSITION: absolute; TOP: 70px" runat="server" Width="181px" Height="20px"></asp:TextBo x>
<asp:RequiredFieldValidato r id="RequiredFieldValidator 1" style="Z-INDEX: 104; LEFT: 279px; POSITION: absolute; TOP: 71px" runat="server" ErrorMessage="Text is required" ControlToValidate="TextBox 1">Please enter something into the text box.</asp:RequiredFieldVal idator>
</form>
</body>
</HTML>
btnSave.Attributes.Add("on
to link my web control button to a JavaScript function. It works well until I added a Validation control into the page. After that, even the all of the validations passed, my ShowMessage() still does not get triggered. When checking the view source carefully, I found that the btnSave button have 2 onClick events defined in the tag. In this case, only the 1st onClick event (the validation one) got triggered.
Does anyone know how to fix this problem and let me be able to run ShowMessage() JavaScript function?
Thanks a lot.
Here is the VB code behind:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents btnSave As System.Web.UI.WebControls.
Protected WithEvents TextBox1 As System.Web.UI.WebControls.
Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.
Protected WithEvents Label1 As System.Web.UI.WebControls.
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
btnSave.Attributes.Add("on
End Sub
End Class
Here is the HTML of the ASPX page:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScri
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function ShowMessage()
{
alert("test message");
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 51px; POSITION: absolute; TOP: 37px" runat="server" Width="236px" Height="16px">Simple Test</asp:Label>
<asp:Button id="btnSave" style="Z-INDEX: 102; LEFT: 59px; POSITION: absolute; TOP: 110px" runat="server" Text="Save"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 59px; POSITION: absolute; TOP: 70px" runat="server" Width="181px" Height="20px"></asp:TextBo
<asp:RequiredFieldValidato
</form>
</body>
</HTML>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
return isValid;