This is a migrated thread and some comments may be shown as answers.

Call editAppointment from Code Behind

7 Answers 171 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 30 Mar 2012, 11:59 PM
Hi,

I am reviewing a sample on how to edit and insert an appointment with a single click. This sample is easy enough to understand and to implement. But I was interested in putting a "twist" on this. This sample provides a JavaScript function editAppointment(), whick places an appointment in edit mode. I would like to call this function from code-behind, passing a RadScheduler and a SchedulerEventArgs to the function.

I am thinking, for examle, of placing an asp:Button on the same page as my Scheduler control, and then creating an OnClick event for the button. Then I would like to call editAppointment() from the button's Click event. Before I click the button, I could capture the SchedulerEventArgs by first clicking on an appointment, and using the radScheduler_AppointmentClick() event to capture the value of SchedulerEventArgs. Then I would click my button to call and execute editAppointment(). But I don't know how to call this javascript function from within my button click event.

Anyone know how to do this?

Thanks,
Steven

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 04 Apr 2012, 09:07 AM
Hi Steven,

 
If you want to start editing an appointment after a button click all you need is the id of the appointment:

protected void rb_Click(object sender, EventArgs e)
    {
        scheduler.ShowAdvancedEditForm( scheduler.Appointments.FindByID(52));
    }

You can get the desired id from the OnClientAppointmentClick, save its value in a hidden field and after that get it from there when clicking on the button.

Hope this will be helpful.


All the best,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Brian
Top achievements
Rank 1
answered on 16 Apr 2012, 05:49 PM
Plamen,

Thanks for your reply. In the example that you provided, does your value of 52 represent an appointment ID? If I understand correctly, the value passed to FindByID() must be a valid ID number for some appointment. But when I apply that to my situation, I get a NullReferenceException on the ShowAdvancedEditForm() command-line. Here is what I did.

I added <asp:HiddenField ID="hfApptID" runat="server" /> to my ASPX page.

Then within my <script type="text/javascript"> I added:
function getApptID(sender, eventArgs) {
    var varAppt = eventArgs.get_appointment();
    document.getElementById("hfApptID").value = varAppt.get_id();
}

Then I went to my telerik:RadScheduler ID="radScheduler" and I added OnClientAppointmentClick="getAppID".

Then to my ASPX page I also added a <telerik:RadButton ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" ToolTip="Edit Appointment" Visible="true"></telerik:RadButton>. And in the code-behind for this button, I added:
protected void btnEdit_Click(object sender, EventArgs e) {
    Appointment apptToEdit = new Appointment();
    object objHidFld = new object();
 
    objHidFld = this.hfApptID;
    apptToEdit = this.radScheduler.Appointments.FindByID(objHidFld.ToString());
 
    this.radScheduler.ShowAdvancedEditForm(apptToEdit);
}

When I browse my ASPX page, I click on the appointment that appears in the scheduler, and then I click [Edit]. The command-line ShowAdvancedEditForm(), line 5 above, always returns a Null reference exception. If I step through the code, I see that the reason why line 5 throws a Null exception is because apptToEdit on line 4 is equal to Null. I am not sure why apptToEdit is a Null when I can verify that objHidFld on line 3 has a valid value of "61". IOWs, the JavaScript correctly assigns a value of 61 (the appointment ID) to the hidden field. If 61 is a valid ID, then I don't understand why line 4 is returning a Null.

If I change line 4 and replace objHidFld.ToString() with a literal, such as "61" or 61, line 4 still returns a Null. IOWs, even when I pass a literal string of "61" or a literal integer of 61 to the FindByID() method, apptToEdit on line 4 continues to be a Null.

What am I doing incorrectly?
0
Plamen
Telerik team
answered on 19 Apr 2012, 01:04 PM
Hello Steven,

 
Here is the code that helped me achieve this functionality at my side :

function OnClientAppointmentClick(sender, eventArgs) {
    var appointment = eventArgs.get_appointment();
    var idToEdit;
    if (appointment.get_recurrenceState() == Telerik.Web.UI.RecurrenceState.NotRecurring) {
              idToEdit=appointment.get_id();
            }
            else if (appointment.get_recurrenceState() == Telerik.Web.UI.RecurrenceState.Master) {
               idToEdit=appointment.get_id();
            }
            else {
               idToEdit=appointment.get_recurrenceParentID()
            }  
    document.getElementById("hfApptID").value = idToEdit;
}
protected void btnEdit_Click(object sender, EventArgs e)
   {
       Appointment apptToEdit = new Appointment();
 
 
       apptToEdit = this.RadScheduler1.Appointments.FindByID(Int32.Parse(hfApptID.Value));
 
       this.RadScheduler1.ShowAdvancedEditForm(apptToEdit,true);
   }

I am attaching my sample project as well.

Hope this will help you. 

Greetings,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Brian
Top achievements
Rank 1
answered on 20 Apr 2012, 10:05 PM
Plamen,
Thank you again for your reply and for the information you provided, including your sample project files.

I wish I could say that the info and files, which you provided, had helped me to resolve this issue, but I am still encountering a Null reference exception. The command line apptToEdit = this.radScheduler.Appointments.FindByID(intAppID); still returns a Null value.

After reviewing the sample project files that you provided, the primary difference that I see between your files and mine is that you are assigning an XML Provider to your scheduler control, whereas I am assigning a DataSource to mine (a SQL Server database table). Is the FindByID method supported with a data source? I am having a difficult time finding documentation on the FindByID method.

Since your project files were very helpful in allowing me to see the difference between what you are doing vs. what I am doing, I would like to provide you with my own sample files so that you can see exactly what I am doing. But it appears that I am only allowed to attach image or HTML files to this post. Is there anyway that I can send you a ZIP that contains my sample files?

Please; I need someone to help me figure out why this does not work.

Thank you,
Steven
0
Plamen
Telerik team
answered on 25 Apr 2012, 09:02 AM
Hello Steven,

There should be no difference when RadScheduler is bound to SQLDateSource with the proper data structure. I am attaching the project where this scenario is implemented. 

Hope this will be helpful.

Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
David
Top achievements
Rank 1
answered on 22 Aug 2012, 01:18 PM
I've found there are some cases where this is true for me as well. Using FindById would throw exceptions constantly as the Appointments collection was apparently missing something.

I've opted for using a LINQ query to pull the appointment out of RadScheduler.Appointments by an ID, but I still find that this throws exceptions on occasion. There is no real reason I can see why the appointment would suddenly drop out of the appointment collection.

Any help into this issue is appreciated.
0
Peter
Telerik team
answered on 22 Aug 2012, 06:29 PM
Hello David,

Can you send us a sample of the issue via a support ticket?

All the best,
Peter
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Scheduler
Asked by
Brian
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Brian
Top achievements
Rank 1
David
Top achievements
Rank 1
Peter
Telerik team
Share this question
or