2009-04-09 36 views
6

我創建使用AJAX Control Toolkit的手風琴,文本框的LinkBut​​ton這樣一個簡單的用戶控制和:LinkBut​​ton的命令事件似乎不能射擊

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> 
+0

你是否將用戶控件動態添加到頁面中? – womp 2009-04-09 20:22:21

回答

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中。它會像往常一樣後退。

相關問題