日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现对gridview删除行时弹出确认对话框的四种方法

發布時間:2023/12/2 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现对gridview删除行时弹出确认对话框的四种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現對gridview刪除行時彈出確認對話框的四種方法

在.net2.0中,實現對gridview刪除行時彈出確認對話框的四種方法 1,GridView中如何使用CommandField刪除時,彈出確認框?
在VS2005提供的GridView中我們可以直接添加一個CommandField刪除列:<asp:CommandField ShowDeleteButton="True" />,完后在它的RowDeleting事件中完成刪除。但在多半我們在做這種刪除操作時都需要先讓操作者再確認下,完后再進行刪除,以避免誤操作引起的誤刪除。
可以通過下面方法給GridView刪除前加上個確認對話框。
首先,在GridView的屬性對框話框中點擊“Columns”進入它的“字段”設計器。接著在“字段”設計器中選擇以前已加上的那個CommandField“刪除”列,這時在它的屬性列表下會看到一個“將此它段轉換為 TemplateFied”的項,點擊將它轉換為TemplateFied列。
完后退出該字段設計器,切換到源碼視圖你會發現該列已由原來的:<asp:CommandField ShowDeleteButton="True" />
變為了:
CODE:
<asp:TemplateField ShowHeader=&amp;quot;False&amp;quot;>
??? <ItemTemplate>
???? <asp:LinkButton ID=&amp;quot;LinkButton1&amp;quot; runat=&amp;quot;server&amp;quot; CausesValidation=&amp;quot;False&amp;quot; CommandName=&amp;quot;Delete&amp;quot;??? Text=&amp;quot;刪除&amp;quot;></asp:LinkButton>
</ItemTemplate>
最后在<asp:LinkButton>中加入:

OnClientClick="javascript:return confirm('真的要刪除嗎?');"

或者加入:

OnClientClick="if(confirm('你確定要刪除此記錄嗎?')){return true;}else{return false;}"

這樣點擊刪除時就會先在客戶端彈出“確認要刪除嗎?”對話框,而原來在RowDeleting事件中寫的代碼完全不用改變。

注意:CommandName="delete" CommandName 一定要設為"delete",否則將不觸發GridView中的RowDeleting 事件.

注意:在事件protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)里需要設置GridView中的DataKeysName = Fid 時,才可以找到相應的ID.(Fid為表的主鍵 id)

?

2,

CODE:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
??? if (e.Row.RowType == DataControlRowType.DataRow)
??? {
??????? if (e.Row.RowIndex > -1)
??????? {
??????????? int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
??????????? LinkButton lbtnDelete = (LinkButton)e.Row.FindControl(&amp;quot;lbtnDelete&amp;quot;);
??????? if (lbtnDelete != null)
??????? {
??????????? lbtnDelete.CommandArgument = id.ToString();
??????????? lbtnDelete.Attributes.Add(&amp;quot;onClick&amp;quot;, &amp;quot;<script>return confirm(’是否確認刪除!’)</script>&amp;quot;);
??????? }
??? }
}
3,針對C#中的Windows窗體程序,可以先引用System.windwos.Forms,然后在進行處理;對于Web應用程序不適合。

CODE:
using System.Windows.Forms;

protected void gvNewList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
??? DialogResult result = MessageBox.Show(&amp;quot;確定要刪除本行嗎?&amp;quot;, &amp;quot;信息提示!&amp;quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,MessageBoxOptions.ServiceNotification);
??? if (result == DialogResult.Yes)
??? {
??????? e.Cancel = false;
??? }
??? else
??? {
??????? e.Cancel = true;
??? }
}
4,添加一個刪除列

CODE:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
??? TableCell tc = (TableCell)e.Row.Cells[e.Row.Cells.Count - 1];
??? for (int i = 0; i < tc.Controls.Count; i += 2)
??? {
??????? // cerco il controllo ImageButton (ho utilizzato quello)
??????? Object o = tc.Controls[i];
??????? if (o is ImageButton)
??????? {
??????????? // controllo trovato!
??????????? // ora aggiungo l’evento js onClick per chiedere conferma all’utente
??????????? ImageButton lb = (ImageButton) o;
??????????? ((ImageButton)lb).Attributes.Add(&amp;quot;onclick&amp;quot;, @&amp;quot;javascript:return confirm('Attenzione: sicuro di voler cancellare?');&amp;quot;);
??????? }
??? }
}


-------------------------------------------------------------
CODE:
<asp:TemplateField ShowHeader=&amp;quot;False&amp;quot;>
<ItemStyle HorizontalAlign=&amp;quot;Center&amp;quot; Width=&amp;quot;16px&amp;quot; />
<ItemTemplate>
<asp:ImageButton ID=&amp;quot;imgDelete&amp;quot; runat=&amp;quot;server&amp;quot; CausesValidation=&amp;quot;False&amp;quot; CommandName=&amp;quot;Delete&amp;quot; ImageUrl=&amp;quot;~/img/ico_elimina.gif&amp;quot; AlternateText=&amp;quot;Cancella data&amp;quot; OnClientClick=&amp;quot;return confirm(’Sicuro di voler cancellare?’);&amp;quot; />
</ItemTemplate>
</asp:TemplateField>


以上方法總結
---------Template 方式 -----------------------------------------------
CODE:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<aspinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="刪除" OnClientClick=’return confirm("Are you sure you want to delete this record?");’></aspinkButton>
</ItemTemplate>
</asp:TemplateField>


-------------RowDeleting method------------------------------------------------

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

??? Response.Write("<script language=javascript>window.confirm('確定刪除嗎?')</script>");
}

-------------RowDataBound method--------------------------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
??? if (e.Row.RowType == DataControlRowType.DataRow)
??? {
??????? ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm(’確實要刪除該記錄嗎?’)");
??? }

}

另外:在web頁面中,在按鈕事件中添加彈出對話框的方法還有,

在前臺點擊按鈕如b1,事件為b1_click,在后臺寫b1_click的代碼,實現點擊后彈出:“確定要刪除嗎?”選擇確定,跳轉頁面如:aa.aspx,選擇取消,無動作!

protected void Page_Load(object sender, EventArgs e)
{
??? b1.Attributes.Add("onclick", "javascript:if(confirm('確定要刪除嗎?')){}else{return false;}");
}

轉載于:https://www.cnblogs.com/blogbai/archive/2012/09/27/2705853.html

總結

以上是生活随笔為你收集整理的实现对gridview删除行时弹出确认对话框的四种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。