views:

33

answers:

3

Hello! I have a question about creating and managing events inside an ascx custom web control.

I have created a very stupid control consisting in a div containing a asp:Label control, it is a very simple structure:

<div id="mydiv" runat="server">
     <asp:Label id="mylabel" text="Text"... />
</div>

That is, very simple. I would like to add an event: clicked. I want to let the user add this control on the page and attach handlers to this event so that when this control is clicked it is possible to do something. It might seem a strange solution, it's like i'm trying to invent again the button control, that is: my custom button. Well to fire the event I would like to add a javascript in my div and call a js function that calls, using ajax mechanism, a server side function. Well how to call a server side function from here. I posted a question about how to call a server side function from a client side one and got some answers (many of them told me to use PageMethods), well it seems that pagemethod does not work, it compiles but when running and clicking on my control and executing the js (in the line PageMethods.mymethod()) here I have an error --> Java script exception: unrecognized method. It seems not finding the PageMethod.

Well, considering my objective, how can I do? Ah, a solution like: use the click event in the label is not what I want because the click event must fire when I click in the div, consider that I might set a large padding so that a large empty space can provide a large clickable area.

Thanks in advance.

A: 

Hey,

PageMethods uses a web service within the page class; I assume you want a postback to the server and process this click in the page, right?

All controls that postback use a __doPostBack('clientid', '') method to postback to the server. Your label would need to do the same. Within the control, the control needs to implement IPostBackEventHandler to process these events, and raise the click event.

Check out this example: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx

Brian
Sorry but I just do not want to post back, Anyway you told me something new, so, even if this is not the case for applying it, thanks! :)
Andry
A: 

Create a Event in your User Control

E.g: -

public event EventHandler<CustomEventArgs> Click;

and a handler

public void OnClick()
{
if(this.Click !=null)
{
    //what ever else you do
    this.Click(new CustomEventArgs(...));
}
}

Also handle the mouse click (mouseleftbuttonup) on the User Control (and fire your event from there). i.e.

protected void OnMouseDown(...){ this.OnClick(); }

From the ASP.NET page, you can register the user control event and handle it:

MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)

protected myctr_click(object sender, CustomEventArgs e){
//do anything
}
Bhaskar
OK, so every ascx control comes with some events like click, and more? I didn't know this.... I see, well this is what I needed Thanks
Andry
A: 

I may have misinterpreted your requirements, but can't you just wrap the div in an asp:linkbutton?

<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
 <asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>

and then on the server side:

protected void lnk_something_Click(object sender, EventArgs e)
{

}

As yellowfrog mentioned in the comments, you could then extend this by further wrapping it in an update panel for an async postback.

Zeus
Sorry, this is not what I want, anyway thanks for your help, I found the correct answer... :)
Andry