Ask a Question related to ASP.NET Building Controls, Design and Development.
-
paul.hester@gmail.com #1
Exposing an event handler as a control property
Hi all,
I have a custom server control that can contain one or more buttons.
I'm trying to expose an event handler as a property so that the buttons
can fire a click event when clicked. However, I'm getting the following
error:
Cannot create an object of type 'System.EventHandler' from its string
representation 'OnSubmit' for the 'OnSubmit' property.
My control's tag looks like this:
<stuff:MyControl OnSubmit="OnSubmit" Width="430"
runat="server">...</stuff:MyControl>
The property looks like this:
public EventHandler OnSubmit
{
get { return _onSubmit; }
set { _onSubmit = value; }
}
I've tried the property with and without a type converter without
success.
Any help would be much appreciated.
Thanks,
Paul
paul.hester@gmail.com Guest
-
exposing a style property
I've built a custom control which basically includes a textbox and a validatorcontol as child controls. (Code included below) I've exposted... -
Exposing Font Name Property for Custom Control
Hi, I've created a custom web control for using in my web application. I want to expose some Font-Name Properties of the control like i did for... -
open pop up window from asp server control event handler
Check out this article, this will help you http://www.microsoft.com/india/msdn/articles/PopupCalendarinASP.aspx In this article, i used window.open... -
Event Handler not Working in Composite Control
peter@workingmachines.com (Peter Chang) wrote in message news:<ce2e3f1c.0306201035.36a03bd5@posting.google.com>... Perhaps it is a bug in ASP.NET... -
How to Create a user control with a event property?
I want to create a usercontrol and can set Click Event Function to it at desined-time,just as <myprefix:myLinkButton ... OnClick="ClickFunction"... -
Teemu Keiski #2
Re: Exposing an event handler as a control property
Hi,
you need to expose it as full-blown event, not as property. Page parser in
ASP.NET is then able to wire syntax On<EventName>="handler_method_name" into
your event as listener. Therefore the code would be pretty much like asa
follows. Read also the comments I've made into the code.
/// <summary>
/// Static key of all the event handlers
/// </summary>
private static readonly object EventClick = new object();
/// <summary>
/// This is the event where event handlers are collected into a
EventHandlerList
/// Page parser parses them from declarative syntax or you add them in
code
/// </summary>
public event EventHandler Submit
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.RemoveHandler(EventClick, value);
}
}
/// <summary>
/// This method is used to raise Submit event. Do not *confuse* with
aspx's OnSubmit="method_name" syntax
/// </summary>
/// <param name="e"></param>
protected virtual void OnSubmit(EventArgs e)
{
EventHandler handler = Events[EventClick] as EventHandler;
if (handler != null)
handler(this, Error);
}
/// When handling Button's click inside the control, you'd call
this.OnSubmit(EventArgs.Empty);
/// when you want the event to be raised
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
[url]http://blogs.aspadvice.com/joteke[/url]
<paul.hester@gmail.com> wrote in message
news:1156742811.599520.168410@m73g2000cwd.googlegr oups.com...> Hi all,
>
> I have a custom server control that can contain one or more buttons.
> I'm trying to expose an event handler as a property so that the buttons
> can fire a click event when clicked. However, I'm getting the following
> error:
>
> Cannot create an object of type 'System.EventHandler' from its string
> representation 'OnSubmit' for the 'OnSubmit' property.
>
> My control's tag looks like this:
>
> <stuff:MyControl OnSubmit="OnSubmit" Width="430"
> runat="server">...</stuff:MyControl>
>
> The property looks like this:
>
> public EventHandler OnSubmit
> {
> get { return _onSubmit; }
> set { _onSubmit = value; }
> }
>
> I've tried the property with and without a type converter without
> success.
>
> Any help would be much appreciated.
>
> Thanks,
>
> Paul
>
Teemu Keiski Guest
-
Teemu Keiski #3
Re: Exposing an event handler as a control property
And line
handler(this, Error);
should be
handler(this, e);
Sorry for the typo.
"Teemu Keiski" <joteke@aspalliance.com> wrote in message
news:%23ahwS0ryGHA.3632@TK2MSFTNGP04.phx.gbl...> Hi,
>
> you need to expose it as full-blown event, not as property. Page parser in
> ASP.NET is then able to wire syntax On<EventName>="handler_method_name"
> into your event as listener. Therefore the code would be pretty much like
> asa follows. Read also the comments I've made into the code.
>
> /// <summary>
> /// Static key of all the event handlers
> /// </summary>
> private static readonly object EventClick = new object();
>
> /// <summary>
> /// This is the event where event handlers are collected into a
> EventHandlerList
> /// Page parser parses them from declarative syntax or you add them in
> code
> /// </summary>
> public event EventHandler Submit
> {
> add
> {
> Events.AddHandler(EventClick, value);
> }
> remove
> {
> Events.RemoveHandler(EventClick, value);
>
> }
> }
>
> /// <summary>
> /// This method is used to raise Submit event. Do not *confuse* with
> aspx's OnSubmit="method_name" syntax
> /// </summary>
> /// <param name="e"></param>
> protected virtual void OnSubmit(EventArgs e)
> {
> EventHandler handler = Events[EventClick] as EventHandler;
> if (handler != null)
> handler(this, Error);
> }
>
> /// When handling Button's click inside the control, you'd call
> this.OnSubmit(EventArgs.Empty);
> /// when you want the event to be raised
>
>
> --
> Teemu Keiski
> ASP.NET MVP, AspInsider
> Finland, EU
> [url]http://blogs.aspadvice.com/joteke[/url]
>
>
> <paul.hester@gmail.com> wrote in message
> news:1156742811.599520.168410@m73g2000cwd.googlegr oups.com...>>> Hi all,
>>
>> I have a custom server control that can contain one or more buttons.
>> I'm trying to expose an event handler as a property so that the buttons
>> can fire a click event when clicked. However, I'm getting the following
>> error:
>>
>> Cannot create an object of type 'System.EventHandler' from its string
>> representation 'OnSubmit' for the 'OnSubmit' property.
>>
>> My control's tag looks like this:
>>
>> <stuff:MyControl OnSubmit="OnSubmit" Width="430"
>> runat="server">...</stuff:MyControl>
>>
>> The property looks like this:
>>
>> public EventHandler OnSubmit
>> {
>> get { return _onSubmit; }
>> set { _onSubmit = value; }
>> }
>>
>> I've tried the property with and without a type converter without
>> success.
>>
>> Any help would be much appreciated.
>>
>> Thanks,
>>
>> Paul
>>
>
Teemu Keiski Guest
-
studen771 #4
Re: Exposing an event handler as a control property
"Teemu Keiski" wrote:
> /// When handling Button's click inside the control, you'd call
> this.OnSubmit(EventArgs.Empty);
> /// when you want the event to be raised> Teemu Keiski
> ASP.NET MVP, AspInsider
> Finland, EU
> [url]http://blogs.aspadvice.com/joteke[/url]
When you gave the instructions, "when handling Button's click inside the
control," how is this done? where in the control's class do you assign
register its click event?
studen771 Guest



Reply With Quote

