469,890 Members | 2,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

button.Attributes.Add does not work after I added Validation control on the page

I use

btnSave.Attributes.Add("onclick", "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.RequiredFieldValidator
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.DebuggerStepThrough()> 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("onclick", "ShowMessage()")

End Sub

End Class

Here is the HTML of the ASPX page:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb"
Inherits="AspxTest2.WebForm1"%>
<!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_defaultClientScript"
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:TextBox>
<asp:RequiredFieldValidator
id="RequiredFieldValidator1" style="Z-INDEX: 104; LEFT:
279px; POSITION: absolute; TOP: 71px" runat="server"
ErrorMessage="Text is required"
ControlToValidate="TextBox1">Please enter something into
the text box.</asp:RequiredFieldValidator>
</form>
</body>
</HTML>

Nov 20 '05 #1
1 3637
Hi Bruce,

This is only throwing out an idea.

Is it possible to <get> the btnSave's onclick Attribute?
If so, you could add on your "ShowMessage()" and put it back.

Dim Index As Integer
Index = [...get the index of the "onclick" Attribute...]
btnSave.Attributes (Index) &= "; ShowMessage()" 'Note the ';'.

If this fails with a casting error, you may need:
sOnClick As String = DirectCast (btnSave.Attributes (Index), String)
btnSave.Attributes (Index) = sOnClick & "; ShowMessage()" 'Note the ';'.

Regards,
Fergus
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tim Meagher | last post: by
1 post views Thread by Matt Howeson | last post: by
3 posts views Thread by Jeff | last post: by
4 posts views Thread by Elliot M. Rodriguez | last post: by
7 posts views Thread by IntraRELY | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.