赐教 e.Item.FindControl("Label1").Controls未将对象引用设置到对象的实例。

cool_jack2006 2009-11-30 11:21:13
我想通过在datagrid_ItemDataBound事件里得到<%# DataBinder.Eval(Container.DataItem,"path" )%>当前的值,还有得到<%# DataBinder.Eval(Container.DataItem,"path" )%>值之后向附值Image1.ImageUrl

<asp:DataGrid ID="mydatagrid" runat="server" AutoGenerateColumns="False" Width="100%"
ShowHeader="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnItemDataBound="mydatagrid_ItemDataBound">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table>
<tr>
<td rowspan="2" style="text-align: center;">
<div>
<asp:Label Visible="false" ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"path" )%>'></asp:Label>
<asp:Image ID="Image1" runat="server" /> </div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>


protected void mydatagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
foreach (Control c in e.Item.FindControl("Label1").Controls)
{
Label label = (Label)c;
string path = label.Text;
}
}
...全文
355 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ncjcz 2009-12-01
  • 打赏
  • 举报
回复
如果你只有表头的话,加一句
if (e.Item.ItemIndex > 0)
把表头过滤掉就可以了,因为表头里没有Label1

还有:e.Item.FindControl("Label1")找到的就是Label1了,不需要再遍历找他的子控件

这样写吧
if (e.Item.ItemIndex > 0)
{

Label label = e.Item.FindControl("Label1") as Label;
if(label != null)
string path = label.Text;


}
jmghoul 2009-12-01
  • 打赏
  • 举报
回复
说明controls是null
freeboy827 2009-12-01
  • 打赏
  • 举报
回复

foreach (Control c in e.Item.FindControl("Label1"))
{
Label label = (Label)c;
string path = label.Text;
}

  • 打赏
  • 举报
回复
五楼的很清楚的
阿非 2009-12-01
  • 打赏
  • 举报
回复

protected void mydatagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
{
Label lbl= e.Item.FindControl("Label1") as Label;
if(lbl!=null)
{
string path = lbl.Text;
}
}
}
msy_84 2009-12-01
  • 打赏
  • 举报
回复
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
{
foreach (Control c in e.Item.FindControl("Label1").Controls)
{
Label label = (Label)c;
string path = label.Text;
}
}

ITEMPLATE 模板是内容行,而datagrid_ItemDataBound 事件是从HEADER 模板,到FOOTER 开始遍历的...
这样,试一下
Crystalzhou 2009-12-01
  • 打赏
  • 举报
回复
if (null==e.Item)
return;
if(e.Item.ItemIndex<0)
return;
Label label = e.Item.FindControl("Label1") as Label;
if(label != null)
string path = label.Text;


yutian_31 2009-11-30
  • 打赏
  • 举报
回复
你把Visible="true"试下

111,120

社区成员

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

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

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