Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two buttons
1.button1
2.button2

one popup with one button

if i click any button i want to open popup that is common for both buttons.

and if i click button1 and that opens popup in that popup button i want button 1 method
and if i click button2 and that opens popup in that popup button i want button 2 method

how can do this

Thanks for advance
Posted

There is one flaw in your post: "popup which is common for both buttons" is not anything certain. Do you mean "the same window object"? If so, it's not a problem: you should have one window variable in outside content: first button (any of the two, whichever comes first) creates window and shows it, the second one just changes content of the same window.

From your post, it's unclear what do you want to do with the window when it is closed and what should cause the window closed. If you don't have to care about preserving some content in the popup window which could be modified by the user, there is no a problem. For example:
HTML
<html>

    <head>
        <script>

            var myPopup

            function Create() {
                myPopup = window.open("", "", "width=200, height=100");
            }
            function CreateShow(text) {
                if (!myPopup)
                    Create();
                else if (myPopup.closed)
                    Create();
                myPopup.document.write(text);
                myPopup.document.close();
            }

            function First() {
                CreateShow("first method");
	    } 

            function Second() {
                CreateShow("second method");
            } 

        </script>
    </head>

<body>
    <input type="button" value="Show First" onclick="First()"><br/>
    <input type="button" value="Show Second" onclick="Second()">
</body>

</html>


If the scenario is going to be more complex, please explain it.

[EDIT]

I also want to warn you: in many scenarios, using popups can be considered pretty bad. The users can be highly annoyed by them, block them by the browser-based plug-in blockers, and so on. This is not because the technique itself is so bad; this is because pop-ups have been heavily abused by shameless advertisers and extremely bad designers; and also because pop-ups take window control out of user's control. Be careful with pop-ups, in many situations, they are best avoided.

—SA
 
Share this answer
 
v2
Simply call the events for both buttons. Did you even try to Google it or find it at MSDN?

Button code would be,

C#
// create new instance of buttons
Button button1 = new Button();
Button button2 = new Button();
// add the content
button1.Content = "Button 1";
button2.Content = "Button 1";
// add the click handler
button1.Click += Button1_Click;
button2.Click += Button2_Click;


The above code would generate the buttons if you don't have then. Otherwise if you have then settled, append the click handler to them using the controlName.Click += Event_Name. += would do the work for you.

Now you can create the events using the very name that you gave inside the C# code for the buttons.

I would create only one event handler, you can create the second one on your own. And they'll work. But I would still recommend you, since they're both the very same events, don't create two methods. Use one function and call it from both buttons but check for which button you're getting the call from. Writing the same code twice is against the rule of "Don't Repeat Yourself".

C#
public void Button1_Click (object sender, EventArgs e) {
   // write the code to execute when button is clicked
   // following is the popup!
   MessageBox.Show("Button was clicked!");
}


This would be enough to work around with this task.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Aug-14 4:25am    
Even though there are some good point in this post, I'm sorry to say that you completely ignored the main problem of the question: popup "that is common for both buttons". Even though the whole question has a lot of unclear aspects, OP made this main problem clear: popup. Unfortunately, without addressing that pop-up and access to it, your answer tells pretty much nothing, no matter how correct your statements might be. I hope you'll understand my point.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900