Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi.i bring link button inside datalist.but dont know how get productid from any imagebutton when clicked on it. i want use commandargument proprty but have two different datasource so how get commandargument in datalist1 that is clicked on imagebutton and use in datalist2 for productID.with these explanations how define productID? more info: when i click on imagebutton is executed a dynamic slideshow and i want from a few color for dress only a color is selected.Thanks
ASP.NET
<asp:datalist id="DataList1" runat="server" datakeyfield="WomenDressID" datasourceid="SqlDataSource2" repeatdirection="Horizontal" repeatcolumns="4" xmlns:asp="#unknown">
              <itemtemplate>
                  <div>
                  <li style="list-style:none"> 
                     <asp:imagebutton id="ImageButton1" runat="server" onclientclick="return false" commandname="click" commandargument="<%# Eval("WomenDressID") %>" src="<%#Eval("picUrl","images/product/{0}") %>" width="50" height="50" cssclass="imgColor" />
                </li> 
                </div>
                   </itemtemplate>
            </asp:datalist>
                
             
         <asp:sqldatasource id="SqlDataSource2" runat="server" connectionstring="<%$ ConnectionStrings:ClothingShopConnection %>" selectcommand="SELECT top 4 * FROM [WomenDress] WHERE ([productName] = @productName)" xmlns:asp="#unknown">
          <SelectParameters>
                <asp:querystringparameter name="productName" querystringfield="Name" type="String" />
            </SelectParameters>
          </asp:sqldatasource>

ASP.NET
<asp:datalist id="DataList2" runat="server" datasourceid="SqlDataSource3" onitemcommand="DataList2_ItemCommand" xmlns:asp="#unknown">
              <itemtemplate>
                  <div style="width:200px;">
                     <div class="mainPrice">
                      <asp:label id="Label2" runat="server" text="قیمت اصلی:"></asp:label>
                         </div>
                     <div class="valuePrice"> 
                      <asp:label id="Label4" runat="server" text="<%#Eval(" price=")%>" cssclass="priceDecor"></asp:label>
                         </div>
                     </div>
                     <div style="width:200px;">
                     <div class="mainPrice">
                      <asp:label id="Label3" runat="server" text="قیمت با تخفیف:"></asp:label>
                         </div>
                     <div class="valuePrice"> 
                      <asp:label id="Label5" runat="server" text="<%#Eval(" withdiscount=")%>"></asp:label>
                         </div>
                      </div>
                  <div class="wrapBasket">
                      <asp:linkbutton id="LinkButton1" runat="server" cssclass="btnBasket" commandname="linkClick">افزودن به سبد خرید</asp:linkbutton>
                  </div>
                  </itemtemplate>
                </asp:datalist>
 <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ClothingShopConnection %>" SelectCommand="SELECT top 1 * FROM [WomenDress] WHERE ([productName] = @productName)">
                    <SelectParameters>
                     <asp:QueryStringParameter Name="productName" QueryStringField="Name" Type="String" />
                </SelectParameters>
                </asp:SqlDataSource>

and codebehind
C#
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if(e.CommandName == "linkClick")
        {
            Int64 ProductID =
            string script = "Redirect();";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Redirect", script, true);
            ShoppingCartAccess.AddItem(ShoppingCartAccess.ShoppingCartId, ProductID);
        }
    }

C#
public class ShoppingCartAccess
    {
        public ShoppingCartAccess()
        {
            //
            // TODO: Add constructor logic here
            //
        }
 
        public static string ShoppingCartId
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string cartId = "";
                object cartIdSession = context.Session["BalloonShop_CartID"];
                if (cartIdSession != null)
                    cartId = cartIdSession.ToString();
                if (cartId != "")
                    return cartId;
                else
                {
                    if (context.Request.Cookies["BalloonShop_CartID"] != null)
                    {
                        cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
                        context.Session["BalloonShop_CartID"] = cartId;
                        return cartId;
                    }
                    else
                    {
cartId = Guid.NewGuid().ToString();
                        HttpCookie cookie = new HttpCookie("BalloonShop_CartID", cartId.ToString());
                        int howManyDays = BalloonShopConfiguration.cartPersistDays;
                        DateTime currentDate = DateTime.Now;
                        TimeSpan timespan = new TimeSpan(howManyDays, 0, 0, 0);
                        DateTime expirationDate = currentDate.Add(timespan);
                        cookie.Expires = expirationDate;
                        context.Response.Cookies.Add(cookie);
                        context.Session["BalloonShop_CartID"] = cartId;
                        return cartId.ToString();
 
                    }
                }
 
            }
        }
 
        public static bool ExecuteSP(string SpName, List<sqlparameter> SPParameters)
        {
            string CS = ConfigurationManager.ConnectionStrings["ClothingShopConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand(SpName, con);
                cmd.CommandType = CommandType.StoredProcedure;
 
                foreach (SqlParameter parameter in SPParameters)
                {
                    cmd.Parameters.Add(parameter);
                }
                con.Open();
                return Convert.ToBoolean(cmd.ExecuteScalar());
            }
        }
 
        public static void AddItem(string cartNum, Int64 proID)
        {
            List<sqlparameter> paramlist = new List<sqlparameter>()
            {
                new SqlParameter()
                {
                    ParameterName="@cartID",
                    Value=cartNum
                },
                 
                new SqlParameter()
                {
                    ParameterName="@productId",
                    Value=proID
                }
         };
            ExecuteSP("ShoppingAddItem", paramlist);
 
        }
     }
}
</sqlparameter>


What I have tried:

ASP.NET, c#,ASP.NET, c#,,ASP.NET, C#3.5, ASP.NET, c#
Posted
Updated 21-Mar-16 6:04am
v6
Comments
Sinisa Hajnal 22-Mar-16 3:33am    
You have to get the colors based on selected ID? That would require either client side filter or server side data retrieval. Depending on the number of items you show on the page, you might even get all needed data when you load the page initially. If you show 10 products per page, it shouldn't be too much to get color array for each. If you show 20 or more it would be more efficient to get colors from server only on request.

As for how to use different IDs: when you go to the server in click event, read the command argument and write it where you need it. Or save it to session object and use it on the client (I would do first version).
Rojalin Sahoo 22-Mar-16 7:15am    
I am not clear about your relation between datalist1 and datalist2. If you want to save the command argument of datalist1 and use it for datalist2 then you can use viewstate for that. on datalist1_ItemCommand save the selected row command argument in viewstate and use it anywhere you want in that page.

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