为何ListView控件中ItemTemplate的 编辑 按钮点击时无效
前台代码:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="EntityDataSource2" GroupItemCount="7" onitemdatabound="ListView1_ItemDataBound"> <EditItemTemplate> <td runat="server"> 课程名称: <asp:TextBox ID="CourseContentTextBox" runat="server" Text='<%# Bind("CourseContent") %>' /> 教师: <asp:TextBox ID="TeacherTextBox" runat="server" Text='<%# Bind("Teacher") %>' /> <br /> <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" /> <br /> </td> </EditItemTemplate> <GroupTemplate> <tr ID="itemPlaceholderContainer" runat="server"> <td ID="itemPlaceholder" runat="server"> </td> </tr> </GroupTemplate> <ItemTemplate> <td runat="server"> <asp:Label ID="CourseContentLabel" runat="server" Text='<%# Eval("CourseContent") %>' /> <asp:Label ID="TeacherLabel" runat="server" Text='<%# Eval("Teacher") %>' /> <br /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" /> <br /> </td> </ItemTemplate> <LayoutTemplate> <table runat="server" border="2"> <tr runat="server"> <td runat="server"> <table ID="groupPlaceholderContainer" runat="server" border="1"> <tr runat="server"> <th runat="server"></th> <th runat="server">星期一</th> <th runat="server">星期二</th> <th runat="server">星期三</th> <th runat="server">星期四</th> <th runat="server">星期五</th> <th runat="server">星期六</th> <th runat="server">星期日</th> </tr> <tr ID="groupPlaceholder" runat="server"> </tr> </table> </td> </tr> </table> </LayoutTemplate> </asp:ListView>
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { Label courseContentLabel = e.Item.FindControl("CourseContentLabel") as Label; Button editButton = e.Item.FindControl("EditButton") as Button; if (courseContentLabel.Text != "") { editButton.Enabled = false; } } }