显示消息框的命令字段

2024-05-14

我有一个像这样的命令字段,

<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?');";
    }
}

但我的索引超出范围,因为那里没有任何控制。SOURCE http://forums.asp.net/t/1045957.aspx/1

这是我的 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>

当我尝试回答时出错


如果删除 CommandField 位于网格视图的第二列

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        LinkButton link = (LinkButton)e.Row.Cells[4].Controls[2];
        if (link != null)
        {
            link.OnClientClick = "return confirm('Do you really want to delete?')";
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示消息框的命令字段 的相关文章

随机推荐