Asp.net MVC C# - DropDownList SelectedIndexChanged not firing - c#

I have a drop down list which is being populated with data from a database using a datasource in visual studio 2010. All the values are unique and I have set up an event to carry out an action when a new value is selected. When I select a new value in the drop down list the new value is selected but the event does not fire. I put a break point in the code to see if it was being executed which was not being hit.
I read about setting the drop down lists AutoPostBack to true but all this seems to do is refresh the page and set the list back to its original selected state (possible why the event is not firing?).
I also read something about enabling the ViewState as this could cause it to not fire, I tried putting EnableViewState = "true" at the top of the View to no avail. The EnableViewState property is also set to true on the drop down list.
I would be grateful if anyone has any insight into a solution?
Thanks

With Asp.Net MVC you do not have events and postback like Asp.Net.
You should use a form tag to handle the values of your drop down, because it's a classic HTML page. If you want to handle in a async way, use javascript connected to the onchange of your dropdown

I usually find this is caused by databinding. If code in Page_Load binds the dropdownlist without checking Page.IsPostBack (Or somewhere else in the pipeline before control events are handled) that will suppress the SelectedIndexChanged event.
Another possibility is that you haven't got OnSelectedIndexChanged="DropDownListId_SelectedIndexChanged" in the decalrative markup for the control, or in the #Page directive you don't have AutoEventWireup=true.
Can you post your code?
Just seen tag and #iridio's post. Is this really MVC ? I would'nt expect to see System.Web.UI.WebControls used in an MVC app, but I am no MVC expert.

Related

Save some values before button PostBack

Well I've got some textboxes dinamically created in a .cs file and I want to save/store their values when I click on Update button in a detailsview's edit mode, so I've got seted button's onclick event to a function to do this.
The problem is that the postback event occurs before the "OnClick" event, and then the textboxes' values are lost.
How can I save/store their values first?.
Please, help me with this.
Thanks in advance!
You can detect the page's first loading or post back(OnClick, SelectedItemIndexChanged like..) with the Page.IsPostBack property.
You should check this property value in the Page_Laoad event handler.
It helps to understand the ASP.NET page life cycle.(See here for more info.)
Basic events are as follows:
PreInit
Init
PreLoad
Load (controls are filled with postback values here)
Control events (OnClick happens here)
PreRender
Render
Unload
Usually during the "Init" event you want to create all of the controls, including any dynamic controls for the user.
During the Load event asp.net copies all of the post date back from the client to the controls in the page. But in order for this to work on the postback the same controls that were created on the initial render must exist in the same order with the same IDs.
Then in PreRender you can change the page state including adding and removing controls for the next post.
So to clarify here's how I would imagine your page flow:
OnInit - Construct your controls and populate with the initial values.
OnLoad - During postback your control will be populated with the users updated values.
OnClick - Use the event of the user's button click to then save the updated values.

How to implement Drop Down Box that loads element on select?

I'm creating a webpage and I'd like to implement a drop down list that loads a specific .jpg somewhere else on the same page when the user selects an option from aforementioned box. I'm using Visual Studio 2010 & I'm looking to implement this in pure HTML rather than CSS or C# code.. But if I have to use either of the 2 then fine.
This drop down list is filled in from a database. Just in case that matters.
Add a SelectedIndexChanged event on the drop down list and set AutoPostBack for the control to true.
In the SelectedIndexChanged event, programmatically add the image.
In addition to what user1948635 says, Yes it is through ASP and you can do this inside an UpdatePanel using the ScriptManager so that you don't have to refresh the whole page in the DropDownList's postback event and just refresh that specific part (.Net AJAX feature)

I need to change Multiview or place holder whenever I select different values in dropdown list

I am creating online booking page.
I got some example page you can see in this link
http://www.parkercorporate.co.uk/webbookercc/OneForm.asp
I need the same behaviour in my site whenever user selects value in pickup dropdown list in the right side I need to change the text values or view.
To do this behaviour how should I write code in ASP.NET and c#.
Thanks
Ranam
First, use an UpdatePanel so you don't see any visual postback.
Second, set the AutoPostBack property on your DropDownList to True.
Third, handle the DropDownList's SelectedIndexChanged event. This will now fire after every change in the DropDownList, because you enabled AutoPostBack.
In the event handler, you can now change the ActiveViewIndex of the MultiView, or show/hide controls, change textboxes, etc.
First, create different blocks (i.e divs) and give ID's to them.
Then, for each value in the dropbox, fill the divs w.r.t it.
Assign "OnChange" event to the dropdown, and in the handler, set visible or invisible to corresponding block.
On the other hand, If you want to do this on client side, I strongly suggest you to use JQuery, to prevent a lot of requests going to server.
Check this out with JQuery:
make visible div on dropdown select in jquery
If you won't fetch values from database, there is nothing complex about this page. Because it just writes hospital if you choose hospital etc. and changes control's visible state.
Even if you think you need db queries when dropdown's selected change event, actually at this page there is no need. Just store db values in hidden controls (like Hospital- for Hospital) and when dropdown selection changes, write hidden's value completely in javascript

Validating textbox content on blur by calling server-side method without affecting page behaviour

I have a textbox in one grid-view column where upon entering a particular value and losing focus of the textbox, should post to the server to get the text validated through a server-side method. If the entry is valid, a result set to fill rest of row cells would be returned, else the bgcolor of the textbox needs to be changed to Red.
I tried posting back through the obvious way, i.e. making the textbox's autopostback as true and wiring up a server-side OnTextChanged event handler to validate the entered value.
It is working with this setup, but is also affecting the remaining page controls behaviour. For example, if I click a button in some other grid after entering some text in the textbox, the OnTextChanged handler gets called thus preventing the button's click event, which I also wish to call to execute its functionality.
Kindly suggest what alternatives/corrections I should carry out to enable textbox content server-side validation plus making the other controls/updatepanels work as expected.
Me dumb. I tried everything from creating PageMethods, UpdatePanels to jQuery as hinted in lincolnk's reply. But the thing which finally worked was removing the Autopostback attribute from the textbox control.
After removing it the OnTextChanged event executed each time any server postback was initiated after changing the text. Thereby, executing both the OnTextChanged method and the other control's method. :)
I can think of a couple general approaches.
Create a web service with your validation routine and manually make the call (jQuery or whatever) when the text changes. Manually update the client display when you get a result.
Convert your gridview column to a templated field. Add a CustomValidator and wrap the textbox and validator in an UpdatePanel. Set the textbox to auto-postback and the UpdatePanel to conditional update so only the one you are using is refreshed.
Option 1 is kind of an end-around the typical asp.net process, and you would still want to validate everything on the server-side when the page is posted back.
Option 2 might have performance issues, since you're hitting the page again every time you do a validation.

Obtaining selected item, value or index from drop down list in formview after button press

I have a formview with an insertion template. In this template there is a drop down list with a number of items I want users to be able to select from. Beside the drop down list there is a button which I am using to add the selected item from the drop down list to a gridview which also exists in the insertion template.
My problem is that when I click the button to add the selected item from the drop down list the selected item, index or value from the drop down list are not available. I am using a OnClick event handler to catch the event from the button click but I suspect there is some kind of refresh of the template going on here which I am not understanding as nothing appears to be accessible from the button event handler. I don't believe a postback is occurring as I have disabled the CausesValidation property for my button.
It seems like you are binding your DDL on postbacks as well. If the ddl data isnt hardcoded and you have the call for your ddl databind function in the Page_Load, you need to call the function like this to ensure it is not bound on postback:
if(!IsPostBack)
{
BindDDL();
}
Otherwise we need more information to help you and please post your code.
If you are clicking an asp:Button with an OnClick eevent attached to it then you are posting back to the server, no matter whether or not CausesValidation is true or not.
Are you binding data to the DropDownList? If so and you are rebinding it on postback then you'll not have the selected item you're expecting.
Can you paste us the code here?
I would have to see the code, but it sounds like you are getting a rebind prior to pulling the chosen items. One way to examine this is to add a watcha nd then make sure you code in the various ASP.NET events and then watch.
Without seeing what you are doing in code, I cannot tell if this is a drag and drop anomoly or something you have coded in. But the symptoms you are describing fits the typcial bind in Page_Load scenario, which is what jmein was aiming at.
So it turns it out it was all my fault. The formview control I have is contained in a panel which did not have view state enabled. This was preventing the drop down list from remembering the item I had selected it seems.
Thanks all for your comments and suggestions.

Categories