Failed to load viewstate when trying to update/cancel on gridview?
<p><strong>Background</strong></p>

<p>I have a <code>GridView</code> that builds a table using an <code>ObjectDataSource</code>. This source uses web services for both the select and update segments. Under the edit section, when clicked, has a <code>DropDownList</code> appear for the two columns that need to be editable. The <code>DropDownList</code>s both use separate <code>ObjectDataSource</code>s storing WebServices that obtain the values meant to be stored in this <code>DropDownList</code>. </p>

<p><strong>Currently working</strong></p>

<p>At the moment, all of the above works. When I load the page, the table comes up with the proper data. When I click on the edit button, the two <code>DropDownList</code>s come up with the proper data stored in them from the WebService.</p>

<p><strong>The Problem</strong></p>

<p>When I select an option to update the DB or when I select cancel, the page throws an error and fails. I am not entirely sure why this happens, other than that it has to do with the binding not being handled correctly. I would like to know how to bind the values obtained from the <code>DropDownList</code> to be used when updating the database?</p>

<p>Below you will find what i have tried so far:</p>

<pre><code>&lt;asp:GridView ID="GridViewHolder" 
                          runat="server" 
                          AllowPaging="True" 
                          AutoGenerateColumns="False" 
                          BackColor="Transparent" 
                          BorderColor="#999999" 
                          BorderStyle="Ridge" 
                          BorderWidth="3px" 
                          CellPadding="4" 
                          CellSpacing="2" 
                          DataSourceID="MachineDataSet" 
                          ForeColor="Black" 
                          HeaderStyle-HorizontalAlign="Center" 
                          HorizontalAlign="Center"  
                          RowStyle-HorizontalAlign="Center" Width="574px"
                          OnRowUpdating="GridViewHolder_Updating"
                          OnRowCancelingEdit="GridViewHolder_Canceling"
                          OnRowUpdated="GridViewHolder_Updated"
                          OnRowEditing="GridViewHolder_Editing"&gt;
                &lt;RowStyle BackColor="Transparent" HorizontalAlign="Center" /&gt;
                &lt;Columns&gt;
                    &lt;asp:BoundField DataField="ID" 
                                    HeaderText="ID" 
                                    SortExpression="ID" 
                                    Visible="False" /&gt;
                    &lt;asp:BoundField DataField="SiteName" 
                                    HeaderText="Site Name" 
                                    SortExpression="SiteName"
                                    ReadOnly="true" /&gt;
                    &lt;asp:BoundField DataField="Name" 
                                    HeaderText="Machine Name" 
                                    ReadOnly="true" 
                                    SortExpression="Name" /&gt;
                    &lt;asp:TemplateField HeaderText="Machine Type" SortExpression="MachineType"&gt;
                        &lt;EditItemTemplate&gt;
                            &lt;asp:ObjectDataSource ID="GetMachineType" 
                                                  runat="server" 
                                                  SelectMethod="GetMachineTypeList" 
                                                  TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService"&gt;
                                &lt;SelectParameters&gt;
                                    &lt;asp:Parameter Name="siteid" Type="String" /&gt;
                                &lt;/SelectParameters&gt;
                            &lt;/asp:ObjectDataSource&gt;
                            &lt;asp:DropDownList ID="MachineTypeDropDown" 
                                              runat="server" 
                                              AppendDataBoundItems="True" 
                                              DataSourceID="GetMachineType" 
                                              DataTextField="Name" 
                                              DataValueField="ID"                                               
                                              Height="21px" 
                                              Width="217px"&gt;
                                &lt;asp:ListItem Enabled="true" 
                                              Text="Select a Machine Type."&gt;
                                &lt;/asp:ListItem&gt;
                            &lt;/asp:DropDownList&gt;
                        &lt;/EditItemTemplate&gt;
                        &lt;ItemTemplate&gt;
                            &lt;asp:Label ID="Label1" 
                                       runat="server" 
                                       Text='&lt;%# Bind("MachineType") %&gt;'&gt;
                            &lt;/asp:Label&gt;
                        &lt;/ItemTemplate&gt;
                    &lt;/asp:TemplateField&gt;
                    &lt;asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel"&gt;
                        &lt;EditItemTemplate&gt;
                            &lt;asp:ObjectDataSource ID="GetMachineModel" 
                                                  runat="server" 
                                                  SelectMethod="GetMachineModelList" 
                                                  TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService"&gt;
                                &lt;SelectParameters&gt;
                                    &lt;asp:Parameter Name="siteid" Type="String" /&gt;
                                &lt;/SelectParameters&gt;
                            &lt;/asp:ObjectDataSource&gt;
                            &lt;asp:DropDownList ID="MachineModelDropDown" 
                                              runat="server" 
                                              AppendDataBoundItems="True" 
                                              DataSourceID="GetMachineModel" 
                                              DataTextField="Name" 
                                              DataValueField="ID"                                               
                                              Height="21px" 
                                              Width="217px"&gt;
                                &lt;asp:ListItem Enabled="true" 
                                              Text="Select a Machine Model."
                                              Value="NULL"&gt;
                                &lt;/asp:ListItem&gt;
                            &lt;/asp:DropDownList&gt;
                        &lt;/EditItemTemplate&gt;
                        &lt;ItemTemplate&gt;
                            &lt;asp:Label ID="Label2" 
                                       runat="server" 
                                       Text='&lt;%# Bind("MachineModel") %&gt;'&gt;
                            &lt;/asp:Label&gt;
                        &lt;/ItemTemplate&gt;
                    &lt;/asp:TemplateField&gt;
                    &lt;asp:CommandField ButtonType="Button" ShowEditButton="True" /&gt;
                &lt;/Columns&gt;
                &lt;FooterStyle BackColor="Transparent" /&gt;
                &lt;PagerStyle BackColor="Transparent" 
                            ForeColor="Black" 
                            HorizontalAlign="Left" /&gt;
                &lt;SelectedRowStyle BackColor="Transparent" 
                                  Font-Bold="True" 
                                  ForeColor="White" /&gt;
                &lt;HeaderStyle BackColor="Black" 
                             Font-Bold="True" 
                             ForeColor="White" 
                             HorizontalAlign="Center" /&gt;
         &lt;/asp:GridView&gt;
</code></pre>

<p>Where the problem seems to lie, is in this area below:</p>

<pre><code>&lt;asp:TemplateField HeaderText="Machine Type" 
    SortExpression="MachineType"&gt;
    &lt;EditItemTemplate&gt;
        &lt;asp:ObjectDataSource ID="GetMachineType" 
            runat="server" 
            SelectMethod="GetMachineTypeList" 
            TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService"&gt;
            &lt;SelectParameters&gt;
                &lt;asp:Parameter Name="siteid" Type="String" /&gt;
            &lt;/SelectParameters&gt;
        &lt;/asp:ObjectDataSource&gt;
        &lt;asp:DropDownList ID="MachineTypeDropDown" 
            runat="server" 
            DataSourceID="GetMachineType"
            DataTextField="Name"
            DataValueField="ID"                                               
            Height="21px" 
            Width="217px"
            AppendDataBoundItems="true"&gt;
            &lt;asp:ListItem Enabled="true"
                Selected="True"
                Text="Select a Machine Type."&gt;                                 
            &lt;/asp:ListItem&gt;
        &lt;/asp:DropDownList&gt;
    &lt;/EditItemTemplate&gt;
    &lt;ItemTemplate&gt;
        &lt;asp:Label ID="Label1" 
            runat="server" 
            Text='&lt;%# Bind("MachineType") %&gt;'&gt;
        &lt;/asp:Label&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:TemplateField&gt;
&lt;asp:TemplateField HeaderText="Machine Model" 
    SortExpression="MachineModel"&gt;
    &lt;EditItemTemplate&gt;
        &lt;asp:ObjectDataSource ID="GetMachineModel" 
            runat="server" 
            SelectMethod="GetMachineModelList" 
            &lt;asp:TemplateField HeaderText="Machine Type" SortExpression="MachineType"&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:ObjectDataSource ID="GetMachineType" 
                                              runat="server" 
                                              SelectMethod="GetMachineTypeList" 
                                              TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService"&gt;
                            &lt;SelectParameters&gt;
                                &lt;asp:Parameter Name="siteid" Type="String" /&gt;
                            &lt;/SelectParameters&gt;
                        &lt;/asp:ObjectDataSource&gt;
                        &lt;asp:DropDownList ID="MachineTypeDropDown" 
                                          runat="server" 
                                          AppendDataBoundItems="True" 
                                          DataSourceID="GetMachineType" 
                                          DataTextField="Name" 
                                          DataValueField="ID"                                               
                                          Height="21px" 
                                          Width="217px"&gt;
                            &lt;asp:ListItem Enabled="true" 
                                          Text="Select a Machine Type."&gt;
                            &lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:Label ID="Label1" 
                                   runat="server" 
                                   Text='&lt;%# Bind("MachineType") %&gt;'&gt;
                        &lt;/asp:Label&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel"&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:ObjectDataSource ID="GetMachineModel" 
                                              runat="server" 
                                              SelectMethod="GetMachineModelList" 


TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService"&gt;
                            &lt;SelectParameters&gt;
                                &lt;asp:Parameter Name="siteid" Type="String" /&gt;
                            &lt;/SelectParameters&gt;
                        &lt;/asp:ObjectDataSource&gt;
                        &lt;asp:DropDownList ID="MachineModelDropDown" 
                                          runat="server" 
                                          AppendDataBoundItems="True" 
                                          DataSourceID="GetMachineModel" 
                                          DataTextField="Name" 
                                          DataValueField="ID"                                               
                                          Height="21px" 
                                          Width="217px"&gt;
                            &lt;asp:ListItem Enabled="true" 
                                          Text="Select a Machine Model."
                                          Value="NULL"&gt;
                            &lt;/asp:ListItem&gt;
                        &lt;/asp:DropDownList&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:Label ID="Label2" 
                                   runat="server" 
                                   Text='&lt;%# Bind("MachineModel") %&gt;'&gt;
                        &lt;/asp:Label&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
</code></pre>

<p><strong>Updated for bounty</strong></p>

<p>My overall problem is: using the gridview, I am unable to get my edit, update, cancel buttons to work. So what I would like to also know since I am throwing a bounty on this is: how can I get these events to work properly using <code>ObjectDataSource</code>s?</p>

<p>I already know that the web services work properly, I just do not know how to load the necessary parameters with the right data-values from the table. Any help or suggestions are greatly appreciated.</p>

<p>Error thrown at the moment:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>I am unsure as to how to get the update and cancel events to fire correctly, so I have been trying to use the <code>OnRow*</code> event handlers, but none of these work or fire, even when I set up a break point on a method just to see if the event will fire.</p>

<p><strong>Update2</strong></p>

<p>So as requested here is what i have for the code behind dealing with the the events i had thought might fire, (<strong>note</strong>: i've tried this with almost all of the other events). When i would run a debugger attaching the page to an asp.net process and click on the update button, cancel button, or edit button i would expect the page to go to the breakpoint, however this does not happen.</p>

<p><strong>Note:</strong> I also know that the code behind is most likely incorrect, but i didn't want to fix any of that until i knew which event would be the right one that fires for update and cancel buttons.</p>

<pre><code>    protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
{

    int machineid;
    string machineTypeid;
    string machineModelid;

    GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex];
    Label id = (Label)row.FindControl("ID");
    DropDownList mType1 = GridViewHolder.Rows[e.RowIndex].FindControl("MachineTypeDropDown") as DropDownList;
    e.NewValues["MachineType"] = mType1.SelectedValue;
    DropDownList mType = (DropDownList)row.FindControl("Machine_Type");
    DropDownList mModel = (DropDownList)row.FindControl("Machine_Model");

    machineid = Convert.ToInt32(id);
    machineTypeid = mType.DataValueField.ToString();
    machineModelid = mModel.DataValueField.ToString();

    inputsService.UpdateMachineTypes(machineid, machineTypeid);
    inputsService.UpdateMachineModels(machineid, machineModelid);

}

protected void GridViewHolder_Updated(object sender, GridViewUpdatedEventArgs e)
{

}

/// &lt;summary&gt;
/// Handles the Click event of the cancel button under edit in the gridview control.
/// &lt;/summary&gt;
/// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;
/// &lt;param name="e"&gt;The &lt;see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/&gt; instance containing the event data.&lt;/param&gt;
protected void GridViewHolder_Canceling(object sender, GridViewCancelEditEventArgs e)
{
    //reset the edit index
    GridViewHolder.EditIndex = -1;
    //Bind data to GridViewHolder
    BindData();
}

protected void GridViewHolder_Editing(object sender, GridViewEditEventArgs e)
{

}
#endregion

#region Private Methods

private void BindData()
{
    GridViewHolder.DataSource = Session["MachineTable"];
    GridViewHolder.DataBind();
}

#endregion
</code></pre>

<p><strong>Update 3</strong>   </p>

<p>Okay you will find above the most recent attempt that i have done to try and get the update and cancel buttons to function properly in the gridview.</p>

<p>Any help or suggestions are greatly appreciated.</p>

<p>Thank you.</p>
