Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET event handler for a DropDown control

Posted on 2015-02-05
1
Medium Priority
?
241 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 2000 total points
ID: 40592728
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

963 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