Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All!!!

i have an image button inside my datalist. i have given it a command name and a command argument. i am calling it on datalistItemCommand. it is not working. when i replaced the image button with linkbutton it started working.I tried using checkpoint but when i am using imagebutton it's not firing itemcommand event. Please tell me ho i can i made my image button work. i really need image button here not the link button.

XML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4"
              onitemcommand="DataList1_ItemCommand" >
      <ItemTemplate>
        <obout:ImageZoom ID="ImageZoom1" runat="server"  ImageUrl='<%#Eval("ImageUrl") %>'/>
      <br />
      <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label>
      <br />
      <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductCost") %>'></asp:Label>
     <br />
           <asp:ImageButton ID="Button1"  runat="server" Text="Add to Cart" CommandName="ADD"  CommandArgument='<%#Eval("ProductID") %>' ImageUrl="~/img/button_add_to_cart.png"/>

     <br />
      </ItemTemplate>
      </asp:DataList>



here is the itemcommand event


protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ADD")
            {
                arg = e.CommandArgument.ToString();
                con.Open();
                SqlCommand cmd1 = new SqlCommand("Select ProductID , ProductName ,ProductCost from Products where ProductID= '" + arg + "'", con);
                SqlDataReader dr;
                dr = cmd1.ExecuteReader();
                dr.Read();
                DataTable MyDT = new DataTable();
                DataRow MyRow;
                if (Session["DataTable"] == null)
                {
                    MyDT.Columns.Add("ProductId", System.Type.GetType("System.String"));
                    MyDT.Columns.Add("ProductName");
                    MyDT.Columns.Add("UnitPrice", System.Type.GetType("System.Decimal"));
                    MyRow = MyDT.NewRow();
                    MyRow[0] = dr.GetValue(0).ToString();
                    MyRow[1] = dr.GetValue(1).ToString();
                    MyRow[2] = dr.GetValue(2).ToString();
                    MyDT.Rows.Add(MyRow);
                }
                else
                {
                    MyDT = (DataTable)Session ["DataTable"];
                    MyRow = MyDT.NewRow();
                    MyRow[0] = dr.GetValue(0).ToString();
                    MyRow[1] = dr.GetValue(1).ToString();
                    MyRow[2] = dr.GetValue(2).ToString();
                    MyDT.Rows.Add(MyRow);

                }

                Session["DataTable"] = MyDT;
                GridView1.DataSource = MyDT;
                GridView1.DataBind();
                con.Close();
            }
        }
        catch (Exception ex)
        {
            Session.Abandon();
            Session.Clear();
            Response.Write(ex.Message);

        }
    }
Posted

Try as
C#
<asp:linkbutton id="btnAdd" runat="server" causesvalidation="false" commandname="Insert" commandargument="<%# Eval("ProductID") %>" xmlns:asp="#unknown">
<asp:image id="Image2" runat="server" imageurl="~/Images/delete.png" />
 </asp:linkbutton>
 
Share this answer
 
Comments
ujjwal uniyal 28-Dec-11 9:14am    
Thank you Uma. it worked :) 5 from me.
ujjwal uniyal 28-Dec-11 9:22am    
But is there a reason why linkbutton works while others don't??
uspatel 29-Dec-11 0:03am    
I got your code right so i give a altrenative.......
Check this link,
Working with the DataList Control[^]

it will help so.
 
Share this answer
 
v4
Comments
Wendelius 28-Dec-11 15:13pm    
Link formatted

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