DataGrid模板列的问题
在DataGrid有两个模板列
一个是Imagebutton,一个是Label,我想点击Imagebutton的时候,取出Label的Text
具体代码如下:
<asp:datagrid id="SearchGrid" runat="server">
<Columns>
<asp:TemplateColumn Visible=false>
<ItemTemplate>
<asp:Label ID="Store"><%# DataBinder.Eval(Container.DataItem,"StoreID")%></asp:Label>
<asp:Label ID="Product"><%# DataBinder.Eval(Container.DataItem,"ProductID")%></asp:Label> </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton ImageUrl='../Image/buy.gif' runat="server" ID="buy" OnClick="ImageButton_click">
</asp:ImageButton> </ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
//Imagebutton事件
public void ImageButton_click
(object sender,System.Web.UI.ImageClickEventArgs e)
{
string StoreID,ProductID;
foreach (DataGridItem DataRows in SearchGrid.Items)
{
ImageButton AddCart = (ImageButton)DataRows.FindControl("buy");
if(AddCart.Equals((ImageButton)sender))
{
Label Store = (Label)DataRows.FindControl("Store");
StoreID = Store.Text; //这里出错,未将对象引用设置到对象的实例。
Label Product = (Label)DataRows.FindControl("Product");
ProductID = Product.Text;
}
}
}
1、Imagebutton可以找到,为什么Label找不到?
2、ImageButton AddCart=(ImageButton)DataRows.Cells[x].FindControl("buy");
Cell后面是0或者1都可以找到Imagebutton,为什么?