简体   繁体   English

ASP.Net为AJAX手风琴窗格动态添加按钮和事件

[英]ASP.Net dynamically add buttons and events to AJAX Accordion Pane

I've been searching for a while on this but cant seem to get a decent answer. 我已经搜索了一段时间,但似乎无法得到一个不错的答案。 I am using an Accordion and dynmically adding Panes with grids on. 我正在使用手风琴,并动态添加带有网格的窗格。 I would also like to add a textbox and a button when each panel is added to the accordion. 当每个面板都添加到手风琴时,我还想添加一个文本框和一个按钮。

//add a comments box and a submit button to the container
            Button btnComments = new Button();
            btnComments.ID = CheckName + "btnComment" + i;
            btnComments.CommandName = "SubmitComment";
            TextBox txtComments = new TextBox();
            txtComments.ID = CheckName + "txtComment" + i;
            pane.ContentContainer.Controls.Add(new LiteralControl("<br />"));
            pane.ContentContainer.Controls.Add(txtComments);
            pane.ContentContainer.Controls.Add(new LiteralControl("<br />"));
            pane.ContentContainer.Controls.Add(btnComments);

The textbox and buttons appear in the accordion as expected but the event doesn't fire. 文本框和按钮按预期方式出现在手风琴中,但事件不会触发。 I've been reading that the event needs to be added at Page_Load but the buttons dont exist at this point. 我一直在阅读该事件需要在Page_Load中添加,但是此时按钮不存在。

I tried adding CommandEvent to the accordion then I could capture the sender but again this isnt firing 我尝试将CommandEvent添加到手风琴中,然后我可以捕获发送方,但是再次没有触发

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Panel ID="Panel1" runat="server" />
        <asp:Accordion ID="Accordion1" runat="server" AutoSize="None" 
            RequireOpenedPane="False" SelectedIndex="-1" FadeTransitions="false"  OnItemCommand="Linked_ItemCommand">
        </asp:Accordion>
        <br />
        <p>
            <asp:Button ID="btnRunChecks" runat="server" OnClick="RunChecks" Text="Run Checks" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="chkDebug" runat="server" EnableTheming="False" Text="Run in Debug mode" />
        </p>
    </ContentTemplate>
</asp:UpdatePanel>

Any ideas what I'm doing wrong - any pointers much appreciated. 任何想法,我在做什么错-任何指针,不胜感激。 When clicking the button it just causes a postback and refresh losing the dynamically created accordion panes. 单击该按钮时,只会导致回发并刷新,从而丢失动态创建的手风琴窗格。

I too have faced a similar issue and after much dismay the only solution is that there is no solution to it. 我也曾经遇到过类似的问题,在万分沮丧之后,唯一的解决方案是没有解决方案。

When you dynamically create controls, regardless of it being for accordion, the controls get created and are sent to the client, where they are rendered as per the client's browser. 当您动态创建控件时,无论是否要使用手风琴,控件都会被创建并发送到客户端,然后根据客户端的浏览器呈现它们。

* Upon postback, there is no means of re-rendering the controls back on the server as per what you had added in the page load event. * 回发后,无法按照您在页面加载事件中添加的内容重新渲染服务器上的控件。 * *

This was what I learnt when trying to dynamically add textboxes to a Gridview which can grow in the number of columns: Pre-add a large number of columns with visibility as false and then unhide only the number I require. 这是我尝试向Gridview中动态添加文本框(可能会增加列数)时学到的:预添加大量可见性为false的列,然后仅隐藏我需要的数目。

If someone out there has a better solution, I'm all game for it! 如果有人在那里有更好的解决方案,那么我将全力以赴!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM