当链接按钮工作时,ImageButton在数据列表中不起作用 [英] Imagebutton not working inside datalist while linkbutton is working
问题描述
大家好!!!!
我的数据列表中有一个图像按钮.我给了它一个命令名称和一个命令参数.我在datalistItemCommand上调用它.它不起作用.当我用链接按钮替换图像按钮时,它开始工作.我尝试使用检查点,但是当我使用图像按钮时,它不触发itemcommand事件.请告诉我,我可以使我的图像按钮正常工作吗.我真的需要图片按钮,而不是链接按钮.
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.
<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>
这是itemcommand事件
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);
}
}
推荐答案
尝试为
<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>
检查此链接,
使用DataList控件 [
Check this link,
Working with the DataList Control[^]
it will help so.
这篇关于当链接按钮工作时,ImageButton在数据列表中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!