By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

How to programmatically invoke Click event of the Button ?

P: n/a
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.

How to perform this ?

Oleg Subachev
Mar 23 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Dnia 23-03-2006 o 17:07:41 Oleg Subachev <ol**@urvb.ru> napisał:
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.

How to perform this ?

[PD] Something like this should work:
typeof(Button).InvokeMember("OnClick",BindingFlags .NonPublic|BindingFlags.InvokeMethod,null,
myButton, new object[]{EventArgs.Empty});
I'm writing without actually compiling this code but you should get the
idea.

--
Piotr Dobrowolski
Piotr.Dobrowolski@_usun_gmail.com
Mar 23 '06 #2

P: n/a
On Thu, 23 Mar 2006 21:07:41 +0500, Oleg Subachev wrote:
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.


You can use the PerformClick() function. But it's a lot better in my
opinion to move the logic behind the button click in a separate function
that would be called in the button's click event handler. You could then
call this function from anywhere else you want to.
Mar 23 '06 #3

P: n/a
theButton.PerformClick();

Obviously, since theButton is proabbly private, you probalby need to wrap
this in a public method

Marc

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.

How to perform this ?

Oleg Subachev

Mar 23 '06 #4

P: n/a
There is Control.InvokeOnClick event that is also protected that you can
call to invoke the OnClick event on any other control as long as you have
reference to it.

For example if your form wants fire the click event on one of its buttons
the form may do:

this.InvokeOnClick(button1, EventArgs.Empty);
--
HTH
Stoitcho Goutsev (100)

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.

How to perform this ?

Oleg Subachev

Mar 23 '06 #5

P: n/a
> You can use the PerformClick() function.

And what if I need to invoke another event ?
Or if I need to invoke an event of Label class ?

I think the best way is to put corresponding protected
On<Event> method into the public wrapper.
But it's a lot better in my
opinion to move the logic behind the button click in a separate function
that would be called in the button's click event handler. You could then
call this function from anywhere else you want to.


I can't do this because Button's click event may contain different handlers
during execution of my application.
Mar 24 '06 #6

P: n/a
> Obviously, since theButton is proabbly private, you probalby need to wrap
this in a public method


Yes, this is the best solution IMHO.

Oleg Subachev
Mar 24 '06 #7

P: n/a
> There is Control.InvokeOnClick event that is also protected that you can
call to invoke the OnClick event on any other control as long as you have
reference to it.


Thanks.
But what if I need to invoke another event ?

Oleg Subachev
Mar 24 '06 #8

P: n/a
On Fri, 24 Mar 2006 09:57:54 +0500, Oleg Subachev wrote:
You can use the PerformClick() function.
And what if I need to invoke another event ?
Or if I need to invoke an event of Label class ?


That of course won't work then. But your question was "How to
programmatically invoke Click event of the Button ?". I'm sorry but I don't
have yet the ability to read into usenet's users mind (but i'm working on
it).

As for your new question, wrapping the OnEvent() methods may be a solution
of course but it sounds like you are mixing a bit to much the UI code with
the application logic which ultimately tends to make the code difficult to
read and maintain. Much better in my opinion would be to clearly separate
those 2 layers. E.g., if you have a button "Take Order", then this button's
click event handler should call the TakeOrder() method. If you need to take
an order programmatically, you can call TakeOrder() instead of calling the
button's event handler. If the event handler of the button changes, this
shouldn't affect the rest of the application logic. Your code should be
able to determine what function to call at any time rather than blindly
calling the button's event handler hoping that it's gonna do the right
thing.
I think the best way is to put corresponding protected
On<Event> method into the public wrapper. [...] I can't do this because Button's click event may contain different handlers
during execution of my application.


What would happen if later you decided to modify your UI and use 2 separate
buttons instead a single button with changing event handlers? If your code
was just calling your button's OnClick() method to do what the button was
supposed to do, you would have to go through all your code and try to find
out for each OnClick() call what you really wanted to do by calling
OnClick(). Plus i don't want to think of how difficult to read your code
would be to another programmer with OnClick() calls of a button which click
event handlers keep changing during the execution of the program.
Mar 24 '06 #9

P: n/a
An open-ended "invoke any event on any object, even if I didn't create it"
is a really bad idea; however, one trick you could use here is a bit of
misdirection;

Currently:
* Control X has events A, B, C, which some other code subscribes to
* you want to invoke them, and can't (except for a few handy ones like
OnClick)

Possible solution:
* Control X has events A, B, C
* You write a proxy class Y, with events D, E, F (probably called the same
as A, B, C); it subscribes to A, B and C, while also providing OnD, OnE,
OnF; if A is caught, it calls OnD, etc
* Your other code subscribes to D, E, F

Now when you want to invoke the events, it calls OnD, etc; you never need
talk directly to the base object.

Just a thought. You could also inherit from the Controls directly, but this
could get messy and more complex

Marc
"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
There is Control.InvokeOnClick event that is also protected that you can
call to invoke the OnClick event on any other control as long as you have
reference to it.


Thanks.
But what if I need to invoke another event ?

Oleg Subachev

Mar 24 '06 #10

P: n/a
Marc si right, in the general case you cannot raise events of other objects
unless the framework priveds API for doing so like the couple of events that
the control class has InvokeMethods for.

In C# (I don't know if it is C# only or is in the .NET as a whole) if an
object can access all memebers of other objects declared on level of the
same type regardless of their visibility.
That's how the a control can invoke OnClick protected method of other
control objects - the trick is that InvokeOnClick and OnClick both are
defined on the level of the Control class.

Other solutions are inheritance and (if OnXXXX methods provided) or use some
other method as the one Marc suggested.
--
HTH
Stoitcho Goutsev (100)

"Marc Gravell" <ma**@nonesuch.com> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
An open-ended "invoke any event on any object, even if I didn't create it"
is a really bad idea; however, one trick you could use here is a bit of
misdirection;

Currently:
* Control X has events A, B, C, which some other code subscribes to
* you want to invoke them, and can't (except for a few handy ones like
OnClick)

Possible solution:
* Control X has events A, B, C
* You write a proxy class Y, with events D, E, F (probably called the same
as A, B, C); it subscribes to A, B and C, while also providing OnD, OnE,
OnF; if A is caught, it calls OnD, etc
* Your other code subscribes to D, E, F

Now when you want to invoke the events, it calls OnD, etc; you never need
talk directly to the base object.

Just a thought. You could also inherit from the Controls directly, but
this could get messy and more complex

Marc
"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
There is Control.InvokeOnClick event that is also protected that you can
call to invoke the OnClick event on any other control as long as you
have
reference to it.


Thanks.
But what if I need to invoke another event ?

Oleg Subachev


Mar 24 '06 #11

P: n/a

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.

How to perform this ?

Oleg Subachev


Derive a class of your own from Button that exposes a public method
("ClickMe", for example). Then on the form, use your class instead of the
standard Button class.

To add your button to the tool bar you'll have to compile it - I've not
fully experimented with how VS.Net cooperates with controls I've simply
included in the project where I want to use them - but it's not a big deal
to create your own control library, compile it separately, and add the
controls to the toolbar. Then you have them available for all your projects.
Just don't forget to ship the control assembly when you ship the
application. (Also - whenever you release the assembly be sure to change its
version number so you get the benefits of side-by-side execution.)

Chris

Use what talents you possess; The woods would be very silent if no birds
sang there except those that sang best. (William Blake)
Chris Trelawny-Ross
Mar 24 '06 #12

P: n/a
When I compile the error is that form does not contain a definition for
this.InvokeOnClick(button1, EventArgs.Empty);
is this version 2.0 how is it done in 1.1??

--
cindy
"Oleg Subachev" wrote:
There is Control.InvokeOnClick event that is also protected that you can
call to invoke the OnClick event on any other control as long as you have
reference to it.


Thanks.
But what if I need to invoke another event ?

Oleg Subachev

Apr 24 '06 #13

P: n/a
cindy,

InvokeOnClick is defined as protected methods on the level of the control
and is available on .NET1.x also.

Check your spelling and make sure that the class you are calling the method
derives from Control; Form does derive from this class so you shouldn't have
problems.
Keep also in mind that this method is not supported by compact framework. If
you use CF you need to find out another way of doing it.
"cindy" <cm****@nospam.nospam> wrote in message
news:F9**********************************@microsof t.com...
When I compile the error is that form does not contain a definition for
this.InvokeOnClick(button1, EventArgs.Empty);
is this version 2.0 how is it done in 1.1??

--
cindy
"Oleg Subachev" wrote:
> There is Control.InvokeOnClick event that is also protected that you
> can
> call to invoke the OnClick event on any other control as long as you
> have
> reference to it.


Thanks.
But what if I need to invoke another event ?

Oleg Subachev

Apr 25 '06 #14

P: n/a
what I have is a parent window and modal
I am trying to close the modal by a programatic click of cmd button with js
tried to it

public class frmAuthor : System.Web.UI.Page
{
//parent form with field for data entry has upload button,
//user clicks upload button first client script opens modal window
//user action in modal window causes the close of the modal window
//execution returns to parent in the CmdUpload button onclick event.
//this side is all good
protected void AddClientScript()
{
const string scriptName = "CALL_POPUP";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language =\"javascript\">");
sb.Append("\nfunction getData(checkBoxListId)");
sb.Append("\n{");
sb.Append("\nvar mystr = window.showModalDialog('PopFrame.aspx');");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
}
private void Page_Load(object sender, System.EventArgs e)
{
AddClientScript();
CmdUpload.Attributes["onClick"] = "getData('checkBoxListTest')";
}
private void InitializeComponent()
{
this.CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);
}

private void CmdUpload_Click(object sender, EventArgs e)
{
try {
//code to upload form data
}
}

}
the modal
public class frmAttach : System.Web.UI.Page
{ //modal window is opened in iframe
//form has fields and 2 buttons, one is CmdUpload where data is processed
//after processing I want to programmatically invoke click of the cmdClose
//to call the javascript which closes the window.
//manually clicking the button works I want to automate this.
private void Page_Load(object sender, System.EventArgs e)
{ AddClientScript(); }
private void InitializeComponent()
{ CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);}
protected void AddClientScript()
{
string scriptName = "FIND_SELECTED_ITEM";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language=\"javascript\">");
sb.Append("\nfunction Item_Selected()");
sb.Append("\n{");
sb.Append("\nwindow.close();");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
CmdClose.Attributes["OnClick"] = "Item_Selected();return false;";
}
private void CmdUpload_Click(object sender, EventArgs e)
{
//code to upload form data

//in this forum there is a thread on programmatically
//invoking the onclick method and they say to use code below
//but it does not compile for me I get no definition for InvokeOnClick
//this is not compact application
this.InvokeOnClick(this.CmdClose, EventArgs.Empty);
}
}
}

--
cindy
"Stoitcho Goutsev (100)" wrote:
cindy,

InvokeOnClick is defined as protected methods on the level of the control
and is available on .NET1.x also.

Check your spelling and make sure that the class you are calling the method
derives from Control; Form does derive from this class so you shouldn't have
problems.
Keep also in mind that this method is not supported by compact framework. If
you use CF you need to find out another way of doing it.
"cindy" <cm****@nospam.nospam> wrote in message
news:F9**********************************@microsof t.com...
When I compile the error is that form does not contain a definition for
this.InvokeOnClick(button1, EventArgs.Empty);
is this version 2.0 how is it done in 1.1??

--
cindy
"Oleg Subachev" wrote:
> There is Control.InvokeOnClick event that is also protected that you
> can
> call to invoke the OnClick event on any other control as long as you
> have
> reference to it.

Thanks.
But what if I need to invoke another event ?

Oleg Subachev


Apr 25 '06 #15

P: n/a
cindy,

this is a web application. This doesn't apply for web applications.
--

Stoitcho Goutsev (100)

"cindy" <cm****@nospam.nospam> wrote in message
news:E5**********************************@microsof t.com...
what I have is a parent window and modal
I am trying to close the modal by a programatic click of cmd button with
js
tried to it

public class frmAuthor : System.Web.UI.Page
{
//parent form with field for data entry has upload button,
//user clicks upload button first client script opens modal window
//user action in modal window causes the close of the modal window
//execution returns to parent in the CmdUpload button onclick event.
//this side is all good
protected void AddClientScript()
{
const string scriptName = "CALL_POPUP";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language =\"javascript\">");
sb.Append("\nfunction getData(checkBoxListId)");
sb.Append("\n{");
sb.Append("\nvar mystr = window.showModalDialog('PopFrame.aspx');");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
}
private void Page_Load(object sender, System.EventArgs e)
{
AddClientScript();
CmdUpload.Attributes["onClick"] = "getData('checkBoxListTest')";
}
private void InitializeComponent()
{
this.CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);
}

private void CmdUpload_Click(object sender, EventArgs e)
{
try {
//code to upload form data
}
}

}
the modal
public class frmAttach : System.Web.UI.Page
{ //modal window is opened in iframe
//form has fields and 2 buttons, one is CmdUpload where data is processed
//after processing I want to programmatically invoke click of the cmdClose
//to call the javascript which closes the window.
//manually clicking the button works I want to automate this.
private void Page_Load(object sender, System.EventArgs e)
{ AddClientScript(); }
private void InitializeComponent()
{ CmdUpload.Click += new System.EventHandler(this.CmdUpload_Click);}
protected void AddClientScript()
{
string scriptName = "FIND_SELECTED_ITEM";
StringBuilder sb = new StringBuilder("");
sb.Append("\n<script language=\"javascript\">");
sb.Append("\nfunction Item_Selected()");
sb.Append("\n{");
sb.Append("\nwindow.close();");
sb.Append("\n}");
sb.Append("\n</script>");
if(!IsClientScriptBlockRegistered(scriptName))
RegisterClientScriptBlock(scriptName,sb.ToString() );
CmdClose.Attributes["OnClick"] = "Item_Selected();return false;";
}
private void CmdUpload_Click(object sender, EventArgs e)
{
//code to upload form data

//in this forum there is a thread on programmatically
//invoking the onclick method and they say to use code below
//but it does not compile for me I get no definition for InvokeOnClick
//this is not compact application
this.InvokeOnClick(this.CmdClose, EventArgs.Empty);
}
}
}

--
cindy
"Stoitcho Goutsev (100)" wrote:
cindy,

InvokeOnClick is defined as protected methods on the level of the control
and is available on .NET1.x also.

Check your spelling and make sure that the class you are calling the
method
derives from Control; Form does derive from this class so you shouldn't
have
problems.
Keep also in mind that this method is not supported by compact framework.
If
you use CF you need to find out another way of doing it.
"cindy" <cm****@nospam.nospam> wrote in message
news:F9**********************************@microsof t.com...
> When I compile the error is that form does not contain a definition for
> this.InvokeOnClick(button1, EventArgs.Empty);
> is this version 2.0 how is it done in 1.1??
>
> --
> cindy
>
>
> "Oleg Subachev" wrote:
>
>> > There is Control.InvokeOnClick event that is also protected that you
>> > can
>> > call to invoke the OnClick event on any other control as long as you
>> > have
>> > reference to it.
>>
>> Thanks.
>> But what if I need to invoke another event ?
>>
>> Oleg Subachev
>>
>>
>>


Apr 25 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.