I have a user control that has a data repeater. When the ItemDataBound even is raised I create a html table which contains a button on each row. The button is created in a seperate class, not in the code behind, so when I attach the button click event I
have to put the click method in the class that creates the control. However, when the button is clicked I need to open a modal form in the page that contains the button. How can I move the event back to the web user control with the sender object? I suspect
I need to do something with a delegate or something, but I am having a hard time putting how to do that together. Can someone tell me how to do this?
Great! Thank you very much. Do I need to attach the event to the buttonCreator class in the OnInit? I ask because I instantiate and kill the button creator class in the ItemDataBind event of a repeater control.
It does not matter where you attach the event handler as long as you databind your repeater before events fire. In simple terms if you databind on load event of high up event in the page life cycle, above solution works.
Krenshau
Member
13 Points
27 Posts
Transfer event OnClick
May 28, 2010 03:34 PM|LINK
Hello,
I have a user control that has a data repeater. When the ItemDataBound even is raised I create a html table which contains a button on each row. The button is created in a seperate class, not in the code behind, so when I attach the button click event I have to put the click method in the class that creates the control. However, when the button is clicked I need to open a modal form in the page that contains the button. How can I move the event back to the web user control with the sender object? I suspect I need to do something with a delegate or something, but I am having a hard time putting how to do that together. Can someone tell me how to do this?
Thank you.
Ben
Charith Guna...
Star
14958 Points
1854 Posts
Re: Transfer event OnClick
May 28, 2010 05:39 PM|LINK
You can add a event to the class where you create your button:
public class ButtonCreater { public event EventHandler ButtonClick; private Button button = new Button(); protected override void OnInit(EventArgs e) { base.OnInit(e); this.CreateControlHierarchy(); } public void CreateButton() { /// /// setup button properties /// button.Click += new EventHandler(ButtonClickInternal); this.Controls.Add(button); } private void ButtonClickInternal(object sender, EventArgs e) { if (this.ButtonClick != null) this.ButtonClick(sender, e); } }Then attach your event to the button creater where you create your buttons
protected override void OnInit(EventArgs e) { base.OnInit(e); this.buttonCreater.CreateButton(); this.buttonCreater.ButtonClick += new EventHandler(UserContorlButtonClick); } private void UserContorlButtonClick(object sender, EventArgs e) { /// /// Your event logic /// }Charith Gunasekara | FAQ
Krenshau
Member
13 Points
27 Posts
Re: Transfer event OnClick
May 31, 2010 05:04 AM|LINK
Great! Thank you very much. Do I need to attach the event to the buttonCreator class in the OnInit? I ask because I instantiate and kill the button creator class in the ItemDataBind event of a repeater control.
Charith Guna...
Star
14958 Points
1854 Posts
Re: Transfer event OnClick
May 31, 2010 10:35 AM|LINK
It does not matter where you attach the event handler as long as you databind your repeater before events fire. In simple terms if you databind on load event of high up event in the page life cycle, above solution works.
Charith Gunasekara | FAQ
Krenshau
Member
13 Points
27 Posts
Re: Transfer event OnClick
Jun 02, 2010 11:08 PM|LINK
Great, thank you very much!