I have a gridview that is populated on the Page_Load event if !PostBack is true. The gridview has several column, each header template has a custom control that I'm using to sort the data in a more efficient way than what the ASP.NET gridview offers.
These custom controls have an OnSort event, I have tried defining them in the markup (was always null after postback) and hooking them up each time the page is loaded (now works).
Basically on a page postback, the ViewState for the custom controls in the gridview are being 'lost'. This is a problem as I have a Sort Direction field that toggles between Asc and Desc, and I rely heavily on the ViewState to remember the last value. Does anyone know what I might be doing wrong here? Or how I could change my code to keep the ViewState.
It was my expectation that by default, the Title property would be stored in ViewState, but this appears not to be the case - the exception in RenderBeginTag keeps getting thrown on postback.
Am I being thick? Do I need to explicitly store properties in ViewState?
I want to write own control which can contain other. And I want to define content of the control in the .aspx file where the control is defined. I have written such control. But now I have issue with ViewState of inner controls of my control. The following samle illustrates the issue: I defined two asp:DropDownList ID="ddl1" and ID="ddl2" One of them is inside of my control and other is outside. When posback is occurred asp:DropDownList ID="ddl1 loses state and becomes empty. How to say ASP.net to store state of inner controls defined by this way?
MyControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="DynamicControls.Controls.MyControl" %>
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
The above error occurs with gridview control , when used with template field columns with item template / and edit templates etc. updating a row within gridview, at the time of loadiong it loads perfectly fined, but when tried to modify an existing row within gridview control, then it produces the error. Make sure the grid view control has to be a child control which manages the child table info with in a master form. to reproduce the above problem the grid view must be loading the child records info of a master within the masterform, meaning both master data and gridview data all to be managed in one single form.
I had to replace gridview control with datagrid and then it worked perfectly fine. I wasted almost 4 days of my time., there is no way we could figure out the problem. here is the full info of the problem.
Server Error in '/' Application.
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Source Error:
[Code]....
Stack Trace:
[HttpException (0x80004005): Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.] System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +306 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +134 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +221 System.Web.UI.Page.LoadAllState() +312 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1661
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082
I want to know that when we create custom controls, then is there State Management through ViewState done automatically or we have to handle it explicitely ?Since i have created Custom Controls that is behaving as if it is not maintaining its state on PostBack.
I have a custom control in an aspx page that has a property named Size
public int Size { get { return Convert.ToInt32(ViewState["CreativeSize"]); } set { ViewState["CreativeSize"] = value; } }
This property is set in the aspx page to a value lets say 500 during a postback called by a Button control that is in the page (not in the custom control).
I also have a button inside the custom control that raises a postback like this
protected void btnUpload_Click(object sender, EventArgs e) { if (fuBannerfile.HasFile) if (fuBannerfile.FileContent.Length / 1024 > this.Size) ;//code here not important }
When this event is called the "this.Size" property is 0. I also noticed during debugging that during Page_Load of the page, if i access the property like this:
int size = customControlId.Size;
the property is set to 500. But after that when the debugger reaches the event inside the control the property is 0.
I have read your article from aspsnippets.com about custom paging it really work, but there are a problem that if there are more record in database (2000 to 5000) than  number of page index  will be goes in large number. like 1 to 200, any way which can divide it  like 12345678910...2000.
I have custom gridview that inherit from the regular gridview.
On of the features is that is has a built in page size dropdown in an extra header row.
Another feature is that this extra header row has a place holder where aditional controls can be added but here is where I have some problems.
When I add a control that is created outside the gridview to the gridviews builtin placeholder and tries to do anything that performs a postback the gridview dissepears, but appears again on a new post back.
Here is the code for the gridview:
[Code]....
And this is the way it is used in a usercontrol on page, the gridview is called gvList and the control that I try to add to its placeholder is the phrExtraHeaderControls:
[Code]....
What can be wrong, is the viewstate messed up in the postback so that the gridview can't be rendered?
I've got a GridView on my page which has some bound controls, one of which is a label (to display total price):
[Code]....
However this value is not being passed to my page. All I see is '£' symbol, no price which makes me think Viewstate["youpay"] is empty? I have enabled viewstate on the Page and Control tags.
There is a lot time involved in determining what cells get the linkbutton and I want to avoid going through that process on a postback when a user clicks one of the linkbuttons.How can I maintain these controls in the gridview after a user clicks a linkbutton and a postback is done?Right now the application has to go through the process all over again.
I have existing project written by someone else that utilizes custom User Control that among other things contains a GridView control.I'm working on implementing the bulk-edit function so that users can modify data in many rows at once and then update the database accordingly.My problem is that in all examples that I see in order to implement this functionality you need to Override the CreateRow method, at which point you are able to put it in Edit State. The issue is that since my custom control inherits from User Control and not from GridView this protected method is not available. If I change the inheritance to GridView, tons of other things break inside this code, so I'd like to avoid that.My question is, is there anyway to still override the CreateRow if I'm inheriting from UserControl class and not GridView.If this is not very clear, please let me know and I'll try to explain further. I really need help with this, as it is time sensitive and it's always hard to work with someone elses code,
I am creating a gridview dynamically in a custom control and adding it to a Panel. I set the EnableViewState = true. The problem is that on a page postback the GridView does not seem to retain its values. If I add a GridView through the UI then the viewstate is preserved. The problem seems to occur when it is put dynamically only.
I have a checkbox column in a Gridview that is added in the aspx and the other columns are added in the code behind. The checkbox is a choice to print the record or not. When the print button is pushed the page posts back and the checkboxes are gone.
I have read that you have to re-add the checkboxes on postback, but where do you do that and how do you retain the viewstate values - checked/not checked? I have tried everywhere - PreInit, PreLoad, PreRender... I can't get the viewstate values to appear anywhere. The checkboxes can be added just about anywhere but the vaules are set to "unchecked" by default... no values ever come back no matter where I re-add them.
I am using a GridView with an ObjectDataSource bound to a DataTable. I am using the GridView for displaying a report, NO editing is being done, just read only, however, I am sorting and paging. I need to turn off ViewState for the GridView to improve performance. After turning off the ViewState I start getting the error messages for both paging and sorting:The GridView 'gridView' fired event Sorting which wasn't handled.This is fine I just need to know how to implement the Sorting and Paging events correctly to leveage the ObjectDataSource. I would preffer not to remove the ObjectDataSource from use, especially if there is a way I can manually wire up the datasource correctly to continue to use the GridView as I had been when ViewState was on.Any help on getting the datasource wired up or a solution for handling the events in the context of the datasource would be great.
My datasource is an ado data table that I have convert to a new data table so that each row in the original table is now a column with 1 row in my new table. I then bind the new datatable and create dynamic template fields with a text box where I bind the values. My problem is, when I try and retrieve the values changed by the user in the rowcommand event, the template columns no longer exists.
Does anyone know how i can retrieve these values? I've read over and over that you have to rebind the grid on each postback when you use dynamic templates, but I'm not sure how to do that and retrieve the values entered in the text boxes on the client side.
I have changed the look and feel of the grid by modifying the cols, rows in the gridview. I need to save this in the gridview viewsate, so that it load the modified look and feel next time it loads from the viewsate.