Updating with using custom class collection not working
- by Risho
I've posted this yesterday on asp forum but no one replied so perhaps I'll have better luck here.
For some reason the OnUpdating method does not pull new values from the grid which is in edit mode. I've search and have come across several blogs and sites, some sugesting that an ObjectDataSource is required in order to use the "e.NewValue" construct others provide code to the contrary.
I don't get any errors - the variables in the code file would contain the old values rather then new ones.
I don't want to use the ODS way of manipulating the data. My delete method works but not the update one. Can you suggest what is wrong with the code? Here is what I've got:
aspx file:
<asp:GridView ID="gvBlack" runat="server" AutoGenerateColumns="False" OnRowUpdating="gvBlack_OnUpdating" OnRowEditing="gvBlack_RowEditing">
<Columns>
<%--<asp:BoundField DataField="Ident_Black" ReadOnly="True" visible="false" />--%>
<asp:TemplateField ItemStyle-Width="1px">
<EditItemTemplate>
<asp:Label ID="lblIdent_Black" runat="server" Text='<%# Bind("Ident_Black") %>' Visible="false" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model" >
<ItemTemplate>
<asp:Label ID="lblModel_Black" runat="server" Text='<%# Bind("Model_Black") %>' width="130px" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtModel_Black" runat="server" Text='<%# Eval("Model_Black") %>' width="100px" />
<asp:RequiredFieldValidator ID="rfvModel_Black" runat="server" ControlToValidate="txtModel_Black"
SetFocusOnError="true" ErrorMessage="*" ValidationGroup="CurrentMfg" ForeColor="Red" Font-Bold="true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" >
<ItemTemplate>
<asp:Label ID="lblDesc_Black" runat="server" Text='<%# Bind("Desc_Black") %>' width="200px" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDesc_Black" runat="server" Text='<%# Eval("Desc_Black") %>' width="170px" />
<span></span>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty" >
<ItemTemplate>
<asp:Label ID="lblQty_Black" runat="server" Text='<%# Bind("Qty_Black") %>' width="35px" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtQty_Black" runat="server" Text='<%# Eval("Qty_Black") %>' width="35px" />
<asp:RequiredFieldValidator ID="rfvQty_Black" runat="server" ControlToValidate="txtQty_Black"
SetFocusOnError="true" ErrorMessage="*" ValidationGroup="CurrentMfg" ForeColor="Red" Font-Bold="true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reorder<br />Limit">
<ItemTemplate>
<asp:Label ID="lblBlack_Reorder_Limit" runat="server" Text='<%# Bind("Black_Reorder_Limit") %>' width="35px" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBlack_Reorder_Limit" runat="server" Text='<%# Eval("Black_Reorder_Limit") %>' width="35px" />
<asp:RequiredFieldValidator ID="rfvBlack_Reorder_Limit" runat="server" ControlToValidate="txtBlack_Reorder_Limit"
SetFocusOnError="true" ErrorMessage="*" ValidationGroup="CurrentMfg" ForeColor="Red"
Font-Bold="true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server" Text='<%# Bind("Notes") %>' width="200px" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNotes" runat="server" Text='<%# Eval("Notes") %>' width="170px" />
<span></span>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="false" ValidationGroup="CurrentToner" />
</Columns>
</asp:GridView>
aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
LoadData_TonerBlack();
}
private void LoadData_TonerBlack()
{
dalConsumables_TonerBlack drTonerBlack = new dalConsumables_TonerBlack();
gvBlack.DataSource = drTonerBlack.GetListTonersBlack();
gvBlack.DataBind();
}
protected void gvBlack_OnUpdating(object sender, GridViewUpdateEventArgs e)
{
//GridView gvBlack = (GridView)sender;
//GridViewRow gvBlackRow = (GridViewRow)gvBlack.Rows[e.RowIndex];
int _Ident_Black = Convert.ToInt32(gvBlack.DataKeys[e.RowIndex].Values[0].ToString());
TextBox _txtModel_Black = (TextBox)gvBlack.Rows[e.RowIndex].FindControl("txtModel_Black");
TextBox _txtDesc_Black = (TextBox)gvBlack.Rows[e.RowIndex].FindControl("txtDesc_Black");
TextBox _txtQty_Black = (TextBox)gvBlack.Rows[e.RowIndex].FindControl("txtQty_Black");
TextBox _txtBlack_Reorder_Limit = (TextBox)gvBlack.Rows[e.RowIndex].FindControl("txtBlack_Reorder_Limit");
TextBox _txtNotes = (TextBox)gvBlack.Rows[e.RowIndex].FindControl("txtNotes");
string _updatedBy = Request.ServerVariables["AUTH_USER"].ToString();
dalConsumables_TonerBlack updateTonerBlack = new dalConsumables_TonerBlack();
updateTonerBlack.UpdateTonerBlack(_Ident_Black,
_txtModel_Black.Text,
_txtDesc_Black.Text,
Convert.ToInt32(_txtQty_Black.Text),
Convert.ToInt32(_txtBlack_Reorder_Limit.Text),
_txtNotes.Text,
_updatedBy);
gvBlack.EditIndex = -1;
LoadData_TonerBlack();
}
protected void gvBlack_RowEditing(object sender, GridViewEditEventArgs e)
{
gvBlack.EditIndex = e.NewEditIndex;
LoadData_TonerBlack();
}
Thanks in advance!
Risho