I'm trying to create a custom list view box that will display a list of items with corresponding images associated with these items. Likewise, when the user clicks on an item within the list view, the server will respond accordingly.
Sounds pretty nice, huh? Well, so far I can do ALL of this BUT, the "OnClick" event. So, in order to move forward, I need to consult outside help.
Currently, on page load, the page adds custom controls that are populated with the icon and text. I'd like to add the handling of the click event at this adding stage. But, my custom control doesn't fire a click event. How can I create events on a custom control? Can someone point me into that direction?
Public Event Click As EventHandler
Protected Sub OnClick(ByVal e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnClick(System.EventArgs.Empty)
End Sub
I can then add to the page creating the server control...
Are you trying to raise an event through the RaisePostBackEvent? What exactly is your goal, typically you wouldn't want to do this, nor would this fire.... Is this user driven or an event you want to fire automatically based on some condition?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
MKoslof,
My goal is to create a "List View" by populating a web-container with custom controls. This custom control is just a line of HTML that displays an image and the associated text with a hidden value. "On mouse-click" of this custom control, I'd like the server to handle the hidden value appropriately. To answer your questions, yes, I'm trying to raise the event through RaisePostBackEvent, I have the code displayed of what I'm using. Also, this is a user driven event I'm trying to create... OnClick. Any suggestions?
Spico,
I have never seen "with events" before... Where do I place this line? I just tried googling it and I've gotten a list of bogus sites. Can you reccommend a good page to learn more about this?
Thanks for your post. I've read the article, plus did some more research on the topic and I'm still not getting the click event to fire. I've decided to take this down to ground zero and build it back up. It would be great if you could continue to help guide me in this process.
SO, ground zero... What am I missing here?
I have a custom control that has ONE input... Text. By specifying the text, a <div> statement gets generate and spits out <div>Input Text</div>. I want this div statement to have a click event that posts back to the server.
The render code...
Code:
Public Overrides Sub RenderBeginTag(ByVal ControlWriter As System.Web.UI.HtmlTextWriter)
ControlWriter.Write("<div>" & Me.Text())
End Sub
Public Overrides Sub RenderEndTag(ByVal ControlWriter As System.Web.UI.HtmlTextWriter)
ControlWriter.Write("</div>")
End Sub
From the recent readings, I've gathered that my click event needs to be declared and coded as follows...
Code:
Private Shared ReadOnly ClickHandler As New Object()
Public Custom Event OnClick As EventHandler
AddHandler(ByVal value As EventHandler)
Events.AddHandler(ClickHandler, value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
Events.RemoveHandler(ClickHandler, value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
Dim submitHandler As EventHandler = CType(Events(ClickHandler), EventHandler)
If Not IsNothing(submitHandler) Then
submitHandler(Me, e)
End If
End RaiseEvent
End Event
I'm assuming, in the generation of the page, the server will automatically code in the post back event for the server to handle. Because I haven't added anything else. Or, should I not??
I find this method extremely cheating and it totally by-passes the entire "click event" I've been striving to achieve. Or... Maybe this is just a short-hand way of doing it?? I'm not sure. BUT, I'd love to get someone's feed back about this.
Thanks!
That is a round about way of doing it though client side events, basically something needs to invoke the actual post back still....for example, lets say its a button:
Code:
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
RaiseEvent OnClick(eventArgument)
End Sub
Private Sub AddControls(theArg)
Dim link As New Button
link.Text = "Something"
link.Attributes.Add("OnClick", Page.GetPostBackEventReference(Me, theArg)
End Sub
Basically you need to invoke the actual postback and pass the GetPostbackReference your type instance (Me) and the eventArg/ID of the control
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET. subscribe