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!
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.
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.EnableViewStateis set totrue(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 aPostBackTriggerto force a full postback (AJAX partial postbacks can break event wiring for dynamic controls):
Alternatively, disable AJAX for the scheduler entirely by setting<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <telerik:RadScheduler ID="RadScheduler1" runat="server" ... /> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="btnViewDetails" /> </Triggers> </asp:UpdatePanel>RadScheduler.EnableAjax="false"if you don’t need partial page updates.
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

