How do I add a confirmation dialog box for the "delete" column in the gridview control?

Source: Internet
Author: User

How do I add a confirmation dialog box for the "delete" column in the gridview control? There are different opinions on the Internet, and we have seen three solutions, which are summarized as follows:
1. inherit the button control in Web. Io and implement an ipostback interface for callback.CodeThe complexity is only what the author wants to know ......
2. In the rowdatabound event of the gridview, traverse all the child controls. If the commandname belongs to the linkbutton class and is "delete", call its atributes. add method to add an onclick event containing the confirm statement, that is

LB. Attributes. Add ( " Onclick " , " Return confirm ('Do you really want to delete this row? ') " );

This approach is inefficient and the code is very indecent.

The Code is as follows:

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
(Linkbutton) (E. Row. cells [5]. controls [0]). Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ')");
}
}

Cells [5] indicates the control of the sixth column in The gridview, and controls [0] indicates the position of the control set on this page, which is forcibly converted to the linkbutton type.

3,The most concise method currently known, Set the deletetext attribute

< Div ID = " De " Onclick = " Javascript: Return confirm ('Are you sure you want to delete it? ') " > Delete </ Div >

This is too strong, like one.

The problem is that the HTML code output by. NET is as follows:

< A Href = "Javascript :__ dopostback ('ctl00 $ contentplaceholder1 $ gridview1', 'delete $0 ')" Style = "Color: # 4a3c8c ;" > < Div ID = "De" Onclick = "Javascript: Return confirm ('Are you sure you want to delete it? ')" > Delete </ Div > </ A >

Why does the bool value returned by the onclick event of the inner <div> affect whether the statement of the outer <A> tag is executed?
After thinking about it, I used to delete tags directly written in ASP as follows:<AHref= "Deleteuser. asp? Id = xxx"Onclick= "Return confirm ('Are you sure you want to delete it? ')">Delete</A>

That is to say, The onclick event accepts a bool value, which determines whether the click event is triggered. If the click event is not triggered, the jump action contained in href does not take effect. Similarly, in the previous code, the <div> jump action of <A> takes effect only when the <div> click event is triggered first. If we select to assign a value of false to The onclick event of <div>, the click event of <div> is canceled, and the redirection of <A> is also canceled.

Then, a new problem arises. If the buttontype in commandfield is a button, this code will become invalid. After thinking about it, you can convert it into a template column.
Convert the field to a template, edit the template column, select the button for deletion, and set its onclientclick attributeReturnConfirm ('Are you sure you want to delete it? ')

You can.

 

========================================================== ====================================

Template Method:

In the gridview, we can directly add a commandfield deletion column to delete a row. However, in order to avoid accidental deletion due to misoperations, the operator is asked to confirm the deletion before deletion.

First, we add a template column to our gridview, as shown below:

<Asp: templatefield headertext = "delete" showheader = "false">
<Itemstyle forecolor = "red"/>
<Itemtemplate>
<Asp: linkbutton id = "btndelete" runat = "server" causesvalidation = "false" commandname = "delete"
TEXT = "delete"> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>

Next, we add onclientclick = "Return confirm ('Are you sure you want to delete this row? ') ", As follows:

<Asp: templatefield headertext = "delete" showheader = "false">
<Itemstyle forecolor = "red"/>
<Itemtemplate>
<Asp: linkbutton id = "btndelete" runat = "server" causesvalidation = "false" commandname = "delete"
TEXT = "delete" onclientclick = "Return confirm ('Are you sure you want to delete this row information? ') "> </ASP: linkbutton>
</Itemtemplate>
</ASP: templatefield>

When you click Delete, "Are you sure you want to delete it?" is displayed on the client first ?" Dialog Box. Generally, confirm the deletion.

 

========================================================== ======================================

In addition, span is used, which does not take a place (replace Div with span)

<Span onclick = "javascript: Return confirm ('Are you sure you want to delete it? ') "> Delete </span>

========================================================== ======================================

Replace text with image
In deletetext:

========================================================== ======================================

This is just a line of code.

buttonlinkfield. text = "

" Del \ " onclick =\ " javascript: return confirm ( ' are you sure you want to delete it? ' ) \ " Delete
"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.