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.
Related
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)
{
}
}
In my User Control I have a Text Box which does the validation to take only digits. I place this user control on my form but the Keypress event is not Firing in form.Following is the code in my user control
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (this.KeyPress != null)
this.KeyPress(this, e);
}
private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar!=(char)Keys.Back)
{
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
but in Form also i want keypress event to fire but it is not firing
public Form1()
{
InitializeComponent();
txtNum.KeyPress += new KeyPressEventHandler(txtPrprCase1_KeyPress);
}
void txtPrprCase1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("KeyPress is fired");
}
but it is not firing. I don't understand what i want to do? It is Urgent for me.
This following override is not needed:
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (this.KeyPress != null)
this.KeyPress(this, e);
}
Because base.OnKeyPress(e); will fire the attached event. You don't need to do it manually.
Instead call OnKeyPress of user control in the text-box's event handler:
private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar!=(char)Keys.Back)
{
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
Try putting the event handler code in your Form_Load event, or using the Form Designer to create the event handler (it's in the lightning icon on the properties page).
In this code, Form1 supposes to listen to the Add button in user control and displays the message in the Form1. When i run it in a debugging mode, it return NullPointerReference at clicked(this,e). Can someone help me with this? thanks.
User Control:
public event EventHandler clicked;
public DataInput()
{
InitializeComponent();
Add.Click+= new EventHandler(Add_Click);
}
private void Add_Click(object sender, EventArgs e)
{
items = textBox1.Text.PadRight(15) + textBox2.Text.PadRight(15) + textBox3.Text.PadRight(15);
clicked(this, e);
}
Form:
public Form1()
{
InitializeComponent();
dataInput.clicked+= new EventHandler(OnChanged);
}
public void OnChanged(Object sender, EventArgs e)
{
MessageBox.Show("testing");
}
Exception is thrown because there are no subscriptions to your clicked event. Either Form1 is no yet created, maybe you're using different constructor, or you unsubscribed later.
Anyway, you should always check for subscription before invoking an event delegate.
Change your code in Add_Click to:
EventHandler evnt = clicked;
if (evnt != null)
evnt(this, e);
not sure why the "clicked" EventHandler is null.
You should always make sure the EventHandler has been initialized before using.
i.e.
if(clicked != null)
{
clicked(this, e);
}
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;
}
}
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
}
}