Add client event to user control?

G

Guest

I have a user control as my Save button for my page. When the Save button is
clicked, I want some client-side validation to take place from a javascript
function in the page, and then I'd like the server event to fire to update
the database. I've successfully done this with a normal webcontrol via:
myControl.Attributes("onclick") = "javascript:alert('You clicked me!!');"

In that case, myControl is an asp:button on the page.

In the case where I'm having a problem, I have a link button wrapped in a
user control, and I can set the properties of it in my aspx.vb page. I tried
the same thing, but the user control doesn't ever fire the client event. Can
anyone tell me what I'm missing? Here's a sample of the pieces I have...

When I click the SaveContinue button in the user control, I need the
ValidateForm() function to fire in the javascript code, then the
lbtnSaveContinue_Click event on the server.

THANKS!!!

Aspx form:
function ValidateForm(form) {

alert("Form validated!");

}

…some HTML ...
<td vAlign="top" width="100" bgColor="teal">
<asp:placeholder id="phLeftNav" Runat="server"></asp:placeholder></td>

…more HTML ….

-------------------------------------------------------------
Aspx.vb form:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

left = LoadControl("../LeftNav.ascx")
With CType(left, LeftNav)
'Add a client-side onclick event to the buttons.
'The client-side event will fire first, then the postback.
.SaveContinue.Attributes.Add("onclick", "return
ValidateForm(this);")
End With
phLeftNav.Controls.Add(left)
End Sub

Protected Sub lbtnSaveContinue_Click() Handles left.SaveContinueEvent
Dim SaveMe As New CompanyBC
'I'm expecting the client side code to fire before I get here, but I get
'here first.
SaveMe.SaveForm()

End Sub
-------------------------------------------------------------
leftNav.aspx (user control):

<asp:LinkButton ID="lbtnSaveContinue" Runat="server"
CssClass="LeftButtonOff" AccessKey="o"
CommandArgument="CompanySummary.aspx?companyid=13640&redirect=true"
ForeColor="White"> Save & C<u>o</u>ntinue</asp:LinkButton>

leftNav.aspx.vb:
Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents lbtnSaveContinue As
System.Web.UI.WebControls.LinkButton
Public Event SaveContinueEvent()

… more generated code...

#End Region
Public Property SaveContinueButton() As WebControls.LinkButton
Get
Return lbtnSaveContinue
End Get
Set(ByVal Value As WebControls.LinkButton)
lbtnSaveContinue = Value
End Set
End Property

Private Sub lbtnSaveContinue_click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lbtnSaveContinue.Click
RaiseEvent SaveContinueEvent()
End Sub
 
G

Greg Burns

Look at the output of the page. Is there 2 onclick's wired to the same
button? It will only call one of them.

Checkout link to see what it is your dealing with (perhaps) (this was just
the first link I found, there are many)
http://tinyurl.com/6zyjw

HTH,
Greg
 
G

Guest

My problem is seems the opposite...no onClick events in the source of the
page. BUT, it's a link button rather than a button. And there is javascript
call in the <a href> - here's the view source section:

<a id="_ctl8_lbtnSaveContinue" accesskey="o"
href="javascript:__doPostBack('_ctl8$lbtnSaveContinue','')"
style="color:White;"> Save & C<u>o</u>ntinue</a>

Any ideas? Is the problem that I'm using a link button? I like the link
button because it looks the way I need it to look. Button's didn't seem
versatile enough for me to achieve the look that I need to make this look
right.
 
K

Ken Dopierala Jr.

Hi,

You might need to change the control name in the doPostBack function to:

'_ctl8_lbtnSaveContinue'

The $ might be throwing it off but I'm not sure. It is worth a try. Also,
maybe you don't even need to put the _ctl8$ or _ctl8_ in there at all
because the server might not need it. Good luck! Ken.
 
G

Guest

Ok, I tried changing my linkbutton to a button, and now I can get the
client-side script event to fire.

I couldn't make a button look right for me before, but I'm now finding it
easier to make an asp:button look different than to fire client-side code
from a link button.

Thanks Greg for your help. I think I'm all set on this one.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top