Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i am using datalist to view image from database in imagebutton i do this and no problem but
i want when clicked in image go to another page and display all ditails of this image i have a problem with getting image index

i use this code in aspx
XML
<asp:DataList ID="DataList1" runat="server"  RepeatColumns="3"  OnItemCommand="DataList1_ItemCommand" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Both" Width="719px"  >
        <ItemTemplate>
            <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl='<%#Eval("image")%>' CommandName="selectimage" CommandArgument='<%# Eval("id") %>' Width=100 Height=100 /><br />


and in cs

MIDL
if (e.CommandName == "selectimage")
{

   int Id = int.Parse(e.CommandArgument.ToString());
}

but get the index empty
and do know the problem
Posted

Set your Id field as DataKeyField and then try to retrieve its value :
In aspx side of Datalist :- DataKeyField="id"
As you require Unique value for CommandArgument.
 
Share this answer
 
v2
Comments
shimaa010 4-Jul-11 13:04pm    
thanks for reply
but i do not understand you can explain it clearly
[no name] 5-Jul-11 6:42am    
just specify 'Id' in the DataKeyField for your Datalist and then check.
Set Your DataList DataKeyField and follow the bellow code:
ASP.NET
<asp:datalist id="dlPhotos" runat="server" repeatcolumns="5" xmlns:asp="#unknown">
                    RepeatDirection="Horizontal" BorderColor="#336699" BorderStyle="Solid" 
                    BorderWidth="1px" CellPadding="3" DataKeyField="SLNO" 
                    onitemcommand="dlPhotos_ItemCommand">                  
                      <itemtemplate>
                          <asp:imagebutton id="imgView">
                              ImageUrl='<%# "Handler.ashx?ImgId=" + Eval("SLNO") %>' runat="server" 
                              Height="110px" Width="150px" CommandName="Select" />
                        <br />
                        <asp:label id="Label1" runat="server" text="<%# Eval("PHOTO_TITLE") %>" font-bold="True">
                            Font-Size="10pt" ForeColor="#336699" Width="100%" />
                        
                    </itemtemplate>
                    <itemstyle horizontalalign="Center" verticalalign="Top" />


C#
protected void dlPhotos_ItemCommand(object source, DataListCommandEventArgs e)
    {
         if (e.CommandName == "Select")
        {
            int strId = Convert.ToInt32(dlPhotos.DataKeys[e.Item.ItemIndex].ToString());
        }

    }
 
Share this answer
 
v2
Comments
RaisKazi 18-Nov-11 10:05am    
Edited: Added "pre" tag for code-block.
Jitendra Parida - Jeetu 23-Nov-11 4:55am    
thank u.

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