Background
I have a GridView
that builds a table using an ObjectDataSource
. This source uses web services for both the select and update segments. Under the edit section, when clicked, has a DropDownList
appear for the two columns that need to be editable. The DropDownList
s both use separate ObjectDataSource
s storing WebServices that obtain the values meant to be stored in this DropDownList
.
Currently working
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 DropDownList
s come up with the proper data stored in them from the WebService.
The Problem
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 DropDownList
to be used when updating the database?
Below you will find what i have tried so far:
<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">
<RowStyle BackColor="Transparent" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="ID"
HeaderText="ID"
SortExpression="ID"
Visible="False" />
<asp:BoundField DataField="SiteName"
HeaderText="Site Name"
SortExpression="SiteName"
ReadOnly="true" />
<asp:BoundField DataField="Name"
HeaderText="Machine Name"
ReadOnly="true"
SortExpression="Name" />
<asp:TemplateField HeaderText="Machine Type" SortExpression="MachineType">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineType"
runat="server"
SelectMethod="GetMachineTypeList"
TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService">
<SelectParameters>
<asp:Parameter Name="siteid" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="MachineTypeDropDown"
runat="server"
AppendDataBoundItems="True"
DataSourceID="GetMachineType"
DataTextField="Name"
DataValueField="ID"
Height="21px"
Width="217px">
<asp:ListItem Enabled="true"
Text="Select a Machine Type.">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("MachineType") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineModel"
runat="server"
SelectMethod="GetMachineModelList"
TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService">
<SelectParameters>
<asp:Parameter Name="siteid" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="MachineModelDropDown"
runat="server"
AppendDataBoundItems="True"
DataSourceID="GetMachineModel"
DataTextField="Name"
DataValueField="ID"
Height="21px"
Width="217px">
<asp:ListItem Enabled="true"
Text="Select a Machine Model."
Value="NULL">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2"
runat="server"
Text='<%# Bind("MachineModel") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="Transparent" />
<PagerStyle BackColor="Transparent"
ForeColor="Black"
HorizontalAlign="Left" />
<SelectedRowStyle BackColor="Transparent"
Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="Black"
Font-Bold="True"
ForeColor="White"
HorizontalAlign="Center" />
</asp:GridView>
Where the problem seems to lie, is in this area below:
<asp:TemplateField HeaderText="Machine Type"
SortExpression="MachineType">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineType"
runat="server"
SelectMethod="GetMachineTypeList"
TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService">
<SelectParameters>
<asp:Parameter Name="siteid" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="MachineTypeDropDown"
runat="server"
DataSourceID="GetMachineType"
DataTextField="Name"
DataValueField="ID"
Height="21px"
Width="217px"
AppendDataBoundItems="true">
<asp:ListItem Enabled="true"
Selected="True"
Text="Select a Machine Type.">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("MachineType") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Machine Model"
SortExpression="MachineModel">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineModel"
runat="server"
SelectMethod="GetMachineModelList"
<asp:TemplateField HeaderText="Machine Type" SortExpression="MachineType">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineType"
runat="server"
SelectMethod="GetMachineTypeList"
TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService">
<SelectParameters>
<asp:Parameter Name="siteid" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="MachineTypeDropDown"
runat="server"
AppendDataBoundItems="True"
DataSourceID="GetMachineType"
DataTextField="Name"
DataValueField="ID"
Height="21px"
Width="217px">
<asp:ListItem Enabled="true"
Text="Select a Machine Type.">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("MachineType") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel">
<EditItemTemplate>
<asp:ObjectDataSource ID="GetMachineModel"
runat="server"
SelectMethod="GetMachineModelList"
TypeName="Datamart.UI.Reporting.Web.FilteredReportInputsSvc.FilteredReportInputsService">
<SelectParameters>
<asp:Parameter Name="siteid" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="MachineModelDropDown"
runat="server"
AppendDataBoundItems="True"
DataSourceID="GetMachineModel"
DataTextField="Name"
DataValueField="ID"
Height="21px"
Width="217px">
<asp:ListItem Enabled="true"
Text="Select a Machine Model."
Value="NULL">
</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2"
runat="server"
Text='<%# Bind("MachineModel") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Updated for bounty
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 ObjectDataSource
s?
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.
Error thrown at the moment:
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.
I am unsure as to how to get the update and cancel events to fire correctly, so I have been trying to use the OnRow*
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.
Update2
So as requested here is what i have for the code behind dealing with the the events i had thought might fire, (note: 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.
Note: 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.
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)
{
}
/// <summary>
/// Handles the Click event of the cancel button under edit in the gridview control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param>
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
Update 3
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.
Any help or suggestions are greatly appreciated.
Thank you.