加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – LinkBut??ton命令事件似乎没有被解雇

发布时间:2020-12-16 03:50:46 所属栏目:asp.Net 来源:网络整理
导读:我使用 AJAX Control Toolkit Accordion,LinkBut??ton和TextBox创建了一个简单的用户控件,如下所示: TestControl.ascx: %@ Control Language="C#" AutoEventWireup="true" CodeFile="TestControl.ascx.cs" Inherits="TestControl" %%@ Register Assembly="
我使用 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>

解决方法

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

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","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中.它的行为就像它总是背后的一个回发.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读