我有一个这样的命令字段,
<asp:CommandField ShowEditButton="true" ShowDeleteButton="True" ItemStyle-Width="10px" />
现在删除时我想显示一个确认框,但不知道该怎么做。
抱歉,我不想使用模板字段。
也许我可以以某种方式使用 JS 来显示消息,或者在这个代码后面的方法中,
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
我试过这个,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//In this sample, there are 3 buttons and the second one is Delete button, that's why we use the index 2
//indexing goes as 0 is button #1, 1 Literal (Space between buttons), 2 button #2, 3 Literal (Space) etc.
((Button)e.Row.Cells[0].Controls[2]).OnClientClick = "return confirm('"Do you really want to delete?');";
}
}
但我让索引超出范围,因为那里没有任何控制。资源
这是我的 GridView 的完整代码,我更改了列名和其他文本,所以不要混淆,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="UserID" AutoGenerateColumns="false"
CellPadding="5" OnRowDataBound="GridView1_RowDataBound" Width="800px" AllowPaging="True"
PageSize="5" GridLines="Horizontal" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField ItemStyle-Width="8px" ControlStyle-Width="8px">
<ItemTemplate>
<a href="javascript:switchViews('div<%# Eval("UserID") %>', 'one');">
<img id="imgdiv<%# Eval("UserID") %>" alt="Click to show/hide orders" border="0"
src="/_layouts/g1.png" />
</a>
</ItemTemplate>
<AlternatingItemTemplate>
<a href="javascript:switchViews('div<%# Eval("UserID") %>', 'alt');">
<img id="imgdiv<%# Eval("UserID") %>" alt="Click to show/hide orders" border="0"
src="/_layouts/g1.png" />
</a>
</AlternatingItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" ReadOnly="true" HeaderText="User ID" ItemStyle-Width="120px" />
<asp:BoundField DataField="Name" HeaderText="User Name" ItemStyle-Width="350px" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="800px" />
<asp:CommandField ShowEditButton="true" ShowDeleteButton="True" ItemStyle-Width="10px" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("UserID") %>" style="display: none; position: relative;
left: 25px;">
<asp:GridView ID="GridView2" runat="server" Width="80%" AutoGenerateColumns="false"
DataKeyNames="TTTT" EmptyDataText="There isn't e.">
<Columns>
<asp:HyperLinkField HeaderText="TTTT Title" DataNavigateUrlFields="anotherfield"
DataTextField="TTTT" DataTextFormatString="{0:c}" Target="_blank" />
<asp:BoundField DataField="orDerDescription" HeaderText="orDerDescription" HtmlEncode="False" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
尝试回答时出错