LinkBut​​ton命令事件似乎没有被触发

时间:2009-04-09 20:15:08

标签: asp.net

我使用AJAX Control Toolkit Accordion,LinkBut​​ton和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>

和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();
        }
    }
}

LinkBut​​ton_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>

1 个答案:

答案 0 :(得分:3)

这是一个解决方案。我在一个测试项目中检查了这个,这有效:

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;
        }
    }
}

看起来数据绑定手风琴有一些问题会让你的事件处理程序陷入困境。每次以某种方式核对它时重新绑定它。

此外,您发布的代码在LinkBut​​ton_Command方法中调用DataBind(),该方法在加载viewstate之后发生。这将导致更新的数据在下一次回发之前不会显示,因为新的绑定不会保存在ViewState中。它的行为就像它总是背后的一个回发。