I am very new to Asp.Net.
Almost all the controls i have seen till now has ViewState.
So, my question is Are there any controls that don't have ViewState ? What are they.
I have googled, but din't find the correct solution.
Thank you !!
Every control has ViewState because that is where all attribute values are stored.
To avoid ViewState you can disable it on the control (EnableViewState=false) and make sure when you set any properties (like Text, Visible etc.) you do that yourself on every postback (so that ViewState is not needed) - so in events like Page_Load instead of Button_Click.
Related
I got a page with multiple dropdownlist. Each of them are fill with a lot of stock.I need the selected value of each of them on postback.
My problem is, at the end of the post back it take so much time to refresh(show) the page.
and I saw that the viewstate is fill with a lot of thing.
I try to disable viewstate for my dropdownlist, but that erase my value on postback.
I already have a conpressor for viewstate.
can you help me.
You can use Page Caching - based on OutputCache property of page
Link Sample : http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.100%29.aspx
Link Configuration : http://msdn.microsoft.com/en-us/library/ms178606%28v=vs.100%29.aspx
Page Caching or disable viewstate and add selected values to session and get that values at pageload again.
I am adding RadDock control and adding its Item Command events which require to be added in Pag_Init. And adding user controls to RadDock.
My problem is that when I have some post back for a specific control page_Init calls which reloads the controls and every control is re-binded every time. I want to avoid control creation every time. And want the specific control's post back should happen.
If I apply (!IsPostBack) condition in Page_Init then controls are not loaded and page gets empty.
I am stuck.
Any best practice or work around is acceptable.
Thanks in advance.
I don't know the specifics of Telerik's tools, but if they work like regular ASP.NET dynamic controls, you have to add the controls to the control tree on every page load. Populating the controls with data is distinct from adding them to the control tree. If the controls are added correctly so that they are placed in the same way in the control tree as on the previous page visit, and implement ViewState correctly (if needed) the runtime will populate them with data from the posted data and ViewState when a postback occurs.
I added IFrame to RadDock and then gave user control source to IFrame. Now it is working fine, Only the specific control's post back occurs.
Any way, Thanks Jonas
I'm reading up to get a better idea of how viewstate works in asp.net webforms and have been reading this article.
One part that I don't quite understand is the Stage 5 - Raise postback event, it says that this stage does not make use of any viewstate information to raise the events (ie. TextChanged).
I thought that the viewstate would be sent back with the page on the postback and after the control tree had been populated the values from the viewstate would then be loaded in and then after that the control would interrogate the new form values comparing them against the current ones loaded from viewstate in order to tell which Changed() events it needs to raise.
If this event doesn't interact with viewstate how can it tell whether a value has changed or if it is still the same from the previous load?
Daniel, you are correct in your assumption - view state is used to determine whether a change-related event needs to be raised. That includes things like the TextChanged event on the TextBox and the SelectedIndexChanged event on the DropDownList, among others.
If you haven't read this article yet, I highly recommend it: Truly Understanding View State. It's an informative write up by Dave Reed.
Thanks!
In the case of TextChanged events, it does look at the viewstate to determine if it gets raised or not - see the answer to question 6215046:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.ontextchanged.aspx
A TextBox control must persist some values between posts to the server for this event >> to work correctly. Be sure that view state is enabled for this control.
Try enabling ViewState for TextBox.
I have next situation:
I load dynamic controls during on init, and I do correct initialization.
I add dynamic control before postback
I don't add anything later in load
control is loaded and diplayed correctly
I press postback and nothing happens
Why I really don't know.. I tried everything. So control IS properly initialised. __EVENTTARGET shows the same path as the UniqueId of linkbutton that is firing it. All controls in tree have viewstate=true. So, I really don't know what this is not working.
Any idea? I am desperate.. I don't know.. if anyone could suggest me, if not solution, then just things I should check would be very good.
Is this problem just for this page or do you have other pages on the same site with the same problem?
I am assuming that you have the same problem on all pages.
It could be relate do javascript not being allowed. You could try to add the site to local intranet security are, then refresh the page.
Dynamic controls have to be added back to the control tree on each postback for the events to fire.
Dynamically created controls are not part of their container's viewstate, so setting it to TRUE wouldn't have any effect on the situation and are not evaluated until after the on_init call completes anyways.
I would wrap the logic that is populating these dynamic controls in with a conditional check for a postback if(!IsPostBack)
{ //Insert logic here }
If your dynamic controls take input from the user, or need access to their view state, then you would need to move this call to the Page_Load method as this is the point in the page's lifecycle where viewstate is first evaluated.
I have a page and a user control inside it.
I assign a value to viewstate in the control and do
Server.Transfer(Request.Url.AbsolutePath);
but when I check the value on control's Page_Load() event the assigned value doesn't exists (viewstate is empty, has not keys).
Why might this happened?
ViewState is stored on per-page basis. As soon as you do a Server.Transfer you're going to lose the ViewState from the page you're transferring from, which seems to be where your control is located? ViewState is only persisted during postback.
Number two, you might be explicitly turning off the ViewState for a page or a single control and not realizing it.