简体   繁体   English

动态创建的LinkBut​​ton导致UpdatePanel执行完全回发

[英]Dynamically Created LinkButton causing UpdatePanel to do Full Postback

Ok, so I'm creating a custom calendar (the one's I've found for free don't fit my needs or are too clunky and the paid ones are well not free) in ASP.NET. 好的,所以我要在ASP.NET中创建一个自定义日历(我免费找到的日历不适合我的需求,或者太笨拙,付费的日历也不是免费的)。 I'm using a number of UpdatePanels to allow for the changing of months and the display of the calendars. 我正在使用许多UpdatePanels来允许更改月份和显示日历。

Basically the flow is like this, the upper UpdatePanel contains the three LinkButtons lbPrev, lbCurr, and lbNext and one Label lblYear. 基本上,流程是这样的,上部的UpdatePanel包含三个LinkBut​​tons lbPrev,lbCurr和lbNext,以及一个Label lblYear。 lbPrev switches the calendar to the previous month, lbNext to the next month and lbCurr does nothing - I just haven't changed it over to a label yet (I may add something when clicking it - like a monthly display I'm not certain). lbPrev将日历切换到上个月,lbNext切换到下个月,并且lbCurr不执行任何操作-我只是还没有将其更改为标签(我可能会在单击时添加一些内容-就像我不确定每月的显示) 。

When clicking any of these, the UpdatePanel works exactly as it is supposed to, updating only the panel itself and not a full postback. 单击其中任何一个时,UpdatePanel都可以正常工作,仅更新面板本身,而不更新整个回发。

The second of the UpdatePanels, we'll call it udpCalMain, contains the heart of the calendar (all the dates) this is done via an asp:Placeholder control that has LinkButton dynamically rendered (with their text being a couple of divs and an image). 第二个UpdatePanels,我们将其称为udpCalMain,它包含日历的心脏(所有日期),这是通过具有动态呈现LinkBut​​ton的asp:Placeholder控件完成的(其文本是几个div和一个图像) )。

The idea is that when you click one of the dynamically created date cells on the calendar, the system updates the third UpdatePanel "udpDisplay" with pertinent details from a database for that day. 这个想法是,当您单击日历上动态创建的日期单元格之一时,系统会使用当天数据库中的相关详细信息更新第三个UpdatePanel“ udpDisplay”。

Now, what happens is that whenever you click any date, the page is forced to do a full postback - I know this is from the fact that a dynamically created LinkButton renders as a simple tag and this automatically forces a postback. 现在,发生的事情是,每当您单击任何日期时,页面都将被强制执行完整的回发-我知道这是因为动态创建的LinkBut​​ton呈现为简单标记,并且会自动强制回发。

What I've tried to do is create an AsyncPostBackTrigger and attach it for each LinkButton that is dynamically created. 我试图做的是创建一个AsyncPostBackTrigger并将其附加到每个动态创建的LinkBut​​ton上。 However, I cannot find (inside the PlaceHolder control) a listing of the controls and their ids so as that I may attach this trigger. 但是,我找不到(在PlaceHolder控件内部)控件及其ID的列表,以便可以附加此触发器。

Any help on doing this or another way of avoiding the full postback would be greatly appreciated. 这样做或避免完全回发的其他方式的任何帮助将不胜感激。

Here is the code I've used to attach the AsyncPostBackTrigger: 这是我用来附加AsyncPostBackTrigger的代码:

    AsyncPostBackTrigger apbtDate = new AsyncPostBackTrigger();
    apbtDate.ControlID = "ctl00$cpMain$CalendarBase$ctll01"; //Just an example
    apbtDate.EventName = "Click";
    udp1.Triggers.Add(apbtDate);

And here is the display code for the UpdatePanels: 这是UpdatePanels的显示代码:

    <asp:UpdatePanel ID="udp1" runat="server" EnableViewState="True" ChildrenAsTriggers="False" UpdateMode="Conditional">
        <ContentTemplate>
            <div style="width: 700px; float: left">
                <div style="width: 700px; text-align: center">
                    <asp:Label ID="lblYear" runat="server" />
                </div>
                <div style="float: left; width: 200px; text-align: center">
                    <asp:LinkButton ID="lbPrev" runat="server" OnClick="lbPrev_Click" />
                </div>
                <div style="float: left; width: 300px; text-align: center">
                    <asp:LinkButton ID="lbCurr" runat="server" />
                </div>
                <div style="float: left; width: 200px; text-align: center">
                    <asp:LinkButton ID="lbNext" runat="server" OnClick="lbNext_Click" />
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="udpCalMain" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
            <div style="width: 700px; height: 700px">
                <asp:PlaceHolder ID="phCalendar" runat="server" />
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="udpDisplay" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
    <ContentTemplate>
        <div style="width: 300px; float: left; height: 35px">

        </div>
        <div style="width: 300px; float: left">
            <asp:Label ID="lblTest" runat="server" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

Basically, I need a way to create clickable elements inside the Placeholder that will not cause a full Postback. 基本上,我需要一种在占位符内创建可点击元素的方法,该元素不会引起完整的回发。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Okay, so I stumbled upon the answer. 好吧,所以我偶然发现了答案。 I simply added a ID to the LinkButton in the codebehind, I had not considered this to this point. 我只是在后面的代码中向LinkBut​​ton添加了一个ID,到目前为止我还没有考虑这一点。 But, apparently the unnamed (no ID that is) LinkButtons simply triggered a default behavior that caused the full postback. 但是,显然,未命名(没有ID)的LinkBut​​ton只是触发了导致整个回发的默认行为。 Naming the LinkButton then caused the resulting control in PlaceHolder to function properly and not cause the full postback. 然后命名LinkBut​​ton会导致PlaceHolder中的结果控件正常运行,并且不会引起完整的回发。

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

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