Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

c# - how to add delete confirmation prompt for command field in detail view?

I want to prompt the user for confirmation when he tries to delete a record in a detail view? I have command filed in which showDeletebutton set to true.

I found how to do the confirmation for gridview, but how can I modify to match detail view?

Code:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    // loop all data rows
    foreach (DataControlFieldCell cell in e.Row.Cells)
    {
       // check all cells in one row
       foreach (Control control in cell.Controls)
       {
            // Must use LinkButton here instead of ImageButton
            // if you are having Links (not images) as the command button.
            ImageButton button = control as ImageButton;
            if (button != null && button.CommandName == "Delete")
                // Add delete confirmation
                button.OnClientClick = "if (!confirm('Are you sure " + 
                       "you want to delete this record?')) return;";
        }
    }
}
}

Anybody?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
     <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
        .....
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:TemplateField ShowHeader="False">
                 <ItemTemplate>
                    <asp:LinkButton  ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="New" Text="New"></asp:LinkButton>

                    <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>

                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView

This can be done easily on the markup code. I simply added the js code to the onClientClick property of the delete button:

OnClientClick="return confirm('Are you sure you want to delete this record');"

Or if you want do this in the code behind:

 protected void DetailsView1_DataBound(object sender, EventArgs e)
  {
    LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
    bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...