Solved

ASP.NET event handler for a DropDown control

Posted on 2015-02-05
1
209 Views
Last Modified: 2015-02-06
I am new to ASP.NET. Normally, when working with Windows Forms, you have events that trigger methods. For example, if a user clicks on a button, the btnRun_OnClick event is called, and the code control jumps to the first line in that method.

However, it appears that with the ASP.NET code below, when the user changes the selected item in a drop down control, a PostBack happens.

Question: Am I correctly assuming that in Windows Forms, events always call methods (Event Handlers), and in ASP.NET, events always are handled via PostBack? What if for the below code, I want to run some code in method Foo(), how would I do that?

<tr>
		                            <td class="ControlLayoutCell" colspan="2">
		                                <cc1:I1Label runat="Server" ID="lblCCPaymentProcessor" Text="Credit Card Payment Processor:" CssClass="LabelGeneric" Width="100%" />
		                            </td>
		                            <td class="ControlLayoutCell">
		                                <cc1:I1DropDownList runat="server" ID="cboCCPaymentProcessor" Width="100%" TabIndex="90" AutoPostBack="true" OnSelectedItemChanged="cboCCPaymentProcessor_SelectedItemChanged"></cc1:I1DropDownList>
		                            </td>
		                        </tr>

Open in new window

0
Comment
Question by:pzozulka
1 Comment
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
Comment Utility
Yes that is correct events are fired when a postback occurs whether this is because an event has been triggered by a control such as a dropdown list item being changed or a button being clicked.

The postback is occurring because you have set the AutoPostBack property to true for the dropdown list it will then call the method specified in the OnSelectedItemChanged property which is currently set to cboCCPaymentProcessor_SelectedItemChanged if you want to call foo() when a user changes the selected item I would put the foo() method call in the cboCCPaymentProcessor_SelectedItemChanged event handler in the code behind.

If you don't want foo() to be called when the dropdown list item is changed and instead when a button is clicked then remove the AutoPostBack=True from the dropdown list and then add a button and add the foo() method call to the button click event handler. By default all button clicks will cause a postback.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now