Click here to Skip to main content
12,070,942 members (65,131 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: 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.
Posted 7-Nov-10 7:31am
Edited 7-Nov-10 7:39am
JF201554.3K
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

foreach(DataListitem item in DataList1.Items)
{
Image im = (Image)item.FindControl("uximage");
im.ImageUtrl = "Handler.ashx";
}
  Permalink  
Comments
sahuanil 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" >
<asp:Image ID="uximage" runat="server" />
</asp:DataList>
please help, how to find image control in a datalist?
Durgapasad 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">




<%# 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.
madhukk 10-Nov-10 0:40am
   
That should work Did you place the code in button4 click event and did you get any error.
madhukk 10-Nov-10 0:50am
   
Make sure you bind the Datalist1 and DataList1 is not empty.
Durgapasad 2-Oct-12 5:29am
   
yes obviously .first we should bind data to datalist.



Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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;
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100