日期:2014-05-20  浏览次数:20487 次

如何获取DataGrid中控件的ID?
DataGrid:
<asp:datagrid   id= "MyDataGrid "   runat= "server "   CssClass= "td "   BorderColor= "#C0FFFF "   DataKeyField= "BookId " AllowSorting= "True "   AutoGenerateColumns= "False "   AllowPaging= "True "   Width= "100% ">
<HeaderStyle   CssClass= "tttable "   BackColor= "#FFFFC0 "> </HeaderStyle>
<Columns>
<asp:HyperLinkColumn   Text= "Name "   DataNavigateUrlField= "BookId "   DataNavigateUrlFormatString= "ShowBook.aspx?bookid={0} "
DataTextField= "Name "   SortExpression= "Name "   HeaderText= "Book   Name "   NavigateUrl= "BookId "> </asp:HyperLinkColumn>
<asp:BoundColumn   DataField= "Publisher "   SortExpression= "Publisher "   HeaderText= "Publisher "> </asp:BoundColumn> <asp:BoundColumn   DataField= "Author "   SortExpression= "Author "   HeaderText= "Author "> </asp:BoundColumn>
<asp:BoundColumn   DataField= "Translator "   SortExpression= "Translator "   HeaderText= "Translator "> </asp:BoundColumn>
<asp:BoundColumn   DataField= "Price "   SortExpression= "Price "   HeaderText= "Price "> </asp:BoundColumn>
<asp:ButtonColumn   Text= "&lt;img   src=img/basket.gif   border=0   alt=Delete   /&gt; "   HeaderText= "Cart "   CommandName= "Cart ">
<ItemStyle   HorizontalAlign= "Center "> </ItemStyle>
</asp:ButtonColumn>
<asp:TemplateColumn   HeaderText= "Delete ">
<ItemStyle   HorizontalAlign= "Center "> </ItemStyle>
<ItemTemplate>
<asp:ImageButton   id= "del "   runat= "server "   CausesValidation= "false "   CommandName= "Delete "   ImageUrl= "img\delete.gif "> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle   Mode= "NumericPages "> </PagerStyle>
</asp:datagrid>

我想给ImageButton加一个确认,
private   void   MyDataGrid_ItemDataBound(object   sender,   System.Web.UI.WebControls.DataGridItemEventArgs   e)
{
ImageButton   lkBtn   =   (ImageButton)e.Item.Cells[6].FindControl( "del ");
lkBtn.Attributes.Add( "onClick ",   "return   confirm( '你能确定要删除此书籍类型吗 '); "                 <--未将对象引用设置到对象的实例。);
}

------解决方案--------------------
直接在 <asp:ImageButton id= "del " runat= "server " CausesValidation= "false " CommandName= "Delete " ImageUrl= "img\delete.gif "> </asp:ImageButton> 中加上onClientClick不就可以了,
要不就(ImageButton)e.Item.FindControl( "del ");试试
------解决方案--------------------
在窗体初始化的InitializeComponent()中添加如下语句即可:

this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_DeleteCommand);

------解决方案--------------------
还有记得把你的删除动作写在

MyDataGrid_DeleteCommand()
{ }
事件中。
------解决方案--------------------