如何在asp.net的数据列表控件中找到图像控件 [英] how to find image control inside a datalist control in asp.net
问题描述
我有一个数据列表:
Hi, I have a datalist as:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" style="z-index: 108; left: 733px; position: absolute; top: 72px" >
<ItemTemplate><asp:Image ID="uximage" runat="server" /></ItemTemplate>
</asp:DataList>
我在代码背后编写代码为:
and I am writing code in codebehind as:
protected void Button4_Click(object sender, EventArgs e)
{
// Image im = (Image)DataList1.FindControl("img");
Image im = (Image)DataList1.FindControl("uximage");
im.ImageUrl = "Handler.ashx";
}
我想要的是在该数据列表中找到图像控件
但我收到错误消息
行中的对象引用未设置为对象的实例"
What I want is to find image control in that datalist
but I am getting error as
"object reference not set to an instance of an object" in the line
im.ImageUrl = "Handler.ashx";
我该怎么办?我认为我正在编写正确的代码.请帮忙.
What should I do now? I think that I am writing the correct code. Please help.
推荐答案
foreach(DataListitem item in DataList1.Items)
{
Image im = (Image)item.FindControl("uximage");
im.ImageUtrl = "Handler.ashx";
}
<asp:DataList ID="dataListEmp" OnItemCommand="DatalistcommandEvent" RepeatColumns="1" CellSpacing="10" GridLines="Both" ItemStyle-Width="300" ItemStyle-BorderColor="Blue" BorderStyle="Dotted" CellPadding="10" BorderColor="Blue" runat="server" DataSourceID="sqlDataSourceEmp">
<ItemTemplate>
<%# Eval("EmpId") %>
<%# Eval("Employee Name") %>
<%# Eval("Location") %>
<asp:ImageButton runat="server" ID="imgData" AlternateText="Image" CommandName="Myimage" />
</ItemTemplate>
</asp:DataList>
受保护的void DatalistcommandEvent(object s,DataListCommandEventArgs e)
{
如果(e.CommandName =="Myimage")
{
DataListItem item = dataListEmp.Items [e.Item.ItemIndex] as DataListItem;
ImageButton imgbtn = item.FindControl("imgData")作为ImageButton;
imgbtn.ImageUrl ="http://localhost:64452/FilesReadAndInsert/Images/dave.jpg";
}
}
protected void DatalistcommandEvent(object s,DataListCommandEventArgs e)
{
if (e.CommandName == "Myimage")
{
DataListItem item = dataListEmp.Items[e.Item.ItemIndex] as DataListItem;
ImageButton imgbtn = item.FindControl("imgData") as ImageButton;
imgbtn.ImageUrl = "http://localhost:64452/FilesReadAndInsert/Images/dave.jpg";
}
}
protected void btnFind_Click(object sender, EventArgs e)
{
DataListItem item=dataListEmp.Items[1] as DataListItem;
ImageButton imgbtn=item.FindControl("imgData") as ImageButton;
imgbtn.ImageUrl = "http://localhost:64452/FilesReadAndInsert/Images/flow3.jpg";
}
e.jpg;
上面的方法可以工作,但问题是
在这里,我们必须对项目索引进行硬编码.我们将无法获得正确的索引,这就是我在datalist命令事件中编写的原因
e.jpg";
above will work but the problem is
here we have to hard code the item index. we will not get proper index that''s why i wrote in the datalist command event
DataListItem item=dataListEmp.Items[1] as DataListItem;
这篇关于如何在asp.net的数据列表控件中找到图像控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!