【问题标题】:Setting an event handler on a linkbutton inside a class Appointment Template won't fire在类约会模板内的链接按钮上设置事件处理程序不会触发
【发布时间】:2018-10-11 07:21:23
【问题描述】:

我有一个 Telerik RadScheduler 控件,我正在为其创建自定义约会模板。模板成功绑定,但是在我的模板中是一个我想绑定事件的链接按钮。我可以看到事件绑定,但是它没有调用代码并点击 response.redirect,而是页面刷新并且约会模板消失。如何让事件在按钮单击时正确处理? apptemplate 被附加到页面加载中。

public class AppTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
                SchedulerAppointmentContainer aptCont = (SchedulerAppointmentContainer)container;
                Appointment app = aptCont.Appointment;
            LinkButton lbs = new LinkButton();
            lbs.ID = "btnConductAppointment";
            lbs.Text = "<div style=\"font-weight:bold;\">" + app.Attributes["ApptClientID"] + "</div><div>" + app.Attributes["Title"] + " " + app.Attributes["Surname"] + "</div>";



            //nlbs.Click += btnConductAppointment_Click;
            lbs.DataBinding += new EventHandler(label1_DataBinding);
            lbs.CausesValidation = false;



            container.Controls.Add(lbs);
        }

        private void label1_DataBinding(object sender, EventArgs e)
        {
            LinkButton target = (LinkButton)sender;
            target.Click += new EventHandler(btnConductAppointment_Click);
        }

        protected  void btnConductAppointment_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(PageDirectory.Default, true);
        }
    }

【问题讨论】:

  • 尝试调试您的代码target.Click += ... 实际执行了吗?
  • 它没有运行该代码。如果我坚持一个断点,它就不会到达那里。但是该页面正在回发。
  • DataBinding 事件在服务器控件绑定到数据源时发生。 在您的代码中,您希望在哪一行触发label1_DataBinding 事件?跨度>
  • 你为什么不在InstantiateIn方法中尝试lbs.Click += new EventHandler(btnConductAppointment_Click);
  • 所以如果页面是回发的,它不会知道事件存在,直到数据被反弹?

标签: c# asp.net webforms telerik


【解决方案1】:

将绑定放在 PageInit 上意味着事件始终存在。

protected override void OnInit(EventArgs e)
{
   RadScheduler1.AppointmentTemplate = new AppTemplate();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多