Solved

ASP.NET event handler for a DropDown control

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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