Custom Event became null on 2nd call - c#

I have written on event in a user control and that user control used twice in a page. Now the problem is, I am getting the Event as null for the 2nd time. Why? How to resolve the issue? Please help.
My code like:
in ascx:
public delegate void OnGoButtonClick();
public event OnGoButtonClick btnGoClickHandler;
protected void btnGo_Click(object sender, EventArgs e)
{
if (btnGoClickHandler != null)
btnGoClickHandler();
}
In aspx:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
MyUserControl.btnGoClickHandler += new UserControls.LoanPicker.OnGoButtonClick(PopulateDataOnGo);
}
But for the 2nd user control it is always null.

Make sure to subscribe to the event from both controls.
Regarding your comment:
how to detect which user control being triggered
You need to supply the object that raised the event to the event handler. Start by altering the delegate signature to look like this:
public delegate void OnGoButtonClick(object sender);
public event OnGoButtonClick btnGoClickHandler;
protected void btnGo_Click(object sender, EventArgs e)
{
if (btnGoClickHandler != null)
btnGoClickHandler(this);
}
Now alter the event handler which in this case appears to be PopulateDataOnGo to accept the sender and check who raised the event from there:
public void PopulateDataOnGo(object sender)
{
if (sender is ControlType1)
{
}
else if (sender is ControlType2)
{
}
}

Related

C# Form UserControl OnClick events

I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}

Text box value are not accessing in asp.net c#

i have a textbox on .aspx page..On this page there is a user control .Inside this user contrl there is a button .I want o get the value of text box on button click which is not inside the user control .How can i do this
Please Help me .
write this line in you button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
TextBox txtbox= (((MyPage)parent).FindControl("TextBoxid") as TextBox);
if(txtbox!=null)
(((MyPage)this.Page).FindControl("TextBoxid") as TextBox).Text;
//or
//(((MyPage)this.Parent).FindControl("TextBoxid") as TextBox).Text;
}
or
alternative is create the property in your page and access it in your user control
public string txtValue
{
get
{
return TextboxID.Text;
}
}
in button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
string txtvalue = ((Mypage)this.Page).txtValue;
//or
//((MyPage)this.Parent).txtValue;
}
protected void MyButton_Click(object sender, EventArgs e)
{
string TextBoxValue;
TextBoxValue = MyTextBox.Text;
}
Is it what you want ?
Try use the following method,
((TextBox)USerControl.Parent.FindControl("txtbox")).Text
((TextBox)USerControl.Page.FindControl("txtbox")).Text
or
((YourPageType)USerControl.Page).TextBox.Text
With de-coupling in mind, I would recommend that if your user control needs to access information outside of it, then that information should passed in, not vice versa. The control shouldn't be responsible for where the information comes from, it just knows there is information. With this in mind, I would recommend bubbling the event to get the required information.
Event Bubbling
This will involve creating a new delegate, and then triggering it once the Button has been clicked, thus bubbling the event and allowing us to return the desired value, which in this case is the textbox value.
Step 1: Declare the delegate
// declare a delegate
public delegate string MyEventHandler(object sender, EventArgs e);
Step 2: Update the user control
// update the user control
public class MyUserControl : UserControl
{
// add the delegate property to your user control
public event MyEventHandler OnSomeButtonPressed;
// trigger the event when the button is pressed
protected void MyButton_Click(object sender, EventArgs e)
{
string someString = string.Empty;
if (this.OnSomeButtonPressed != null)
{
someString = this.OnSomeButtonPressed(this, e);
}
// do something with the string
}
}
Step 3: Update the page
// be sure to register the event in the page!
public class MyPage : Page
{
protected override void OnLoad(object sender, EventArgs e)
{
base.OnLoad(sender, e);
myUserControl.OnSomeButtonPressed += this.HandleUserControl_ButtonClick;
}
public string HandleUserControl_ButtonClick(object sender, EventArgs e)
{
return this.SomeTextBox.Text;
}
}

How to call button click delegate of one ascx on another ascx button click?

I have a ascx page suppose page1.ascx in that I have a button click event handler
btnSave.Click +=
delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
and I have another ascx page suppose page2.ascx in that I have a button click
protected void btnEdit_Click(object sender, EventArgs e)
{
// want to call
btnSave.Click +=delegate
{
if (Save != null) Save(this, EventArgs.Empty);
};
}
I want to call btnSave click delegate(page1.ascx) on btnEdit(page2.ascx). Is it possible if yes then how?
If I understood what you mean, one way would be as below:
public class Control1 : UserControl {
public delegate void ButtonClickHandler(object sender, EventArgs e);
public ButtonClickHandler ButtonClickEvent {get;set;}
public void Save(object sender, EventArgs e) {
//do something
if (ButtonClickEvent != null) {ButtonClickEvent(sender, e);}
//do something
}
}
public class Control2 : UserControl {
protected override void OnLoad(EventArgs e) {
control1.ButtonClickEvent += YourMethod;
}
protected void YourMethod(object sender, EventArgs e) { // do something here ... }
}
Another way is to declare your button's event in the first control as property and assign your method in the 2nd control.
I'd recommend moving the code out of the code-behind and into a separate class, so that it is accessible to both .ascx files.

Declaring event in user control asp.net?

I have declared a event on my user control
public event EventHandler<AddressEventArgs> SaveButtonClick;
protected void ButtonSave_Click(object sender, EventArgs e)
{
if (SaveButtonClick != null)
{
SaveButtonClick(this, new AddressEventArgs) ;
}
}
After I have added the user control to the new page, how would I trap the
event raised by the user control?
Either you can [Browsable] property on the event, or you can imperatively bind to the events.
userControl.SaveButtonClick += new EventHandler(handlerFunctionName);
public void handlerFunctionName(AddressEventArgs args)
{
// Here, you have access to args in response the the event of your user control
}
Control.SaveButtonClick += controlClicked;
protected void controlClicked(object sender, EventArgs e)
{
//Do work
}
First you need to subscribe to your event, and give it a method to call when the event is raised.

How to work with delegates and event handler for user control

I have created a user control that contains a button.
I am using this control on my winform which will be loaded at run time after fetching data from database.
Now I need to remove a row from a datatable on the Click event of that button.
The problem is that how do I capture that event in my form. Currently it goes in that user control's btn click event defination.
You can create your own delegate event by doing the following within your user control:
public event UserControlClickHandler InnerButtonClick;
public delegate void UserControlClickHandler (object sender, EventArgs e);
You call the event from your handler using the following:
protected void YourButton_Click(object sender, EventArgs e)
{
if (this.InnerButtonClick != null)
{
this.InnerButtonClick(sender, e);
}
}
Then you can hook into the event using
UserControl.InnerButtonClick+= // Etc.
It's not necessary to declare a new delegate. In your user control:
public class MyControl : UserControl
{
public event EventHandler InnerButtonClick;
public MyControl()
{
InitializeComponent();
innerButton.Click += new EventHandler(innerButton_Click);
}
private void innerButton_Click(object sender, EventArgs e)
{
if (InnerButtonClick != null)
{
InnerButtonClick(this, e); // or possibly InnerButtonClick(innerButton, e); depending on what you want the sender to be
}
}
}
Just modernizing ChéDon's answer, here is how you can do it in 2018:
public class MyControl : UserControl
{
public event EventHandler InnerButtonClick;
public MyControl()
{
InitializeComponent();
innerButton.Click += innerButton_Click;
}
private void innerButton_Click(object sender, EventArgs e)
{
InnerButtonClick?.Invoke(this, e);
//or
InnerButtonClick?.Invoke(innerButton, e);
//depending on what you want the sender to be
}
}

Categories

Resources