views:

935

answers:

2

how can i call a VB function - deleteevent() in usercontrol.ascx.vb from a javascript function in clickhandler(e) in usercontrol.ascx. The call should cause a postback because i need the usercontrol to display the changes.

i am currently trying to do it by using a linkbutton with style display:none, and calling its click event from the javascript function. But i dunno how to call the click event.

i had to pass a value to the vb function from the javascript, but i am taking care of it using a hiddenfield.

the environment is asp.net 3.0 language:vb

thanks.

KK

+4  A: 

Check this two links about calling codebehind methods with JQuery :

Using jQuery to directly call ASP.NET AJAX page methods

Using jQuery to Call ASP.NET AJAX Page Methods

You should call aspx instead of ascx methods. beacuse at runtime all user controls (ascx) merged to your page (aspx).

If you have a user control with multiple instances in a page, you should use a parameter to define context of method in each usercontrol. Maybe user control's clientID.

Canavar
A: 

This one is slightly tricky, but kinda fun.

Basically, you need to:

  1. Implement IPostBackEventHandler on the control where you want to raise the event
  2. Create a postback reference for the control
  3. Call the postback reference, either via a hyperlink or jscript
  4. In the code behind, handle the postback reference and call your code

    Public Class MyControl
    Inherits UserControl
    Implements IPostBackEventHandler
    
    
    Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
    
    
    
    ' Call deleteEvent here
    DeleteEvent(eventArgument)  ' This will contain "SomeArgumentYouWantToPassToDeleteEvent"
    
    End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim cs As ClientScriptManager = Page.ClientScript
    Dim a As New HtmlAnchor()
    a.ID = "myanchor1"
    a.InnerText = "Delete Event"
    a.HRef = cs.GetPostBackClientHyperlink(Me, "SomeArgumentYouWantToPassToDeleteEvent")
    
    
    ' You could alternatively construct some jscript and output it.
    
    
    Controls.Add(a)
    
    End Sub

    End Class

For more info, see here: ClientScriptManager.GetPostBackEventReference

Matt Lynch