Click here to Skip to main content
Rate this: bad
good
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
JF201553.4K
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 at 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 at 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 at 1-Oct-12 6:13am
   
That question has asked almost 2 years ago.
madhukk at 10-Nov-10 0:40am
   
That should work Did you place the code in button4 click event and did you get any error.
madhukk at 10-Nov-10 0:50am
   
Make sure you bind the Datalist1 and DataList1 is not empty.
Durgapasad at 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
0 Sergey Alexandrovich Kryukov 345
1 OriginalGriff 210
2 DamithSL 210
3 Kornfeld Eliyahu Peter 210
4 Peter Leow 95
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2014
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