I have DataList which contains different fields. One of them is next DataList. This inner DataList contains button. And now I would like to bind data for the inner DataList in code behind in OnItemDataBound method. And I need to write there delegate for button. How can I do it? I find the button as the follow:
((Button)e.Item.FindControl("btn_down"))
And now I would like somehow to define what it should do
And it's important I don't want to use:
((Button)e.Item.FindControl("btn_down")).Click +=new EventHandler(btn_Click);
as I need to use some data from OnItemDataBound in this 'Click' function
((Button)e.Item.FindControl("btn_down")).Click +=new EventHandler(btn_Click);
private void btn_Click(object sender, EventArgs e)
{
}
Edit, if you need a custom event handler instead of the default one:
((Button)e.Item.FindControl("btn_down")).Click += new EventHandler(delegate(Customer Parameters Here) {});
private void btn_Click(Customer Parameters Here)
{
}
Ok,
I found a solution:
((Button)e.Item.FindControl("btn_up")).Click += new EventHandler(delegate(object s, EventArgs args) {});
Related
this is my code and from toolstrip click event i want to call the menustrip sub items
Ex: Menu like : Settings -> User. I want to call user_click event from toolstip click
private void tbrIUC1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string menuname="mnuuser";
e.ClickedItem.Click += new EventHandler(menuname + "_Click");
}
You can use anonymous delegate:
private void tbrIUC1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string menuname="mnuuser";
e.ClickedItem.Click += (s, ev) => { your code when clicked };
}
From another stackoverflow post I got the following working for me:
ButtonName.Click += (se, ev) => button1_Click(se, ev);
Link to "inspiration" How can I create a dynamic button click event on a dynamic button?
Look for the answer from A9S6 and the comment to this from Scott Beeson
EDIT
To call a function BASED on the function name (string) I got the following article:
http://www.vcskicks.com/call-function.php
I haven't got it working for me by now but I wanted to share a possible solution
If all fails: I recommend declaring a new method with a Switch that gets the control and the string passed
I want to add an event to a programmatically generated button like this:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
I'm getting the following exception in the 2nd line:
Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong.
You need to create a instance of RoutedEventHandler:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF.
You can also use lambda functions
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
In C#, how is the best way to add an additional property to a button event call?
Here is the code for the EventHandler:
button.Click += new EventHandler(button_Click);
Here is the code for the button_Click:
private void button_Click(object sender, EventArgs e)
{
}
If I want to add a PropertyGrid parameter to the button_Click function parameters, how is the best way to do this?
I am wanting to do this as the button.Click code is in a function that has a PropertyGrid parameter, and in the button_Click function, I need to set the PropertyGrid selected object. This is only set when the button.Click button is clicked.
If I set the tag of the button to be a PropertyGrid object, how can I retrieve this tag object in the button_Click code?
The button_Click event is called from an object, and the sender is the object, that is not the button.
Can I please have some help with the code?
You cannot convince a Button that it should know anything about a PropertyGrid. When it fires its Click event then it can only tell you about what it knows. Which is cast in stone.
You trivially work around this by using a lambda expression, it can capture the PropertyGrid argument value and pass it on to the method. Roughly:
private void SubscribeClick(PropertyGrid grid) {
button.Click += new EventHandler(
(sender, e) => button_Click(sender, e, grid)
);
}
This can seems like a simple question ... the crux is how to match the button delegate signature void, object, eventargs with my method or use an event delegate.
As an example, I have code for a button that changes color when it's clicked. However,
button1.Click += new EventHandler(KK.ChangeColor);
carries the EventArgs from the button to the ChangeColor(object sender, EventArgs e) method, but is meaningless to the rest of the code which use ColorEventArgs; and
button1.Click += delegate(object sender, EventArgs e){ KK.ChangeColor(sender); };
doesn't allow for later removal of the delegate later in the code.
So which is better? Adding unnecessary parameters to all my methods to match the button delegate or suffering from not being able to remove the delegate later ?
or How would I change the delegate signature of the button? It seems there must be a 'cleaner' way to do this?
Will appreciate advice.
"It seems there must be a 'cleaner' way to do this?"
In my opinion, better design would depends on what exactly ChangeColor method do. If it is doing only specific operation that closely related to event button clicked, I would just leave it as the real event handler method. That means, it should have required parameters to match Button.Click event handler signature (I don't think there is option to "change the delegate signature") :
void ChangeColor(object sender, EventArgs e)
{
MessageBox.Show("Button {0} clicked!!!", ((Button)sender).Name);
}
Otherwise, if it is doing not only specific operation related to event button clicked, I would refactor codes unrelated to button click event to another method. This way, the other method doesn't need to have unnecessary parameters. For example :
void ChangeColor(object sender, EventArgs e)
{
var buttonName = ((Button)sender).Name;
MessageBox.Show("Button {0} clicked!!! Save form data", );
//assume that form name can be determined from name of button being clicked
SaveFormToDatabase(buttonName);
}
private void SaveFormToDatabase(string formName)
{
//save form specified in parameter
}
here is the basic model of the code I have:
private void textBlock1_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox TextBox1 = new TextBox();
TextBlock tblk = (TextBlock)sender;
ApplicationBar = new ApplicationBar();
TextBox1.LostFocus += TextBox1_LostFocus;
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/check.png", UriKind.Relative));
appBarButton.Text = "Accept";
ApplicationBar.Buttons.Add(appBarButton);
appBarButton.Click +=
}
void TextBox1_LostFocus(object sender, RoutedEventArgs e)
{
//do things here
}
I need to subscribe to the click Event, and when it is triggered, I need TextBox1_LostFocus to be called and TextBox1 to be sent as a parameter. Basically what I want to do is make appBarButton.Click do the exact same thing as TextBox1.LostFocus.
Problem is, LostFocus is a RoutedEventHandler and TextBox1_LostFocus takes a RoutedEventArgs as a parameter while appBarButton.Click is an EventHandler.
I'm not very experienced in coding at all so any help is much appreciated!
RoutedEventArgs inherits EventArgs.
You can add the same handler to both events.
Better yet, you can move the code to a function and call it from two different event handlers.