Convert ADDHandler and AddressOf to C#

G

Guest

Been reading Dino Esposito's Article "A DetailsView Control for ASP.NET 1.x"
found in the msdn Library. His example was created using VB.NET. I am trying
to convert the example (DetailsView control) to C#.

The Issue is I converted this line (in VB):

AddHandler _insertButton.Click, AddressOf OnApplyInsertMode

to C#.NET with this:

this._insertButton.Click +=new EventHandler(this.OnApplyInsertMode);

the problem is the OnApplyInsertMode(object sender, EventArgs e) is not
subscribing to the click event fired by _insertButton.....Its not actually
calling OnApplyInsertMethod().

how do I go about this? FYI, the method containing AddHandler
_insertButton.Click, AddressOf OnApplyInsertMode and the
OnApplyInsertMode(object sender, EventArgs e) is in the same class.
 
K

Karl Seguin [MVP]

You need to make sure that the handler is hooked up on postback. If you add
it on page load, and then click the button, the page postsback to itself,
recreating the page class and all controls. The event handler will have
gotten lost and you need to hook it up again during the onLoad or earlier
phase...

Karl
 
G

Guest

Where exactly do I have to put the EventHandler? on DataBind(),
CreateControlHierarych(), CreateChildControls...I'm really confused. here is
the case, the OnApplyInsertMode(object sender, EventArgs e) calls
SetCurrentMode(DetailsViewMode mode) which then contains the call to
overriden DataBind(). So having said that, if OnApplyInsertMode() is not
called, DataBind() would never be called, and if DataBind() is never called,
subsequent methods like, CreateControlHierarchy(), CreateChildControls(),
Render() would never be read. In effect. even If I put the event on the
overriden DataBind(), it would never be called on postback. Am I correct?
whew, Confusing....back to the earlier question, where do you think is the
best place to put my event. Is it possible for the control to know if the
page using the control Posts back?

anyway here is the complete OnApplyinsertMode() --

protected void OnApplyInsertMode(object sender, EventArgs e)
{
SetCurrentMode(DetailsViewMode.Insert);
}

// and here is the SetCurrentMode() method which calls DataBind()

protected void SetCurrentMode(DetailsViewMode mode)
{
CurrentMode = mode;
DataBind();

// Must follow DataBind because we need to recreate the structure
to display
// the new record. In doing so, the link buttons are recreated
too with
// default settings.

ShowSaveButton(true);
}

-- and here is the DataBind Method --

public override void DataBind()
{
base.OnDataBinding(EventArgs.Empty);
// Clears the existing control hierarchy

Controls.Clear();

// Clears any viewstate for children because new children are
being created

ClearChildViewState();

// Track changes during data binding
TrackViewState();

// CreatControlhierarchy
CreateControlHierarchy(true);

// Mark the Control as having been created
ChildControlsCreated = true;
}

I'm looking into DataBind() thinking that this is the method called on
reconstructing the the control, but again, OnApplyInsertMode() causes(through
the call of SetCurrentMode()) to call DataBind()....
 
K

Karl Seguin [MVP]

I downloaded the article. I noticed that the event is hooked into the
CreateCommandBar function which is called by CreateControlHierarchy.

I see what you mean about this being called in DataBind, but it's also
called by CreateChildControls which would always be called. There's a
comment in the function that explain how it works. But it basically uses a
value to determine if it's in postback and if so, called the
CreateControlHierach (if it isn't in postback. My guess is you might have
gone wrong here...

Karl
 
G

Guest

Oh ok....I'll check the CreateChildControls()and my CreateControlHierarchy()
and i'll work my codes from there. Your definitely right, Most probably that
is the problem.

Thank you very much!!! :)
 
G

Guest

It worked now, just a little problem on paging but the primary problem was
solved (EventHandling), Guess the subject must be changed to "EventHandling
in Controls" so it may be usefull for others if they encounter the problem.

Thanks again Karl. :)

Carlo
 
G

Guest

ok now everything is working as it should....The real problem was not with
CreateControlHierarchy( ) since it was done as it should but it was a problem
with my overriden LoadViewState( ) causing my items not to be retrieved, so
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top