我使用 AJAX Control Toolkit Accordion、LinkButton 和 TextBox 创建了一个简单的用户控件,如下所示:
测试控制.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestControl.ascx.cs" Inherits="TestControl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:Accordion ID="Accordion1" runat="server">
<Panes></Panes>
<HeaderTemplate>
<div><%# Container.DataItem %></div>
</HeaderTemplate>
<ContentTemplate>
<div>
<asp:TextBox ID="textBox" Text='<%# Container.DataItem %>' runat="server"></asp:TextBox>
<asp:LinkButton Text="Update" CommandName="Update" CommandArgument='<%# Container.DataItem %>' OnCommand="LinkButton_Command" runat="server"></asp:LinkButton>
</div>
</ContentTemplate>
</cc1:Accordion>
和 TestControl.ascx.cx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Accordion1.DataSource = new string[] { "one", "two", "three" };
Accordion1.DataBind();
}
protected void LinkButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextBox value = ((LinkButton)sender).Parent.FindControl("textBox") as TextBox;
((string[])Accordion1.DataSource)[Accordion1.SelectedIndex] = value.Text;
Accordion1.DataBind();
}
}
}
LinkButton_Command 事件处理程序在第一次单击时根本不会触发,但在第二次单击时则不会触发。创建控件的生命周期是否存在问题,导致事件无法正确连接?
更新:我静态添加控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="TestControl.ascx" tagname="TestControl" tagprefix="uc2" %>
<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div border="1">
<uc2:TestControl ID="TestControl1" runat="server" />
</div>
</form>
</body>
</html>
这是一个解决方案。我在一个测试项目中检查了这一点,这有效:
ASCX:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="WebApplication1.TestControl" %>
<%@ Import Namespace="System.ComponentModel"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:Accordion ID="Accordion1" runat="server" Enabled="True">
<Panes></Panes>
<HeaderTemplate>
<div><asp:Label runat="server" ID="HeaderLabel"><%# Container.DataItem %></asp:Label></div>
</HeaderTemplate>
<ContentTemplate>
<div>
<asp:TextBox ID="textBox" Text='<%# Container.DataItem %>' runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton1" Text="Update" CommandName="Update" CommandArgument='<%# Container.DataItem %>'
OnCommand="LinkButton_Command" runat="server"></asp:LinkButton>
</div>
</ContentTemplate>
</cc1:Accordion>
隐藏代码:
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Accordion1.DataSource = new string[] {"one", "two", "three"};
Accordion1.DataBind();
}
}
protected void LinkButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Update")
{
TextBox value = ((LinkButton)sender).Parent.FindControl("textBox") as TextBox;
(Accordion1.Panes[Accordion1.SelectedIndex].Controls[0].Controls[1] as Label).Text = value.Text;
}
}
}
手风琴数据绑定似乎存在一些问题,这些问题会扰乱您的事件处理程序的连接。每次都会以某种方式重新绑定它。
此外,您发布的代码在 LinkButton_Command 方法中调用了 DataBind(),该方法在加载视图状态后发生。这将导致更新的数据直到下一次回发才会显示,因为新的绑定不会保存在 ViewState 中。它会表现得好像总是落后一次回发。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)