本文介绍了在链接按钮上单击选定的图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! hi.i带来了datalist里面的链接按钮。但是不知道如何在点击它时从任何图像按钮获得productid。我想使用commandargument proprty,但有两个不同的数据源,所以如何在datalist1中获取命令规则,点击imagebutton并在datalist2中使用productID.with这些解释如何定义productID?更多信息:当我点击图像按钮执行一个动态幻灯片时,我想从几种颜色的衣服只选择一种颜色。谢谢 < asp:datalist id = DataList1 runat = server datakeyfield = WomenDressID datasourceid = SqlDataSource2 repeatdirection = 水平 repeatcolumns = 4 xmlns:asp = #未知 > < 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 = 名称 type = 字符串 / > < / SelectParameters > < / asp:sqldatasource > < asp:datalist id = DataList2 runat = server datasourceid = SqlDataSource3 onitemcommand = DataList2_ItemCommand xmlns:asp = #unknown > < itemte mplate > < 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 > < / d iv > < / div > < div style = width:200px; > < div class = mainPrice > < asp:label id = Label3 runat = server text = قیمتباتخفیف: > < / asp:标签 > < / 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 cssclas s = 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 名称 = productName QueryStringField = 名称 类型 = 字符串 / > < / SelectParameters > < / asp:SqlDataSource > 和代码隐藏 protected void DataList2_ItemCommand( object source,DataListCommandEventArgs e) { if (e.CommandName == linkClick) { Int64 ProductID = string script = Redirect();; ScriptManager.RegisterStartupScript( this , this .GetType(), 重定向,脚本, true ); ShoppingCartAccess.AddItem(ShoppingCartAccess.ShoppingCartId,ProductID); } } public class ShoppingCartAccess { public ShoppingCartAccess() { // // TODO:在此处添加构造函数逻辑 // } public static string ShoppingCartId { get { HttpContext context = HttpContext 。当前; 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]。值; 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,列表< sqlparameter> SPParameters) { string CS = ConfigurationManager.ConnectionStrings [ ClothingShopConnection]。ConnectionString; 使用(SqlConnection con = new SqlConnection(CS)) { SqlCommand cmd = new SqlCommand(SpName,con); cmd.CommandType = CommandType.StoredProcedure; foreach (SpaParameter参数在 SPParameters中) { cmd.Parameters.Add(参数); } con.Open(); return Convert.ToBoolean(cmd.ExecuteScalar()); } } public static void AddItem( string cartNum, Int64 proID) { List< sqlparameter> paramlist = new 列表< sqlparameter>() { new SqlParameter() { ParameterName = @ cartID, Value = cartNum }, new SqlParameter() { ParameterName = @ productId, Value = proID } }; ExecuteSP( ShoppingAddItem,paramlist); } } } < / sqlparameter > 我有什么试过: ASP.NET,c#,ASP.NET,c#,ASP.NET,C#3.5,ASP.NET,c#解决方案 ConnectionStrings:ClothingShopConnection%> select命令 = SELECT top 4 * FROM [WomenDress] WHERE([productName] = @productName) xmlns:asp = #unknown > < SelectParameters > < asp:querystringparameter name = productName querystringfield = 名称 type = 字符串 / > < / SelectParameters > < / asp:sqldatasource > < asp:datalist id = DataList2 runat = server datasourceid = SqlDataSource3 onitemcommand = DataList2_ItemCommand xmlns:asp = #unknown > < itemtemplate > < div 样式 = width:200px; > < div class = mainPrice > < asp:label id = Label2 runat = server text = قیمتاصلی: > < / asp:label > < / div > < div 类 = 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 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); } } 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#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: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: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 codebehindprotected 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); } }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# 解决方案 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: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 codebehindprotected 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); } }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# 这篇关于在链接按钮上单击选定的图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 11:23