Set a Programmatically Event for a Child Inside a FormView

I understand that you can declaratively assign an event handler to a child element inside the form by adding its attribute to the aspx page (that is, onclick = "Button_Click"), but if I wanted to do it programmatically, how would I go about it? This control will not be found through FormView.FindControl in the Init Init or Load events, so it cannot be assigned at these stages. The DataBound event for FormView will allow you to find the control, but does not fit, because it happens only once, and then the event will not always be connected, and it will not fire. I do not ask, because I can not get around this, I just wanted to know how to do this.

Greetings.

+6
c # event-handling formview
source share
4 answers

You must use the FormView ItemCreated Event. This happens after all the rows are created, but before the FormView is bound to the data.

Take a look at this MSDN page for more information.

+2
source share

in C #, this is done through:
this.btnProcess.Click + = new EventHandler (btnProcess_Click);

+1
source share

Have you tried PreRender? I think it's already too late, but I'm surprised that this is not in your post.

It seems strange to me that you add an event that you expect to start immediately. Why not just call a handler or function in this case.

+1
source share

Maybe something is missing for me! Instead, you can use event bubbles. But the FormView control does not have events to handle the events of the child controls. So, we have some changes.

[System.Web.UI.ToolboxData("<{0}:FormView runat=\"server\" />")] public class FormView : System.Web.UI.WebControls.FormView { private static readonly object _itemClickEvent = new object(); [System.ComponentModel.Category("Action")] public event EventHandler<System.Web.UI.WebControls.FormViewCommandEventArgs> ItemClick { add { base.Events.AddHandler(_itemClickEvent, value); } remove { base.Events.RemoveHandler(_itemClickEvent, value); } } protected virtual void OnItemClick(System.Web.UI.WebControls.FormViewCommandEventArgs e) { EventHandler<System.Web.UI.WebControls.FormViewCommandEventArgs> handler = base.Events[_itemClickEvent] as EventHandler<System.Web.UI.WebControls.FormViewCommandEventArgs>; if (handler != null) handler(this, e); } protected override bool OnBubbleEvent(object source, EventArgs e) { this.OnItemClick((System.Web.UI.WebControls.FormViewCommandEventArgs)e); return base.OnBubbleEvent(source, e); } } 

The above code snippet demonstrated how we can add an ItemClick event to a FormView control to handle the events of child controls. You should now map or drag the new FormView control.

+1
source share

All Articles