AutoPostBack ONCE on pageload - c#

I have a listview with a dropdown, with autopostback enabled.
The user can select yes - which postsback and brings up loads more dropdowns, no - which brings up a textbox, and blank - which does nothing.
As the original dropdownbox is databound it displays one of those values but at the moment always acts as if it was blank ie no other controls showing..
Can I make the page do ONE autopostback as soon as it's loaded without any user input to display the correct controls if it's a yes or no as opposed to blank?

try calling dropdownbox_selectedindexchanged(null,null) after binding in page load. it is just calling selectedindexchanged event programmatically. as far as i know you want to load controls based on dropdown selection when page first load and user haven't changed dropdown value. so this code will call it from code behind.

Related

How to set up page loading in ASP.Net for dynamic gridviews?

Here is how my code is set up.
The webpage itself works like this:
You have a drop down that allows you to select between different values. Lets call it dropdown A. Depending on the value selected, a gridview gets generated.
How generation works:
When a item in the dropdown A gets selected, inside the selectedIndexChanged is a method call to a function that creates a DataTable. That datatable gets binded to the gridview inside selectIndexChanged.
When it gets bounded, onRowBoundEvent gets called, and this is where I add all the necessary controls with unique IDs.
There is a button called saved that looks at the data in gridview, and saves it.
Problem: When I press save, there are no controls in the gridview for me to find.
I can use findControl since I know all the ids, but how do I make the controls stick around?
If I bind it in the page_load, how do I know what gridview to generate since if I select a value from dropdown A, page_load still fires before I can get a selection value from dropdown A, so I can't make a simple conditional statement based on the dropdown value.
I can't show any code, sorry. But this is more of a conceptual question I have.
I was able to figure this out on my own.
PrePage_Load during the project lifecycle has access to controls. Dropdown A in my example can be accessed in PrePage_Load, allowing me to get the necessary values and set them before Page_Load starts.
For controls in the gridview, I made it so the gridview does not automatically load from viewstate, and I rebuilt the gridview myself during page_load.

DropDownList after reset button c#

I have a c# dropdownlist inside a form. My goal is to hit a reset button and set the entire page back to the way it was when I first loaded the page. My textboxes are clear and everything is where it should be except my dropdownlist. It displays the last selected dropdownlist Item selected. I would like it to go back to the initial selection when I reload the page. How would I do this?
DropDownList1.ClearSelection();
or
DropDownList.Clear();
Doesn't work
DropDownList1.SelectedIndex= -1

Dynamically Created User Control's Dropbox Items from Database resets on postback

First off, I have managed to create a web application where my dynamically created user controls are recreated and repopulated with the correct information upon postback. I am not sure what my problem is, but i hope that you will be able to help me figure it out based on my situation:
On my page i enter the number of controls to be created into a hardcoded textbox (its on the aspx page) and click the okay butten. This in turn, creates the specified number of user controls dynamically using c# in the background.
So far the desired number of dynamic controls are in a table on the page.
Next...
I have 1 textbox and 4 dropboxes on each dynamic user control. When i type a company name into the textbox field and press enter or click away (on text changed event) it autoposts back and the textbox retains the company name that i have typed in.
Based on this string the dropboxes are populated from the database. Now when i select the desired items from the dropboxes and click on the save button (located outside of the dynamic controls, on the page) it does an insert to the database, but it turns out that upon this postback the indexes from the dropboxes have been reset and the wrong values get inserted.
The following pictures show firstly, how it should be and then how it is.
Basically the company name remains in the textbox of the dynamic control, but the information i choose from the dropbox resets to the first index.
It's hard to tell what happend without code, but this is a common mistake:
If you fill/create the dropdownlist controls in the page load event and you post back, the code will refill/recreate the controls. That's why you have to use something like If(!IsPostBack) in your page load event. Otherwise it will execute that code everytime you do a postback and actually just want to execute the code in your event handler for that button.
If you're dynamically creating the controls, make sure to do that in the Page_Init event. Dynamic controls have to be recreated on every postback. Their state is restored after the Page_Init (if it is a postback), so make sure to only set their values in Page_Load if you want to overwrite them.

choose a dynamic item in a dropdown without losing choice upon submit

Every time I click an ASP button on my page to select an item in my dropdown list, it is only choosing the first item in the list. It is as if every time I click a button the page refreshes and all variables reset. How do you choose an item from a dropdown when using dynamic data from a database?
This sounds indicative of populating the DropDownList in Page_Load. Is that the case? (I'm assuming you're using WebForms.) If you're populating the control in Page_Load then you'll want to wrap it in a conditional:
if (!IsPostBack)
{
// populate your controls from data
}
Otherwise, they'll get re-populated with each postback. When you click a button or perform some other action on the page which initiates a postback, Page_Load is called before the event handler. So in effect, this is happening:
User navigates to the page
Page_Load clears and re-populates the DropDownList
User chooses an item in the DropDownList
User clicks a button
Page_Load clears and re-populates the DropDownList
Button handler gets the current selection from the DropDownList, which is the default.
What do you mean by "click ASP button"?
If you are talking about the downarrow to open the dropdown list, then you need to set AutoPostback options to false if you don't want that opening the dropdown list triggers postback (that is on the other side required if you have any events related to the opening of your list).

using autopostback on dropdownlist that also has submit button

Have a dropdown list with autopostback set to 'yes' have another dropdown list box that will be populated based on the selection of the first dropdown. It works fine until I put a submit botton on the form. When I do it appears to not do the auto postback until the submit button is pushed. Can you have a submit button on a form that has a dropdown with autopostback active. thanks...
Sure you can.
Are you handling the autopostback event of the first dropdown in the code-behind file to populate the second dropdown? If so, your postback event should be firing when you change your selection in the first dropdown. Put a breakpoint in your code to assure that your event code is being called.
Sure you can. Are you seeing it not load the second dropdown? If so, check where you're handling that, as it may be in the wrong spot (if you're doing it with code) or you might have it wrapped in a !postback without thinking about it. Post more code and explanation for more help.

Categories

Resources