I have (1) dropdown list, (2) listboxes, (1) button on a page.
The listboxes start out empty, but it is filled via jquery/ajax on the dropdown list change.
When i click the button it causes a postback and every time it does the contents (options) within the listboxes are removed.
Questions:
1) WHY does this happen?
2) HOW do i keep the contents of the listbox from being removed? No options in the listboxes are selected
The content/state of form controls are stored in view state that is created on server side by asp.net engine. The elements added on client wont be accessible on server side. You can put the content data in the hidden field in javascript and get the data back on server side from the hidden field and assign to control like listbox, dropdown etc. This is how asp.net also works.
This happends because since you add the values with AJAX, they are not added to the ViewState, which is what asp.net uses to persist the state of the form. Your best option is probably to use AJAX to avoid a postback or load the values on your page load event only if it's a postback.
Related
I have a two column asp.net webform. The form is the on the left side and the gridview is on the right side. When a user clicks on a button, there is a method on the server side that will process a list of items. I want to show the results in the gridview on the right as each item in the list is processed. I also want to disable a bunch of controls on my page after the button click and enable them once the entire list is processed.
Thanks!
make use of client side/javascript.
e.g. run a client side loop on button click, take the first item from gridview process it, update the second gridview and repeat.
I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.
Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
User clicks a item on menu.
Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
Controls are created and placed into a Panel (named p).
Now controls are visible and available for editing (RadioButtons, TextAreas)
User clicks on button to save information from dynamic controls into a Database
Problems I'm facing
Controls dissapear on postback when clicking button, because they weren't created on Init.
Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next
The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.
Thank you in advance.
When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.
My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.
I have a ASP.net page with couple of tabpanels. When a user hits submit button when he is on the First tab I am showing the user the second tab control. Ontabindexchanged I am dynamically creating a usercontrol and passing some values to the control.
Now when the user is in the 2nd tab and if he navigates to the first tab, I need to pass some values to the first tab.
At the tabcontainer level how do I pass values between tabs?
I think what your problem is, you need to maintain viewstate of your dynamically created controls, otherwise there is no problem to pass values between tabs. like..
tab1TextBox1.text=tab2Textbox2.text (Since your controls are in AJAX tab panel, no need to find control from tab panel)
For Maintaining viewstate of your dynamically created user control, you need to create your control in page_init() event.
So i have an ASP.NET GridView control in which each column is a 'BoundField'. I did not create a TemplateField(ItemTemplate/EditItemTemplate), because i was planning on using JQuery to convert the BoundField row to a 'Edit' field by just converting the text in each cell to a textbox or textarea when that row was clicked. This is done all client side.
So far all the client side stuff works great. But I have a 'Save' button on each row of the grid that triggers an eventhandler on the server side. In that server side method, i try to grab values of that current row, but they are all the OLD cell values before I modified the fields/data using jquery/javascript.
When i iterate through each cell of the row, it's the same state as it was when it was rendered.
So what i'm trying to understand is. Why do all of this fancy javascript/jquery stuff when the state of the Grid stays exactly as it was when it was rendered when posting back to the server ?
someone please shed some light on this!!!
Thanks!
The GridView controls uses ViewState to persist its state between postbacks. When you do a postback it loads its previous state from the ViewState. Changes that you do via client-side Javascript are ignored. This is simply how the control works.
If you want to keep using the GridView control with inline editing, the easiest option would be to move to edit mode via a postback.
Another option is to have the fields in each row to begin with, only hidden. Then, you could show them via Javascript. You'll be able to access the values on the server.
Yet another option is not use a postback but an Ajax call, so that the grid is not re-rendered when you click the button. But that means you would have to manually collect the values from the grid via client-side code and pass it to the server.
Using .NET 1.1, I have a DataGrid that contains three columns for each row.
Row one has a label with some text.
Row three will always have two radio buttons and two checkboxes and those are in the HTML side of the page.
Row two will have dynamically generated controls (just textboxes for now) and there can be 1 or more per row. These will be used for user input.
There is a button on the page and when the user clicks the button I need to update the DataGrid’s source (my DataTable) with the new values from the user’s input.
The issue is the DataGrid seems to be losing the dynamically generated controls on PostBack. I can loop through each Item in the DataGrid and I can access the radio buttons and the checkboxes, but the textboxes aren’t there.
Any ideas?
Remember: every time a postback occurs you are working with a new instance of your page class. Dynamic controls added to the page during a previous postback went to the garbage collector as soon as the page for that postback rendered to the browser, along with the rest of that page instance. You need to re-create your dynamic controls on every postback.
you have to regenerate the controls. You should be able to get their values from the http request object