How do I create a DataGrid with Delete buttons and pop up a message box asking the user for confirmation before deleting a record?
The ASPX file below demonstrates the proper technique. It populates a DataGrid with content from the Titles table of the Pubs database that comes with Microsoft SQL Server. The DataGrid's leftmost column contains a row of Delete buttons. The OnDeleteRecord method simulates a record deletion by writing the Title field of the record to be deleted to a Label control. The OnAttachScript method, which is called once for each row in the DataGrid in response to ItemCreated events, attaches to each button an OnClick attribute that activates a bit of client-side JavaScript. That script displays a confirmation dialog and prevents the page from posting back to the server (thus preventing OnDeleteRecord from being called) if the user clicks Cancel rather than OK.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html>
<body>
<form runat="server">
<asp:DataGrid ID="MyDataGrid"
AutoGenerateColumns="false" CellPadding="2"
BorderWidth="1" BorderColor="lightgray"
Font-Name="Verdana" Font-Size="8pt"
GridLines="vertical" Width="90%"
OnItemCreated="OnAttachScript"
OnItemCommand="OnDeleteRecord"
RunAt="server">
<Columns>
<asp:ButtonColumn Text="Delete"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center"
ButtonType="PushButton"
CommandName="Delete" />
<asp:BoundColumn HeaderText="Item ID"
DataField="title_id" />
<asp:BoundColumn HeaderText="Title"
DataField="title" />
<asp:BoundColumn HeaderText="Price"
DataField="price" DataFormatString="{0:c}"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="right" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white"
Font-Bold="true" />
<ItemStyle BackColor="white" ForeColor="darkblue" />
<AlternatingItemStyle BackColor="beige"
ForeColor="darkblue" />
</asp:DataGrid>
<asp:Label ID="Output" RunAt="server" />
</form>
</body>
</html><script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
if (!IsPostBack) {
SqlDataAdapter adapter = new SqlDataAdapter
("select * from titles where price != 0",
"server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds = new DataSet ();
adapter.Fill (ds);
MyDataGrid.DataSource = ds;
MyDataGrid.DataBind ();
}
} void OnAttachScript (Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem) {
WebControl button = (WebControl) e.Item.Cells[0].Controls[0];
button.Attributes.Add ("onclick",
"return confirm (\"Are you sure?\");");
}
} void OnDeleteRecord (Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "Delete") {
Output.Text = "Deleted \"" + e.Item.Cells[2].Text + "\"";
}
}
</script>
The ASPX file below demonstrates the proper technique. It populates a DataGrid with content from the Titles table of the Pubs database that comes with Microsoft SQL Server. The DataGrid's leftmost column contains a row of Delete buttons. The OnDeleteRecord method simulates a record deletion by writing the Title field of the record to be deleted to a Label control. The OnAttachScript method, which is called once for each row in the DataGrid in response to ItemCreated events, attaches to each button an OnClick attribute that activates a bit of client-side JavaScript. That script displays a confirmation dialog and prevents the page from posting back to the server (thus preventing OnDeleteRecord from being called) if the user clicks Cancel rather than OK.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html>
<body>
<form runat="server">
<asp:DataGrid ID="MyDataGrid"
AutoGenerateColumns="false" CellPadding="2"
BorderWidth="1" BorderColor="lightgray"
Font-Name="Verdana" Font-Size="8pt"
GridLines="vertical" Width="90%"
OnItemCreated="OnAttachScript"
OnItemCommand="OnDeleteRecord"
RunAt="server">
<Columns>
<asp:ButtonColumn Text="Delete"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center"
ButtonType="PushButton"
CommandName="Delete" />
<asp:BoundColumn HeaderText="Item ID"
DataField="title_id" />
<asp:BoundColumn HeaderText="Title"
DataField="title" />
<asp:BoundColumn HeaderText="Price"
DataField="price" DataFormatString="{0:c}"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="right" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white"
Font-Bold="true" />
<ItemStyle BackColor="white" ForeColor="darkblue" />
<AlternatingItemStyle BackColor="beige"
ForeColor="darkblue" />
</asp:DataGrid>
<asp:Label ID="Output" RunAt="server" />
</form>
</body>
</html><script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
if (!IsPostBack) {
SqlDataAdapter adapter = new SqlDataAdapter
("select * from titles where price != 0",
"server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds = new DataSet ();
adapter.Fill (ds);
MyDataGrid.DataSource = ds;
MyDataGrid.DataBind ();
}
} void OnAttachScript (Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem) {
WebControl button = (WebControl) e.Item.Cells[0].Controls[0];
button.Attributes.Add ("onclick",
"return confirm (\"Are you sure?\");");
}
} void OnDeleteRecord (Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "Delete") {
Output.Text = "Deleted \"" + e.Item.Cells[2].Text + "\"";
}
}
</script>
btnDel.Attributes.Add("onclick","<script>return confirm('是否删除?');</script>");
哪怎样做法
btnDel.Attributes.Add("onclick","<script>return confirm('是否删除?');</script>");
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
}
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton delBttn = (LinkButton)e.Item.FindControl("LinkButton_del");
delBttn.Attributes.Add("onclick","javascript:return confirm('确定删除?');");
}
}