DataGrid模板列的问题

lovefootball 2003-11-03 05:32:25
在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,为什么?
...全文
43 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xd123 2003-11-03
  • 打赏
  • 举报
回复
<asp:Label ID="Store" runat=server text'%# DataBinder.Eval(Container.DataItem,"StoreID")%>'></asp:Label>
<asp:Label ID="Product" runat=server text='<%# DataBinder.Eval(Container.DataItem,"ProductID")%>'></asp:Label>

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧