Solved

ASP.NET event handler for a DropDown control

Posted on 2015-02-05
1
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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