473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

AddAttribute

Assume that a custom server control derived from
System.Web.UI.WebControls.WebControl encapsulates a Button & a TextBox.
The Button & the TextBox controls are added using the
CreateChildControls sub like this:

Namespace MyNS
Public Class MyClass : Inherits WebControl
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Dim txt1 As TextBox
Dim btn1 As Button

txt1 = New TextBox
btn1 = New Button

Me.Controls.Add(New LiteralControl("<br>"))

txt1.ID = "txt1"
txt1.EnableViewState = True
Me.Controls.Add(txt1)
btn1.ID = "btn1"
btn1.Text = "CLICK ME"
Me.Controls.Add(btn1)

ChildControlsCreated = True
End Sub
End Class
End Namespace

Now I want to add a OnClick event to the Button control which should
invoke a JavaScript function. This is what I tried:

Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Output.AddAttribute(HtmlTextWriterAttribute.OnClic k, "CallSub1()")
End Sub

When I had a look at the HTML source of the ASPX page which uses the
above custom control, I found that the HTML source corresponding to the
AddAttributesToRender sub was:

<span id="sc1" onclick="CallSub1()"><br>
<input name="txt1" type="text" id="txt1" />
<input type="submit" name="btn1" value="CLICK ME" id="btn1" />
</span>

The OnClick event got associated with the <spanelement rather than
with the Button. As a result, the JavaScript function gets invoked not
only when the Button is clicked but also when the TextBox is clicked.

How do I add the OnClick event ONLY to the Button so that the
JavaScript function gets invoked when the Button is clicked? Nothing
should happen if a user clicks any part of the TextBox (of course,
other than setting the focus on the TextBox).

Nov 2 '06 #1
1 1327
if you inherit frm webcontrol, then you are a span. attributes will be
applied to the span. to add a clcient click t the button, add the onclick
attribute to the button.

bt1.Attributes.Add("onclick","doclick();");

-- bruce (sqlwork.com)

<rn**@rediffmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
Assume that a custom server control derived from
System.Web.UI.WebControls.WebControl encapsulates a Button & a TextBox.
The Button & the TextBox controls are added using the
CreateChildControls sub like this:

Namespace MyNS
Public Class MyClass : Inherits WebControl
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Dim txt1 As TextBox
Dim btn1 As Button

txt1 = New TextBox
btn1 = New Button

Me.Controls.Add(New LiteralControl("<br>"))

txt1.ID = "txt1"
txt1.EnableViewState = True
Me.Controls.Add(txt1)
btn1.ID = "btn1"
btn1.Text = "CLICK ME"
Me.Controls.Add(btn1)

ChildControlsCreated = True
End Sub
End Class
End Namespace

Now I want to add a OnClick event to the Button control which should
invoke a JavaScript function. This is what I tried:

Protected Overrides Sub AddAttributesToRender(ByVal Output As
HtmlTextWriter)
MyBase.AddAttributesToRender(Output)
Output.AddAttribute(HtmlTextWriterAttribute.OnClic k, "CallSub1()")
End Sub

When I had a look at the HTML source of the ASPX page which uses the
above custom control, I found that the HTML source corresponding to the
AddAttributesToRender sub was:

<span id="sc1" onclick="CallSub1()"><br>
<input name="txt1" type="text" id="txt1" />
<input type="submit" name="btn1" value="CLICK ME" id="btn1" />
</span>

The OnClick event got associated with the <spanelement rather than
with the Button. As a result, the JavaScript function gets invoked not
only when the Button is clicked but also when the TextBox is clicked.

How do I add the OnClick event ONLY to the Button so that the
JavaScript function gets invoked when the Button is clicked? Nothing
should happen if a user clicks any part of the TextBox (of course,
other than setting the focus on the TextBox).

Nov 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that...
1
by: Dan Nash | last post by:
Hi guys Just developing a cool list that pulls data from an SQL database. I've got a nice header, a footer, borders down the side, but no top/bottom borders for the rows, and onmouseover /...
1
by: dx | last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as simple as setting oCheckBox.Checked = True.. yet for some reason it isn't. I have a user control (ascx) that that has a...
9
by: brian.mills | last post by:
I've been building some custom controls which have some special functionality with the data I use from a web service, specifically the ability to data bind to attributes (without an accessor...
2
by: James T. | last post by:
Hello! I developed a composite control that inherits from HyperLink and overrides Render method. In web form I am using this control with DataGrid. On DataGrid ItemDataBound event I set...
8
by: Lyners | last post by:
I have code that retrieves all of the file names within a directory. After retriving them, I display the information in a datagrid. What I would like to do is add an extra output column on the...
5
by: owais | last post by:
Hi, I have a problem, I want to implements Parent class interface methods in child class. for e.g -------------- Test1.vb ---------------- Imports System Imports System.Web.UI Imports...
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
23
anfetienne
by: anfetienne | last post by:
Hi, I have this php code that creates a xml file(#1)....is is possible to get it to generate a xml like the 1 below(#2)? The main part of my xml is the caption....can i add a new line to be...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.