LinkButton的命令事件似乎不被解雇 [英] LinkButton command event seems to not be firing
问题描述
我创建使用AJAX Control Toolkit的手风琴,文本框的LinkButton这样一个简单的用户控制和:
TestControl.ascx:
<%@控制语言=C#AutoEventWireup =真codeFILE =TestControl.ascx.cs继承=系统testControl%GT;
<%@注册大会=AjaxControlToolkit命名空间=AjaxControlToolkit标签preFIX =CC1%GT;
< CC1:手风琴ID =Accordion1=服务器>
<&窗格GT;< /窗格>
<&HeaderTemplate中GT;
< DIV><%#的Container.DataItem%GT;< / DIV>
< / HeaderTemplate中>
<&的ContentTemplate GT;
< DIV>
< ASP:文本框ID =textBox中的文本='<%#的Container.DataItem%GT;' =服务器>< / ASP:文本框>
< ASP:LinkButton的文本=更新的CommandName =更新CommandArgument ='<%#的Container.DataItem%GT;'按需=LinkButton_Command=服务器>< / ASP:LinkButton的>
< / DIV>
< /&的ContentTemplate GT;
< / CC1:手风琴>
和TestControl.ascx.cx:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;公共部分类系统testControl:System.Web.UI.UserControl
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
Accordion1.DataSource =新的String [] {一,二,三化};
Accordion1.DataBind();
} 保护无效LinkButton_Command(对象发件人,CommandEventArgs E)
{
如果(e.CommandName ==更新)
{
文本框的值=((LinkButton的)寄件人).Parent.FindControl(textBox中)的文本框;
((字符串[])Accordion1.DataSource)Accordion1.SelectedIndex] = value.Text;
Accordion1.DataBind();
}
}
}
该LinkButton_Command事件处理不火都在第一次点击,但在第二。是否有与在正在创建的控制周期,导致事件不能正确地迷上了问题?
更新:我添加静态控制:
<%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;<%@注册SRC =TestControl.ascx变量名=系统testControl标签preFIX =UC2%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT; <表ID =form1的=服务器>
< ASP:的ScriptManager ID =ScriptManager1=服务器>
< / ASP:ScriptManager的>
< DIV边界=1>
< UC2:ID系统testControl =TestControl1=服务器/>
< / DIV> < /表及GT;
< /身体GT;
< / HTML>
下面是一个解决方案。我在测试项目中选中该出来,这个作品:
ASCX:
<%@控制语言=C#AutoEventWireup =真codeBehind =TestControl.ascx.cs继承=WebApplication1.TestControl%GT;
<%@导入命名空间=System.ComponentModel%GT;
<%@注册大会=AjaxControlToolkit命名空间=AjaxControlToolkit标签preFIX =CC1%GT;< CC1:手风琴ID =Accordion1=服务器启用=真> <&窗格GT;< /窗格>
<&HeaderTemplate中GT;
< DIV>< ASP:标签=服务器ID =HeaderLabel><%#的Container.DataItem%GT;< / ASP:标签>< / DIV>
< / HeaderTemplate中>
<&的ContentTemplate GT;
< DIV>
< ASP:文本框ID =textBox中的文本='<%#的Container.DataItem%GT;' =服务器>< / ASP:文本框>
< ASP:LinkButton的ID =LinkButton1文本=更新的CommandName =更新CommandArgument ='<%#的Container.DataItem%GT;'
按需=LinkButton_Command=服务器>< / ASP:LinkButton的>
< / DIV>
< /&的ContentTemplate GT;< / CC1:手风琴>
codebehind:
公共部分类系统testControl:System.Web.UI.UserControl
{
保护无效Page_Init(对象发件人,EventArgs的发送)
{
如果(!的IsPostBack)
{
Accordion1.DataSource =新的String [] {一,二,三化};
Accordion1.DataBind();
}
} 保护无效LinkButton_Command(对象发件人,CommandEventArgs E)
{
如果(e.CommandName ==更新)
{
文本框的值=((LinkButton的)寄件人).Parent.FindControl(textBox中)的文本框;
(Accordion1.Panes [Accordion1.SelectedIndex] .Controls [0] .Controls [1]作为标签)。文本= value.Text;
}
}
}
看来,数据绑定手风琴有弄乱你的事件处理程序得到接线一些问题。再结合它的每一次核弹他们莫名其妙。
另外,你的贴code具有的DataBind()被调用的方法LinkButton_Command,视图状态已被加载后发生。这将导致不被显示,直到下一次回发更新的数据,因为新的绑定将不会被保存在ViewState中。它会像它总有一个回传后面。
I created a simple user control using the AJAX Control Toolkit Accordion, LinkButton, and TextBox like this:
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>
And 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();
}
}
}
The LinkButton_Command event handler doesn't fire at all on the first click, but on the second. Is there a problem with where the lifecycle the controls are being created that causes events not to be hooked up properly?
Update: I'm adding the control statically:
<%@ 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>
Here's a solution. I checked this out in a test project, and this works:
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>
Codebehind:
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;
}
}
}
It appears that Databinding the Accordion has some issues that mess up your event handlers getting wired up. Re-binding it every time nukes them somehow.
Also, your posted code has a DataBind() being called in the LinkButton_Command method, which is occurring after viewstate has been loaded. This would lead to the updated data not being shown until the next postback, because the new bindings wouldn't be saved in ViewState. It would act like it was always one postback behind.
这篇关于LinkButton的命令事件似乎不被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!