LinkButton command event seems to not be firing

前端 未结 1 1472
抹茶落季
抹茶落季 2021-01-13 00:40

I created a simple user control using the AJAX Control Toolkit Accordion, LinkButton, and TextBox like this:

TestControl.ascx:

<%@ Control Languag         


        
相关标签:
1条回答
  • 2021-01-13 01:00

    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.

    0 讨论(0)
提交回复
热议问题