Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > EditItemTemplate Error

Reply
Thread Tools

EditItemTemplate Error

 
 
Aaron Prohaska
Guest
Posts: n/a
 
      09-12-2003
Can anyone tell me why the following code is throwing this error:

System.InvalidCastException: Specified cast is not valid

Line 43: string itemID = e.Item.Cells[2].Text;
Line 44: string manufacturer =
((TextBox)e.Item.Cells[3].Controls[0]).Text;
Line 45: string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
Line 46: string shortBlurb =
((TextBox)e.Item.Cells[5].Controls[0]).Text;

aspx code

<aspataGrid
AutoGenerateColumns="False"
BorderStyle="Solid"
CellPadding="2"
CellSpacing="0"
Width="100%"
DataKeyField="SpecialID"
OnEditCommand="EditItemClick"
OnUpdateCommand="UpdateItemClick"
OnDeleteCommand="DeleteItemClick"
OnCancelCommand="CancelItemClick"
ID="viewSpecials"
Runat="server">
<HeaderStyle BackColor="#ffffff" Font-Bold="True"/>
<ItemStyle BackColor="#ffffff"/>
<AlternatingItemStyle BackColor="WhiteSmoke"/>
<Columns>
<asp:EditCommandColumn EditText="Edit" UpdateText="Save"
CancelText="Cancel"/>
<asp:ButtonColumn Text="Delete" CommandName="Delete"/>
<asp:BoundColumn DataField="SpecialID" ReadOnly="True"
Visible="False"/>
<asp:TemplateColumn HeaderText="Manufacturer">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Manufacturer") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="enterManufacturer" Text='<%#
DataBinder.Eval(Container.DataItem, "Manufacturer") %>' Width="100"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Model") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox1" Text='<%#
DataBinder.Eval(Container.DataItem, "Model") %>' Width="100"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Blurb">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ShortBlurb") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox8" Text='<%#
DataBinder.Eval(Container.DataItem, "ShortBlurb") %>' Width="200"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Color">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Color") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox2" Text='<%#
DataBinder.Eval(Container.DataItem, "Color") %>' Width="100"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Image File">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Image") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox3" Text='<%#
DataBinder.Eval(Container.DataItem, "Image") %>' Width="100"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Part Type">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "PartType") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox4" Text='<%#
DataBinder.Eval(Container.DataItem, "PartType") %>' Width="60"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grams">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Grams") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox5" Text='<%#
DataBinder.Eval(Container.DataItem, "Grams") %>' Width="50"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="MSRP">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "MSRP") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox6" Text='<%#
DataBinder.Eval(Container.DataItem, "MSRP") %>' Width="80"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Discount">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Discount") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox7" Text='<%#
DataBinder.Eval(Container.DataItem, "Discount") %>' Width="50"
Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="SalePrice" HeaderText="Sale Price"
ReadOnly="True"/>
<asp:BoundColumn DataField="Savings" HeaderText="Savings"
ReadOnly="True"/>
</Columns>
</aspataGrid>

Codebeind C#:

string itemID = e.Item.Cells[2].Text;
string manufacturer = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
string shortBlurb = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
string color = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
string image = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
string partType = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
string grams = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
string msrp = ((TextBox)e.Item.Cells[10].Controls[0]).Text;
string discount = ((TextBox)e.Item.Cells[11].Controls[0]).Text;

regards,

Aaron
 
Reply With Quote
 
 
 
 
Aaron Prohaska
Guest
Posts: n/a
 
      09-12-2003
I seem to have found my answer. I'm now using the following to fix it.

string manufacturer = ((TextBox)e.Item.FindControl("controlName")).Text;

thanks anyways,

Aaron

Aaron Prohaska wrote:
>
> Can anyone tell me why the following code is throwing this error:
>
> System.InvalidCastException: Specified cast is not valid
>
> Line 43: string itemID = e.Item.Cells[2].Text;
> Line 44: string manufacturer =
> ((TextBox)e.Item.Cells[3].Controls[0]).Text;
> Line 45: string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
> Line 46: string shortBlurb =
> ((TextBox)e.Item.Cells[5].Controls[0]).Text;
>
> aspx code
>
> <aspataGrid
> AutoGenerateColumns="False"
> BorderStyle="Solid"
> CellPadding="2"
> CellSpacing="0"
> Width="100%"
> DataKeyField="SpecialID"
> OnEditCommand="EditItemClick"
> OnUpdateCommand="UpdateItemClick"
> OnDeleteCommand="DeleteItemClick"
> OnCancelCommand="CancelItemClick"
> ID="viewSpecials"
> Runat="server">
> <HeaderStyle BackColor="#ffffff" Font-Bold="True"/>
> <ItemStyle BackColor="#ffffff"/>
> <AlternatingItemStyle BackColor="WhiteSmoke"/>
> <Columns>
> <asp:EditCommandColumn EditText="Edit" UpdateText="Save"
> CancelText="Cancel"/>
> <asp:ButtonColumn Text="Delete" CommandName="Delete"/>
> <asp:BoundColumn DataField="SpecialID" ReadOnly="True"
> Visible="False"/>
> <asp:TemplateColumn HeaderText="Manufacturer">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Manufacturer") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="enterManufacturer" Text='<%#
> DataBinder.Eval(Container.DataItem, "Manufacturer") %>' Width="100"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Model">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Model") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox1" Text='<%#
> DataBinder.Eval(Container.DataItem, "Model") %>' Width="100"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Blurb">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "ShortBlurb") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox8" Text='<%#
> DataBinder.Eval(Container.DataItem, "ShortBlurb") %>' Width="200"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Color">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Color") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox2" Text='<%#
> DataBinder.Eval(Container.DataItem, "Color") %>' Width="100"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Image File">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Image") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox3" Text='<%#
> DataBinder.Eval(Container.DataItem, "Image") %>' Width="100"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Part Type">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "PartType") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox4" Text='<%#
> DataBinder.Eval(Container.DataItem, "PartType") %>' Width="60"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Grams">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Grams") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox5" Text='<%#
> DataBinder.Eval(Container.DataItem, "Grams") %>' Width="50"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="MSRP">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "MSRP") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox6" Text='<%#
> DataBinder.Eval(Container.DataItem, "MSRP") %>' Width="80"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:TemplateColumn HeaderText="Discount">
> <ItemTemplate>
> <%# DataBinder.Eval(Container.DataItem, "Discount") %>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:TextBox ID="Textbox7" Text='<%#
> DataBinder.Eval(Container.DataItem, "Discount") %>' Width="50"
> Runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:BoundColumn DataField="SalePrice" HeaderText="Sale Price"
> ReadOnly="True"/>
> <asp:BoundColumn DataField="Savings" HeaderText="Savings"
> ReadOnly="True"/>
> </Columns>
> </aspataGrid>
>
> Codebeind C#:
>
> string itemID = e.Item.Cells[2].Text;
> string manufacturer = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
> string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
> string shortBlurb = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
> string color = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
> string image = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
> string partType = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
> string grams = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
> string msrp = ((TextBox)e.Item.Cells[10].Controls[0]).Text;
> string discount = ((TextBox)e.Item.Cells[11].Controls[0]).Text;
>
> regards,
>
> Aaron

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
DropDownList in DataGrid EditItemTemplate Return Error rsw8n ASP .Net Datagrid Control 2 11-28-2003 03:55 PM
CustomValidator in the EditItemTemplate of a DataGrid Dave ASP .Net 1 10-31-2003 07:10 AM
Datalist EditItemTemplate SStory ASP .Net 0 08-18-2003 11:01 PM
datagrid edititemtemplate for dates Trevor Hartman ASP .Net 0 07-16-2003 06:46 PM



Advertisments