Solved

ASP.NET event handler for a DropDown control

Posted on 2015-02-05
1
212 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

22 Experts available now in Live!

Get 1:1 Help Now