CheckBox control and IPostBackDataHandler

F

Francois

Hi,

I am trying to implement a custom control that extends the standard
System.Web.UI.WebControls.CheckBox control. In the .Net class library
reference it is said that CheckBox Implements IPostBackDataHandler
interface. I want to override the 2 methods defined by that interface but i
do not see in the CheckBox class that the methods of IPostBackDataHandler
are implemented. But they should be as the class is said to implement the
interface.
The 2 methods defined by IPostBackDataHandler are:
- RaisePostDataChangedEvent
- LoadPostData

Then how cam I create a custom control that extends CheckBox and overrides
RaisePostDataChangedEven and LoadPostData?

I tried to following code but it does not compile, it says the
CheckBoxValue.cs: 'Bos.CustomControls.CheckBoxValue.LoadPostData(string,
System.Collections.Specialized.NameValueCollection)': no suitable method
found to override

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Bos.CustomControls
{

public class CheckBoxValue : System.Web.UI.WebControls.CheckBox
{

private bool isChanged = false;

public CheckBoxValue()
{
}

public bool IsChanged
{
get
{
return this.isChanged;
}
set
{
this.isChanged = value;
}
}

protected override void OnCheckedChanged(EventArgs e)
{
IsChanged = true;
base.OnCheckedChanged(e);
}

protected override bool LoadPostData( string postDataKey,
NameValueCollection postCollection)
{
}

}
}

Best regards,

Francois
 
J

Joe Fallon

I recommend you get the book "ASP.Net Server Controls and Components" by
Kothari and Datye.

You might want to try some code like this:

override bool IPostBackDataHandler.LoadPostData( string postDataKey,
NameValueCollection postCollection)

I think you are not allowed to us Public, Private, Protected on Interface
methods.
You also have to prefix the method with the Interface name.

Please let me know if that works for you.
 
F

Francois

Hi Joe,

Thanks for your help but that code does not compile neither and says that:
" The modifier 'override' is not valid for this item "

It seems that somehow i cannot override the interface
IPostBackDataHandler.LoadPostData method implemented by the base class.

I think I may not understand well enough inheritance and interface
implementation in C#. But I am pretty sure than in Java this would work.
Then maybe the problem is not related with the ASP.NET control but rather
with my pure C# skills. Any information on how to get up to speed on that
side? any good links in your Favorites? ;)

Thanks a lot,
Francois.
 
J

Joe Fallon

The samples in the book (I am 1/2 way through it) show that the controls
being built are inheriting from Web Control and directly implementing the
interface.


I am sure you can inherit an existing control but I have not got to the part
about the interface overrides.
Sorry.
 
J

Joe Fallon

BTW - I assumed you tried the code without the use of the word override.
But that may have been a poor assumption.

I believe C# allows you to simply re-implement the interface. (VB does not.)

bool IPostBackDataHandler.LoadPostData( string postDataKey,
NameValueCollection postCollection)
 
F

Francois

With or without the use of the keyword override it does not work.

And yes i can easily extend checkbox and it all works well. the only things
i cannot do is to override the methods of the interface
IPostBackDataHandler. Which for me is nonsense as those methods must exist
(as the base class implements the interface). If I can override the other
methods why not those 2 ? Eventually, I could solve to problem in the
following manner (see code hereunder), but I do not understand why. For me
the override should work. And it would also be a cleaner way to do. This
seems to me more like bypassing the implementation of the interface of the
base class which sounds dangerous to me as it brings a possibilty to do it
accidently.

public class CheckBoxValue : System.Web.UI.WebControls.CheckBox,
IPostBackDataHandler
{

protected override void OnCheckedChanged(EventArgs e)
{
// here i can override a method inherited from the base class
}

bool IPostBackDataHandler.LoadPostData(string postDataKey,
NameValueCollection postCollection)
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHandler. I have a
to re implement the interface like if it was never implemented by the base
class.
*/
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
/* here i cannot override this method that the base class implements (as
the base class also implement the interface IPostBackDataHandler. I have a
to re implement the interface like if it was never implemented by the base
class.
*/
}

}

Francois.
 
J

Joe Fallon

Try it without IPostBackDataHandler at the top of the class.

public class CheckBoxValue : System.Web.UI.WebControls.CheckBox
/// IPostBackDataHandler

The base class already implements it so you should not have to re-declare
that you are implementing it.

Then perhaps you should be able to use:
bool IPostBackDataHandler.LoadPostData(string postDataKey,
NameValueCollection postCollection)
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top