Custom Control

R

rn5a

I have created a custom control button which when clicked displays a
message in the JavaScript alert dialog. I could successfully compile
the VB class file into a DLL & also could add it to the Toolbox in
Visual Web Developer 2005 Express Edition but the alert message isn't
popping up when I am implementing this control in an ASP.NET page &
clicking the Button in the ASPX page. This is the class file:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace MessageBox
Public Class ShowMsgBox : Inherits Button
Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Dim jsScript As String
jsScript = alert("Some Message Here")

Output.AddAttribute(HtmlTextWriterAttribute.OnClick,
jsScript)
End Sub
End Class
End Namespace

This is the second way I tried it but this doesn't work as well

Namespace MessageBox
Public Class ShowMsgBox : Inherits Button
Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Output.AddAttribute(HtmlTextWriterAttribute.OnClick,
"ShowMsg()")
End Sub

Protected Overrides Sub RenderContents(ByVal Output As
HtmlTextWriter)
Output.Write("<script language='JavaScript'>")
Output.Write(vbCrLf)
Output.Write("function ShowMsg(){")
Output.Write(vbCrLf)
Output.Write("alert('Some Message Here')")
Output.Write(vbCrLf)
Output.Write("}")
Output.Write(vbCrLf)
Output.Write("</script>")
End Sub
End Class
End Namespace

When I have a look at the source code, I find that the source code of
the button has 2 'onclick' events i.e. the button code looks like this:

<input type="submit" name="ShowMsgBox1" value="CLICK"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;ShowMsgBox1&quot;, &quot;&quot;, true,
&quot;&quot;, &quot;&quot;, false, false))" id="ShowMsgBox1"
onclick="ShowMsg()" />

Where is the first 'onclick' event coming from? I guess that's the
cause of the problem.

Can someone please help me resolve this issue?
 
R

rn5a

After shooting a lot of arrows in the dark, I could finally nab the
culprit but still have been unable to resolve the issue.

Actually the ASPX page that uses the Button custom control also makes
use of a user control which encapsulates a few TextBoxes. All the
TextBoxes are accompanied by validation controls & the presence of
these validation controls is causing the Button source code to have 2
'onclick' events which, in turn, is preventing the JavaScript function
named 'ShowMsgBox' from getting invoked when this Button custom control
is clicked. If I get rid of the validation controls, then the source
code of the Button custom control changes to the more conventional one
with just one 'onclick' event:

<input type="submit" name="ShowMsgBox1" value="CLICK" id="ShowMsgBox1"
onclick="ShowMsg()"/>


Now, the JavaScript alert message pops-up when Button custom control is
clicked.

The problem is I just can't do without the validation controls for the
TextBoxes in the user control but at the same time, they are preventing
the JavaScript alert message from popping-up.

Any workaround to this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Button & TextBox Custom Control 1
Suppress Form Action 1
Custom WebControl with OnClick event 3
AddAttribute 1
Drawing in Custom Controls 3
A composite control 2
Custom Page Control 4
data binding problem 1

Top