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 itemcreated event of the gridview, traverse all controls. If it belongs to the linkbutton class and the commandname is "delete ",
Add an attribute. The specific method is to convert the forced type of the retrieved control to linkbutton, then call its atributes. Add method to add
The onclick event contains the confirm statement, that is
LB. Attributes. Add ( " Onclick " , " Return confirm ('Do you really want to delete this row? ') " );
3. As long 1 Buttonlinkfield. Text = " <Div id =" " Del" " Onclick =" " Javascript: Return Confirm ( ' Are you sure you want to delete it? ' )" " > Delete </div> "
That's OK!
Method 2: 1 Protected Void Gridview1_rowdatabound ( Object Sender, gridviewroweventargs E)
2 {
3 // Bind a data row
4 If (E. Row. rowtype = Datacontrolrowtype. datarow)
5 {
6 If (E. Row. rowstate = Datacontrolrowstate. Normal |
E. Row. rowstate = Datacontrolrowstate. Alternate)
7 {
8 (Linkbutton) E. Row. cells [ 6 ]. Controls [ 0 ]). Attributes. Add ( " Onclick " , " Javascript: Return confirm ('Are you sure you want to delete :" "" + E. Row. cells [1]. Text + " " " ? ') " );
9 }
10 }
11
12 }
4,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: < A Href = "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 you click
If the event is not triggered, the jump action contained in href does not take effect. Similarly, in the previous Code, <div> in <A>, only the <div>
Div>, the jump action of <A> takes effect. In The onclick event of <div>
False: The <div> click event is canceled, and the <A> redirect is 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 attribute Return Confirm ('Are you sure you want to delete it? ') You can.
Auto: http://www.cnblogs.com/dgqqcom/articles/764485.html