ASP.NET VS2008 C# - dropdown list - postback - c#

How can use a dropdown list without the autopostback=true.
The value on the server is not being changed according to the one selected from the client side. As I already stated I do not wish that for each dropdown I have the autopostback will trigger a post back.

Any time I have lost the value of the drop-down it is because I messed up and repopulated the drop down before handling the value change. For me, it has been drop-downs that I need to do something special with like add item attributes for Javascript, etc. This is data that needs to be added on every page load (aka data that is not persisted in the drop down like the names and values of each item). In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH!
Here is the page lifecycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Dollars to donuts that is what is happening. You are probably just reloading the items before you get to handling whatever postback event you are using to grab the value. If you are doing this and cannot get around this work flow, just save the selected index at the beginning of the logic that populates the drop-down, then set the selected index of the drop down with that value when done.

it'll be saved in the viewstate, so the value will be correct when you do eventually post back, and if you're really desperate to get the current value without a postback, javascript would be the way to do this.

Worst case you can grab the value right off the request object:
string selectedID = Request[DropdownControl.UniqueID];

You should make sure you are only filling the select box with options during the initial page load, and not again during the postback
if (!this.Page.IsPostBack) {
//fill select box here
}

Related

Where should I populate my dropdownlist so that it retains its selection?

I have seen posts which recommend that I populate the list within a if(!IsPostback) block.
However, I only generate the dropdown lists in response to postbacks so I dont think that will work for me.
What I currently do is databind the dropdownlists in Page_Init. However, when I inspect the list after a new selection is made (and therefore the autopostback has been triggered) using "inspect element" I see that the first item in the list is always "selected."
Please let me know how to keep the selection selected through a postback.
If you bind the control in Page_Init, then you'll lose your selection. Don't do that.
Bind the control once, then ViewState will retain the control data. But when the selection changes, that will be sent to the server, and the SelectedIndexChanged event will fire.
More generally, any properties you set on a control before the Render phase will be saved in ViewState. On the next Post Back, the control will load the contents of ViewState, and therefore will restore itself to the state it was in before the PostBack.
The control will then take note of any POSTed values that were sent on the PostBack. This includes changes in the selection, textbox contents, etc. The control will fire the appropriate somethingChanged event to indicate that there has been a change from the prior state of ViewState.

What is the best way to persist the usercontrol values without having to postback or to write server-side code?

Scenario: I am having a user control which has two drop down lists. The values of the second drop down is fetched and populated depending on the selection made in the first. So, the first drop down makes a postback and pulls the data for the second. The second dropdown, therefore, does not need any postbacks on any selection changes.
This user control is being used on a page in a gridview, and therefore it repeats several times depending on the number of records in the grid.
Problem: When I make a selection in any of the dropdowns, my previous selections in all the user control dropdowns are lost and reset to the first item in the dropdown list.
Solution that works for me: I am able to fix this behavior by setting the selections made in viewstate on SelectedIndexChanged event for the respective DropDownList. But I want to avoid the postback being made by the second dropdown as it does not do anything related to UI (changing or pulling additional data to update on screen) and from the user perspective, its a waste of time and thus irritating.
I have also tried using javascript to write the values I want to persist into hidden fields, but the hidden field values are empty when the page is refreshed.
So, What is the best way to persist the dropdown values without having to postback or to write server-side code?
Thanks in advance.
#Aniruddha , I guess you are filling the first dropdown at the page load of UserControl so write that code in !IsPostBack constraint, i.e.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
// Write your existing drop down bind or other code here.
}
}
Aniruddha you are probably binding you grid in the Page_Load and so in this case bind the grid under constraint of !IsPostBack as above
Hope this will help you !!

ASP.NET - Dynamic Control Values & Postback

I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right.
What should happen is:
User selects a radiobutton
Depending which button was
selection, a usercontrol is loaded
to specify some data and a viewstate
to say which enum type it's
equivalent to.
When they click
save, if the UserControl is just a
textbox input - the simplest), the
contents are read and saved, then
saved to the database with the
format(the radiobutton choice) so
they can be deserialized again
later.
The page posts back, and
the value and format are read from
the database, then the right control
is loaded.
The problem is - the first time the page posts back, it works. Every other postback it resets to the default value of the textbox.
I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it.
There was actually a bug in my original code which meant it would never have in the way I was using the modified version. Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored.
Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem.

How to save user entered data to session state from a dynamically created control?

using c# (asp.net)
i'm programmatically creating several drop down lists (random number).
the user selects a particular value from each list and i need to save the user selected value.
if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)
if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).
How do i save the user selected value from the drop down lists when a button is clicked?
i'm doing this in the code behind file.
As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.
You have to recreate all of the dropdowns on every postback.
Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.
The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow

Form post doesn't contain textbox data [ASP.NET C#]

I have several "ASP:TextBox" controls on a form (about 20).
When the form loads, the text boxes are populated from a database.
The user can change the populated values, and when they submit the form, I take the values posted to the server and conditionally save them (determined by some business logic).
All but 1 of the text boxes work as intended.
The odd box out, upon postback, does not contain the updated value that the user typed into the box.
When debugging the application, it is clear that myTextBox.Text reflects the old, pre-populated value, not the new, user-supplied value.
Every other box properly shows their respective user-supplied values.
I did find a workaround.
My solution was to basically extract the text box's value out of the Request.Form object: Request.Form[myTextBox.UniqueID], which does contain the user-supplied value.
What could be going on, here?
As I mentioned, the other text boxes receive the user-supplied values just fine, and this particular problematic text box doesn't have any logic associated to it -- it just takes the value and saves it.
The main difference between this text box and the others is that this is a multi-line box (for inputting notes), which I believe is rendered as an HTML "textarea" tag instead of an "input" tag in ASP.NET.
Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?
this happens to me all the time.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// populate text boxes from database
}
}
I would second Jonathan's response I would check your databinding settings.
If you do not need ViewState for the textboxes (i.e. no postback occurs until form submit) then you should disable it.
It sounds like you are not having problems saving the data (since you said you have managed to get the control to read the correct data back). Therefore, I would say the problem loads in your databinding code.
Remember the order of the page lifecycle, and where you are databinding your form.
PreInit
Init
Load
Your Control Event Handler
If you are reading the value in the Control Event handler, yet databinding in Init or Load, you'll have the old value.
The trick is to always databind in the correct event, or check for postback and don't databind then.
Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?
I had almost forgotten to check the ViewState, but ended up remembering to verify that it wasn't disabled before making my post here on SO. I even set EnableViewState="true" to make sure.
I did find the solution, and it coincided with most of the answers here. The form was indeed loading its data more than once (which is intentional behavior). I implemented some special code for this field, and all is well.
Thanks for your replies, all!

Categories

Resources