【发布时间】:2019-12-08 07:48:21
【问题描述】:
我正在试验 ASP.NET WebForms 和数据。我可以使用在 ASPX 中剪断的小代码将数据发布到页面,但我无法使用删除、编辑和查看单个记录的按钮。我相信这是因为我在代码 sn-p 中创建表格行时无法添加runat="server"。
<table id="TBL_Albums" class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
<th>Release Date</th>
<th>Price</th>
<th>Genre</th>
<th>Artist</th>
<th># of Tracks</th>
<th colspan="3"> </th>
</tr>
</thead>
<tbody>
<% For Each album In GetAlbums()
%>
<tr>
<td><%=album.AlbumID%></td>
<td>
<span class="text-primary font-weight-bold"><%=album.AlbumName %></span>
</td>
<td>
<img alt='<%=album.AlbumName %>' title='<%=album.AlbumName %>' style="width: 96px;" class="img-fluid" src="../Content/Images/DB/<%=album.ImagePath %>" />
<!-- Path broken for now. Will get fixed when I rearrange pages? -->
</td>
<td><%=album.ReleaseDate.ToString("MMMM yyyy") %></td>
<td><%=album.UnitPrice.ToString("c") %></td>
<td><%=album.Genre.GenreName %></td>
<td><%=album.Band.BandName %></td>
<td><%=album.TotalTracks %></td>
<td>
<asp:Button ID="BTN_Details" runat="server" CssClass="btn btn-block btn-lg btn-info" Text="Details" OnClick="BTN_Details_Click" />
</td>
<td>
<asp:Button ID="BTN_Edit" runat="server" CssClass="btn btn-block btn-lg btn-warning" Text="Edit" OnClick="BTN_Edit_Click" data-toggle="modal" data-target="#EditAlbum" />
</td>
<td>
<asp:Button ID="BTN_Delete" runat="server" CssClass="btn btn-block btn-lg btn-danger" Text="Delete" OnClick="BTN_Delete_Click" />
</td>
</tr>
<%
Next %>
</tbody>
</table>
当我运行项目并单击其中一个按钮时,我得到了这个异常:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.HtmlControls.HtmlTableRow'.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
我的编辑按钮代码如下所示:
Protected Sub BTN_Edit_Click(sender As Object, e As EventArgs)
Dim context As New MusicContext
ClientScript.RegisterStartupScript(Page.GetType(), "EditAlbum", "$('#EditAlbum').modal();", True)
Dim buttonDetails As Button = TryCast(sender, Button)
Dim row As HtmlTableRow = DirectCast(buttonDetails.NamingContainer, HtmlTableRow)
Dim img As HtmlImage = TryCast(row.Cells(2).Controls(0), HtmlImage)
LBL_AlbumToUpdate.Text = row.Cells(1).InnerText
HF_AlbumIDEdit.Value = row.Cells(0).InnerText
TB_EditAlbumName.Text = row.Cells(1).InnerText
IMG_AlbumImage.ImageUrl = img.Src
TB_EditAlbumReleaseDate.Text = CDate(row.Cells(3).InnerText).ToString("yyyy-MM")
TB_EditAlbumPrice.Text = CDec(row.Cells(4).InnerText)
row.Cells(5).InnerText = DDL_EditAlbumGenre.SelectedItem.Text
row.Cells(6).InnerText = DDL_EditAlbumBand.SelectedItem.Text
End Sub
【问题讨论】:
标签: asp.net vb.net visual-studio webforms