On my asp.net page, I have several DropDownLists.
I also have a Repeater.
In the ItemDataBound event I want to get the value of these DropDownLists, to change the data in the Repeater. The SelectedValue of these DropDownLists is empty.
But after the ItemDataBound, the Page_Load is executed. There I can get the value of these DropDownLists.
Is there a solution to get the value when the ItemDataBound is executed.
thanks!
Filip
You need to data-bind these drop-down lists in the Page.Load event.
There're a lot of web controls that get their state or other details during load life-cycle (I had these kind of problems long time ago).
NOTE: When I say "State" I'm not talking about ViewState.
And why don't you do that data-bind after the load event?
Can you get the drop down lists' selected values in the page PreInit event? If so, store them in view state and retrieve them from view state during the repeater's item data bound event.
If that doesn't work, try adding a selected index changed event to each drop down. When the drop downs change, set a view state variable that you can retrieve during the repeater's item data bound event. If you have values to which you are setting the drop downs during page load, such as when reading from a database, use those values to directly set the appropriate view state variables.
Related
I have a Master Page which contains a Dropdown, based on the Drop down selection I change the language of the entire site. Now, in one of the child pages I am using a repeater and binding some data to it. I am also using the ItemDataBound event of the repeater as well (I require a tooltip for one of the cells). I am trying to figure out a way when the master page Dropdown value is changed I should also fire the ItemDataBound event so that the data is displayed in the correct language. I am guessing it has to do something with playing around certain events. Any ideas or feedback will be appreciated.
ItemDataBound is only fired when data is bound to the control. You would have to rebind the data to the repeater in order for its ItemDataBound events to fire again.
Your other option is to write additional code to loop through all of the items in your repeater and perform the actions you usually perform in ItemDataBound.
Am using DropDownList controls in ASP.NET binding values from SQL table. Some control have only one value, some control have more than one value. The SelectedIndexChanged event is not fired which control have only one value.
I set AutoPostBack=true and 0th item as select. Even though that event is not fired for that control?
SelectedIndxChanged event fires only if you change your selection and your DDL has AUTOPOSTBACK true.
If you have only one value in DDL then you don't need this event , you can write your desire code after you bind the dropdown on server side itself.
If you are not binding DDL from server side you can write your code on or after Page_Load event there you'll get the selected index
SelectedIndexChanged only fires when the selection has changed. If there's only one item in the dropdown list, you can never get this to fire (there's nothing to change to).
That being said, you can add a blank item to the top of the list (index 0), with the following at the top of the code where you populate the dropdown list:
MyDDList.Items.Add("");
That way you CAN choose something even with your one item.
I am trying to update controls in the of a ListView control after handling the ItemCommand event.
My ListView displays line items of a purchase order in the as html table rows along with a TextBox to enter a new quantity and a Button to update the quantity. My ListView then displays sub-totals, discounts, and a grand total of the line items above in the as html table rows as well. On initial load, I set the values of the controls in the ListView's in the LayoutCreated event handler.
When a new quantity is entered and the button to update is clicked, I handled the event in the ItemCommand event handler. I update the quantity of the specific line item. I then re-bind my ListView to the underlying collection and call DataBind(). The problem is, LayoutCreated is not fired this time around, only on initial load.
My work around is to just pull those controls out of the and address them as static controls, but I like having them inside because my table markup can be fully contained in the and my can show cleanly without having to juggle the static controls' display properties.
Is what I am asking possible? Thank you for any help you can provide.
I'd reccomend handling the DataBound event of the ListView (instead of the LayoutCreated event), and setting the values there. That will get called everytime you re-bind the ListView, as well as when it loads for the first time, which (from your description) is what you want to do.
I have an ASCX control which is a special dropdownlist. I add that control dynamically to the page and fill it with data. This control has a postback that will change the contents of a second dynamically created standard dropdownlist.
When I change the selection on the first dropdown, the indexchanged fires and I get new data and attempt to place it in the second dropdownlist's items collection, by first clearing it then filling it with new data.
This works fine the first time a change the selection, but when I select a second time the following error is thrown:
The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.
I'm not adding or removing new controls in the fired event, only changing data. And again, it works the first time, but doesn't subsequent times.
If I disable stateview on the child control, then the control just doesn't get updated with data at all.
Hope this is clear enough :)
Thanks!
In your ASCX - are you re-creating both Dropdown lists each time in the control's OnInit event? If they are created dynamically, they need to be created each time there is a postback and this must be done before ViewState's values are deserialized in to the control tree.
I am having a listbox in ASP.net. I am populating the listbox values from another listbox in a page dynamically. During postbacks the values of output listbox are not persisted.
(while going to another page and come back to this page).
Please suggest some good answer. EnableViewstate = "true" is not working.
Are you doing anything in Page_Load that should be in a
if(!IsPostBack) {}
Initialization code in load needs to only be called when the page is first loaded, not on postbacks.
If you are going to another page and then coming back to this page, I think you need to preserve the information yourself in the Session and then restore it when you come back to the page.
The viewstate is only preserved as long as your on the same page doing postbacks.
As Lou Franco wrote
if(!IsPostBack) {}
You use this on the initial pagerequest to fill in the data. if you wish to preserve the data across pages using the session to store the values is the best bet.
preferably you fill in the data in your listbox before the SaveViewState event thats in PreInit as far as I recall.
Initialize the content of your controls in your Page's Init event (Page_Init). That way any values the user supplies are not overwritten by your defaults.
EnableViewState will just repopulate the output listbox with the values that it had when the page first rendered, since they're still the ones stored in the viewstate. The browser sends only the selected value in the postback, so there's no way for the server to know what other values you added on the client.
You can work around this by adding a hidden input to the page and populating it with the dynamic values when you update the listbox. Your page can then check that value during a postback and repopulate the list properly.
Changes made to the listbox on the client side are not persisted during a postback, you need to record that information in hidden fields and then configure the control during the page_load event to make the changes stick during the rest of the postback.