I have a gridview that is displaying XML data properly and a button
for editing in a textbox. I get the following error when I hit
"Update":
Unable to cast object of type
'System.Web.UI.WebControls.DataControlLinkButton' to type
'System.Web.UI.WebControls.TextBox'.
Here is my asp.net/html. Also I've included a page that IS updating
correctly below that. The only difference is I've used "template"
design for the first one that IS NOT working. Any clues?:
<%@ import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
If Not IsPostBack Then
bind()
End If
End Sub 'Page_Load
Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
DataBind()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e
As GridViewEditEventArgs)
Me.GridView1.EditIndex = e.NewEditIndex
bind()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal
e As GridViewUpdateEventArgs)
Dim originalId As String = GridView1.DataKeys
(e.RowIndex).Value.ToString()
Dim id As String = CType(GridView1.Rows(e.RowIndex).Cells
(0).Controls(0), TextBox).Text
Dim subject As String = CType(GridView1.Rows(e.RowIndex).Cells
(1).Controls(0), TextBox).Text
Dim year As String = CType(GridView1.Rows(e.RowIndex).Cells
(2).Controls(0), TextBox).Text
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("history.xml"))
Dim node As XmlNode = doc.SelectSingleNode(("//row[id='" +
originalId + "']"))
Dim nodeid As XmlNode = node.SelectSingleNode("id")
Dim nodesubject As XmlNode = node.SelectSingleNode("subject")
Dim nodeyear As XmlNode = node.SelectSingleNode("year")
nodeid.InnerText = id
nodesubject.InnerText = subject
nodeyear.InnerText = year
doc.Save(Server.MapPath("history.xml"))
bind()
End Sub
Private Sub bind()
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath("history.xml"))
Me.GridView1.DataSource = ds
GridView1.DataBind()
End Sub 'bind
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Timetable</title>
</head>
<body>
<form id="form1" runat="server">
<div align="left"
style="border-style: inset; border-color: white;
overflow: auto; font-size: X-Small; width: 85%;
font-family: 'Times New Roman'; height: 499px;
color: black; left: 79px; position: absolute;
top: 11%; background-color: #ccffff; padding-right:
10px; float: none; margin-left: 20px;
clip: rect(auto auto auto auto); text-indent: 5%;
text-align: left; padding-left: 10px;">
<body>
<br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="id"
OnRowEditing="GridView1_RowEditing"
OnRowCanceling="GridView1_RowCanceling"
OnRowUpdating="GridView1_RowUpdating" RowStyle-Width="20"
CellPadding="5" CellSpacing="2"
Width="766px"
ForeColor="Black" GridLines="None" BorderStyle="Solid" Font-
Bold="True"
HorizontalAlign="Center" RowHeaderColumn="id" Font-Size="Medium"
Font="Verdana"
BorderColor="Black">
<RowStyle Width="20px" BackColor="#CCFFCC" ForeColor="#333333" Font-
Bold="False"></RowStyle>
<EmptyDataRowStyle Font-Bold="False" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Event">
<ItemTemplate>
<%#Eval("id")%>
<%#Eval("year")%><br /><br />
<%# Eval("subject") %><br /><br /><hr />
</ItemTemplate>
<EditItemTemplate>
<%# Eval("subject") %>
<br /><br />
<asp:textbox Text='<%# Eval("subject") %>' runat="server" ID="txtEdit"
TextMode="MultiLine" Width="413px" Font-Names="Verdana" Font-
Size="Small" Height="200px">
</asp:textbox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCFF99" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<HeaderStyle BackColor="#0033CC" Font-Bold="True" ForeColor="White"
BorderStyle="None" />
<EditRowStyle BackColor="#FFCC99" Font-Bold="True" Wrap="true"/>
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<p align="center">
</p>
<p align="center">
</p>
</div>
</form>
</body>
</html>
______________________________________________________________________________
This one is updating correctly:
<%@ import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
DataBind()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As
GridViewEditEventArgs)
Me.GridView1.EditIndex = e.NewEditIndex
bind()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
Dim originalId As String = GridView1.DataKeys
(e.RowIndex).Value.ToString()
Dim id As String = CType(GridView1.Rows(e.RowIndex).Cells
(0).Controls(0), TextBox).Text
Dim subject As String = CType(GridView1.Rows(e.RowIndex).Cells
(1).Controls(0), TextBox).Text
Dim year As String = CType(GridView1.Rows(e.RowIndex).Cells
(2).Controls(0), TextBox).Text
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("history.xml"))
Dim node As XmlNode = doc.SelectSingleNode(("//row[id='" +
originalId + "']"))
Dim nodeid As XmlNode = node.SelectSingleNode("id")
Dim nodesubject As XmlNode = node.SelectSingleNode("subject")
Dim nodeyear As XmlNode = node.SelectSingleNode("year")
nodeid.InnerText = id
nodesubject.InnerText = subject
nodeyear.InnerText = year
doc.Save(Server.MapPath("history.xml"))
bind()
End Sub
Private Sub bind()
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath("history.xml"))
Me.GridView1.DataSource = ds
GridView1.DataBind()
End Sub 'bind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
bind()
End If
End Sub 'Page_Load
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Redirect("http://localhost:1810/WebSite6/
AddToTimeline.aspx")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Timetable</title>
</head>
<form id="form1" runat="server">
<div align="left"
style="border-style: inset; border-color: white;
overflow: auto; font-size: X-Small; width: 85%;
font-family: 'Times New Roman'; height: 499px;
color: black; left: 79px; position: absolute;
top: 11%; background-color: #ccffff; padding-right:
10px; float: none; margin-left: 20px;
clip: rect(auto auto auto auto); text-indent: 5%;
text-align: left; padding-left: 10px;">
<body borderwidth="20px" background="vignette.gif">
<br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="id"
ed
OnRowEditing="GridView1_RowEditing"
OnRowCanceling="GridView1_RowCanceling"
OnRowUpdating="GridView1_RowUpdating" RowStyle-Width="20"
CellPadding="5" CellSpacing="2"
Width="766px"
ForeColor="Black" GridLines="None" BorderStyle="Outset" Font-
Bold="True"
HorizontalAlign="Center" RowHeaderColumn="id" Font-Size="X-Small">
<RowStyle Width="20px" BackColor="#CCFFCC" ForeColor="#333333" Font-
Bold="True"></RowStyle>
<EmptyDataRowStyle Font-Bold="True" />
<Columns>
<asp:BoundField DataField="id" />
<asp:BoundField DataField="subject" />
<asp:BoundField DataField="year" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCFF99" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<HeaderStyle BackColor="#0033CC" Font-Bold="True" ForeColor="White"
BorderStyle="None" />
<EditRowStyle BackColor="#FFCC99" Font-Bold="True" Wrap="true"/>
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<p align="center">
</p>
<p align="center">
</p>
</div>
</form>
</body>
</html>