Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I need to have one button fire the click event of a second button

Posted on 2009-04-07
9
Medium Priority
?
443 Views
Last Modified: 2012-05-06
I've pasted the code needed to try this below.

1) put two buttons on an asp.net web form. I'm using VS2008.
2) in the page load event, assign the button2 postbackurl to be another .aspx page in your project.
3) run the project, click on button2, and watch the page navigate to the other .aspx page (no problems here).
4) add a little code in the button1 click routine to fire the click routine of button2.
5) run the project, click on button1, and it does not navigate to the other .aspx page.
6) put some code in the button2 click rountine
7) run the project, click button1, and the code in button2 click is run, but the program does not navigate to the .aspx assigned to button2 postbackurl.

Question: How do I make button1 operate button2 as if I had clicked on button2 directly?

Eric



<asp:Button ID="btnBut1" runat="server" Text="Button 1" />
    <asp:Button ID="btnBut2" runat="server" Text="Button 2" />
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btnBut2.PostBackUrl = "anypage.aspx"
    End Sub
 
    Protected Sub btnBut1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBut1.Click
        Dim t As Type = GetType(Button)
        Dim p As Object() = New Object(0) {}
        p(0) = EventArgs.Empty
        Dim m As MethodInfo = t.GetMethod("OnClick", BindingFlags.NonPublic Or BindingFlags.Instance)
        m.Invoke(btnBut2, p)
    End Sub
 
 
    Protected Sub btnBut2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBut2.Click
        Beep()
    End Sub

Open in new window

0
Comment
Question by:phc_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24093486
Shouldnt this :
m.Invoke(btnBut2, p)
be this:
m.Invoke(btnBut2_Click, p)
0
 
LVL 4

Expert Comment

by:rentonc
ID: 24094784
Apresto is right, make sure you use the exact subroutine name :)
0
 

Author Comment

by:phc_dev
ID: 24096354
with m.Invoke(btnBut2_Click, p), it is expecting an argument for sender and e, and using m.Invoke(btnBut2_Click(sender,e), p) produces a "Expression does not produce a value".  Remember, I am not necessarily trying to run the code with the _Click event, I am trying to get the PostBackURL of button2 to fire.
Thanks again,
Eric
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:phc_dev
ID: 24096375
the line above should read: "trying to run the code withIN the _Click event"
0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24100738
Hi there,
You can easily trigger the second button's click event, but the PostBackUrl will not be processed. The PostBackUrl is handled on the client-side in a onclick event on the button.

You can Response.Redirect using the PostBackUrl property. This is in C#.
protected void Page_Init(object sender, EventArgs e)
{
	Button2.PostBackUrl = "webform2.aspx";
}
 
protected void Button2_Click(object sender, EventArgs e)
{
	Response.Redirect(Button2.PostBackUrl);
}
 
protected void Button1_Click(object sender, EventArgs e)
{
	Button2_Click(sender, EventArgs.Empty);
}

Open in new window

0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24100751
If you don't need any server side code to run for button 1, then you can call button 2's client-side click event from button 1 to trigger the postback.
0
 

Author Comment

by:phc_dev
ID: 24101500
"If you don't need any server side code to run for button 1, then you can call button 2's client-side click event from button 1 to trigger the postback."
===================================

This is exactly what I need. Would you have an example of this for me?
Eric
0
 
LVL 6

Accepted Solution

by:
hehdaddy earned 2000 total points
ID: 24101896
Try something like this:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="document.getElementById('Button2').click();" UseSubmitBehavior="false" />
 
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

Open in new window

0
 

Author Comment

by:phc_dev
ID: 24102175
Thanks all for the help.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question