gpt4 book ai didi

asp.net - LinkBut​​ton 命令事件似乎没有触发

转载 作者:行者123 更新时间:2023-12-02 12:04:12 24 4
gpt4 key购买 nike

我使用 AJAX 控件工具包 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>

最佳答案

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

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

Accordion 数据绑定(bind)似乎存在一些问题,这些问题会扰乱您的事件处理程序的连接。每次都会以某种方式重新绑定(bind)它。

此外,您发布的代码在 LinkBut​​ton_Command 方法中调用了 DataBind(),该方法在加载 View 状态后发生。这将导致更新的数据直到下一次回发才会显示,因为新的绑定(bind)不会保存在 ViewState 中。它会表现得好像总是落后一次回发。

关于asp.net - LinkBut​​ton 命令事件似乎没有触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/735777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com