------.ASPx 添加:OnRowCreated="GridVIEw1_RowCreated"
<ASP:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Base.mdb"
SelectCommand="SELECT [id], [Title], [Classid] FROM [leoWeb_News]"
DeleteCommand="delete from leoweb_news where id = @id"
></ASP:AccessDataSource>
<ASP:GridView ID="GridVIEw1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="AccessDataSource1" OnRowCreated="GridVIEw1_RowCreated" >
<Columns>
<ASP:BoundField DataFIEld="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<ASP:BoundField DataFIEld="Title" HeaderText="Title" SortExpression="Title" />
<ASP:BoundField DataFIEld="Classid" HeaderText="Classid" SortExpression="Classid" />
<ASP:CommandFIEld ShowDeleteButton="True" />
</Columns>
&nbs
p; </ASP:GridVIEw>
------------------------------------------------------------------------cs 添加
protected void GridView1_RowCreated(object sender, GridVIEwRowEventArgs e)
{
if (e.Row != null && e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.Cells[3].Controls[0] as LinkButton;
//如果是Button按鈕,即 ButtonType="Button" 時
//則 Button lb = e.Row.Cells[3].Controls[0] as Button;
lb.Attributes.Add("onclick", "return window.confirm(''刪除確認'')");
}
}
------------------------------------------------------------------------------------------------
在最終的Html結果源文件中,ButtonType="LinkButton" 時 onclick="return window.confirm(''刪除確認'');" A標記回加上上面這個屬性。
如果:ButtonType="Button" 則 onclick="return window.confirm(''刪除確認'');Javascript:__doPostBack(''GridVIEw1'',''Delete$9'')" 可以看到這個屬性被加到了原來的onclick之前。
知道這些對了解.Net2.0控件執行機理的幫助是很大的。