Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi, I have a datalist as:
XML
<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.
Posted
Updated 7-Nov-10 6:39am
v3

foreach(DataListitem item in DataList1.Items)
{
Image im = (Image)item.FindControl("uximage");
im.ImageUtrl = "Handler.ashx";
}
 
Share this answer
 
Comments
software_Engi08 9-Nov-10 13:39pm    
The above code is ie foreach(DataListitem item in DataList1.Items)
{
Image im = (Image)item.FindControl("uximage");
im.ImageUtrl = "Handler.ashx";
}
is not working it is not going to the body of the foreach loop .i have my 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" />
</asp:DataList>
please help, how to find image control in a datalist?
Samsani.v.s.Durga Prasad 28-Sep-12 8:09am    
<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" />



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";
}

}
m@dhu 1-Oct-12 6:13am    
That question has asked almost 2 years ago.
m@dhu 10-Nov-10 0:40am    
That should work Did you place the code in button4 click event and did you get any error.
m@dhu 10-Nov-10 0:50am    
Make sure you bind the Datalist1 and DataList1 is not empty.
XML
<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>


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";
}

}
 
Share this answer
 
v2
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";

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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900