You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telerik RadScheduler自定义预约模板中LinkButton事件无法触发求助

Hey there, let's work through this RadScheduler custom appointment template LinkButton issue step by step—this is a common gotcha with dynamic templates in Telerik controls!

Root Cause Breakdown

The main problem here is that dynamic templates need to be recreated on every postback for ASP.NET to rebuild the control tree and wire up events correctly. If you only added the template on the first page load (!IsPostBack), on postback the template disappears entirely, which explains the refresh and missing template behavior. Plus, binding events declaratively (like OnClick in markup) can be unreliable for dynamically created controls.

Fixes to Get Your LinkButton Working

1. Recreate the Template on Every Postback

Update your Page_Load (or better yet, Page_Init) code to assign the custom template every time the page runs—including postbacks. This ensures the control tree is rebuilt properly:

protected void Page_Init(object sender, EventArgs e)
{
    // Use Page_Init instead of Page_Load for earlier control tree setup
    RadScheduler1.AppointmentTemplate = new CustomAppointmentTemplate();
}

Why Page_Init? It’s the earliest stage where you can safely add dynamic controls/templates, ensuring they’re part of the control tree before view state and event processing kicks in.

2. Bind LinkButton Events via Code (Not Declaratively)

For dynamic controls, code-based event binding is far more reliable than declarative OnClick attributes. Update your custom template class to bind the Click event directly:

public class CustomAppointmentTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        var appointmentContainer = container as AppointmentTemplateContainer;
        if (appointmentContainer == null) return;

        LinkButton btnRedirect = new LinkButton
        {
            ID = "btnViewDetails",
            Text = "View Appointment",
            // Pass appointment ID via CommandArgument for easy access
            CommandArgument = appointmentContainer.Appointment.ID.ToString()
        };

        // Bind the click event directly in code
        btnRedirect.Click += BtnRedirect_Click;

        container.Controls.Add(btnRedirect);
        // Add other template elements here...
    }

    private void BtnRedirect_Click(object sender, EventArgs e)
    {
        var btn = sender as LinkButton;
        if (btn == null) return;

        string appointmentId = btn.CommandArgument;
        // Execute your redirect logic here
        HttpContext.Current.Response.Redirect($"AppointmentDetails.aspx?id={appointmentId}");
    }
}

3. Verify ViewState & AJAX Settings

  • Ensure RadScheduler.EnableViewState is set to true (this is default, but double-check if you’ve modified it). ViewState helps ASP.NET track dynamic controls between postbacks.
  • If your scheduler is inside an UpdatePanel, add the LinkButton as a PostBackTrigger to force a full postback (AJAX partial postbacks can break event wiring for dynamic controls):
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <telerik:RadScheduler ID="RadScheduler1" runat="server" ... />
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnViewDetails" />
        </Triggers>
    </asp:UpdatePanel>
    
    Alternatively, disable AJAX for the scheduler entirely by setting RadScheduler.EnableAjax="false" if you don’t need partial page updates.
Quick Recap

The key fix is ensuring your custom template is reinitialized on every postback, paired with code-based event binding for the LinkButton. This rebuilds the control tree correctly, allowing ASP.NET to find the button and trigger its click event as expected.

内容的提问来源于stack exchange,提问作者Mr Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:58:34