我使用AJAX Control Toolkit Accordion,LinkButton和TextBox创建了一个简单的用户控件,如下所示:
TestControl.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>
Run Code Online (Sandbox Code Playgroud)
和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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案。我在一个测试项目中检查了这一点,这有效:
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>
Run Code Online (Sandbox Code Playgroud)
隐藏代码:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
手风琴数据绑定似乎存在一些问题,这些问题会扰乱您的事件处理程序的连接。每次都会以某种方式重新绑定它。
此外,您发布的代码在 LinkButton_Command 方法中调用了 DataBind(),该方法在加载视图状态后发生。这将导致更新的数据直到下一次回发才会显示,因为新的绑定不会保存在 ViewState 中。它会表现得好像总是落后一次回发。
| 归档时间: |
|
| 查看次数: |
14190 次 |
| 最近记录: |