双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

DataGrid模板列的问题

lovefootball lovefootball 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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,为什么?
...全文
评论 1
收藏 已收藏
分享
返回顶部
lovefootball
C# 111,129 成员 642,540 内容