积极答复者
DataList里的Button控件不能触发datalist的ItemCommand事件?

问题
-
DataList里的Button控件不能触发datalist的ItemCommand事件,但是替换掉button控件改用linkbutton就可以了。
EnableEventValidation 设置为false.
<asp:Button runat="server" ID="btnEdit" CommandName="edit" Text="Edit" Height="21px" onclick="btnEdit_Click" />
itemcommand的事件代码就不给出了,单步执行,点击button完全不触发commanditem事件。这是为什么?
用F12开发人员工具查看了一下 用button控件的前端代码:
<input type="submit" name="dlManager$ctl01$btnEdit" value="Edit" id="dlManager_ctl01_btnEdit" style="height:21px;" />
用linkbutton的前端代码:
<a id="dlManager_ctl01_lnkbtnUserName" href="javascript:__doPostBack('dlManager$ctl01$lnkbtnUserName','')">accessking</a>
怀疑是不是和wen.comfig的设置有问题, 就重新建了一个项目,然后试了一下。结果一样。
答案
-
根據你的描述我做了下列程式碼來模擬你的情境,似乎跟Button無關,有興趣不妨參考看看。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListDemo.aspx.cs" Inherits="WebApplication2.DataListDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" DataKeyField="RegionID" DataSourceID="SqlDataSource1" onitemcommand="DataList1_ItemCommand"> <ItemTemplate> RegionID: <asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' /> <br /> RegionDescription: <asp:Label ID="RegionDescriptionLabel" runat="server" Text='<%# Eval("RegionDescription") %>' /> <br /> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> RegionID: <asp:TextBox ID="RegionIDTextBox" runat="server" Text='<%# Bind("RegionID") %>' /> <br /> RegionDescription: <asp:TextBox ID="RegionDescriptionTextBox" runat="server" Text='<%# Bind("RegionDescription") %>' /> <br /> </EditItemTemplate> </asp:DataList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Region]"></asp:SqlDataSource> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class DataListDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "Edit") { DataList1.EditItemIndex = e.Item.ItemIndex; DataList1.DataBind(); } } } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 Jone Zhu 2012年3月21日 10:57
全部回复
-
既然要用datalist的ItemCommand事件
就請把onclick="btnEdit_Click"拿掉
还有 CommandName="Edit" ,这个Edit是保留字,请至少改成myEdit
然后在ItemCommand事件判断
if(e.CommandName=="myEdit")
{
Response.Write("Button's Edit");
}
↑类似这样
- The blog of typewriter職人
- Convert C# to VB.NET
- /*If my concept is wrong ,please correct me.Thanks.*/
- 已编辑 Shadow .Net 2012年3月19日 11:49
-
根據你的描述我做了下列程式碼來模擬你的情境,似乎跟Button無關,有興趣不妨參考看看。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListDemo.aspx.cs" Inherits="WebApplication2.DataListDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" DataKeyField="RegionID" DataSourceID="SqlDataSource1" onitemcommand="DataList1_ItemCommand"> <ItemTemplate> RegionID: <asp:Label ID="RegionIDLabel" runat="server" Text='<%# Eval("RegionID") %>' /> <br /> RegionDescription: <asp:Label ID="RegionDescriptionLabel" runat="server" Text='<%# Eval("RegionDescription") %>' /> <br /> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> RegionID: <asp:TextBox ID="RegionIDTextBox" runat="server" Text='<%# Bind("RegionID") %>' /> <br /> RegionDescription: <asp:TextBox ID="RegionDescriptionTextBox" runat="server" Text='<%# Bind("RegionDescription") %>' /> <br /> </EditItemTemplate> </asp:DataList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Region]"></asp:SqlDataSource> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class DataListDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "Edit") { DataList1.EditItemIndex = e.Item.ItemIndex; DataList1.DataBind(); } } } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 Jone Zhu 2012年3月21日 10:57
-
终于找到答案了, 我把Page.EnableEventValidation设置为了false.这是一个多么悲剧的问题啊。人生就如一个茶几,上面摆满了悲剧......
同时也十分感谢Shadow And Happy Code和TerryChuang的热心帮助。