1

I have a superb Web Part downloaded from Codeplex which essentially is a Countdown Timer in jQuery. (spCountdown) The code itself works perfectly, however I want to tweak it, in what I thought would be a relatively easy thing to do.

In the Web Part properties of the solution, there is a textbox where you enter the Date/Time which you wish to set the countdown timer. When I click 'OK' to submit the WebPart, I would like the date value in the textbox to be added to a List as a new List item. I've got the code to add the List item (or what I think is the correct code) but what I can't do is get the function to execute when the WebPart 'OK' button is clicked.

Now, I have downloaded the source code which is available but I can't fathom how to hook the 'Update List Item' code onto the onclick event of the button (as I can't find an onclick event!).

My code to add the date to the new List:

// create item in Deadline Configuration List using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb oWeb = oSiteCollection.OpenWeb()) { SPList oList = oWeb.Lists["DLConfig"]; SPListItem oListItem = oList.Items.Add(); oListItem["Title"] = "Deadline Date"; oListItem["TaskDueDate"] = new DateTime(WebPart.TargetDate.Year, WebPart.TargetDate.Month, WebPart.TargetDate.Day); oWeb.AllowUnsafeUpdates = true; oListItem.Update(); } } 

Original WebPart Code:

SPCountdownWebpart.cs

using System; using System.ComponentModel; using System.Web.UI.WebControls.WebParts; namespace SPCountdown.SPCountdownWebPart { [ToolboxItemAttribute(false)] public class SPCountdownWebPart : WebPart { // Visual Studio might automatically update this path when you change the Visual Web Part project item. private const string _ascxPath = @"~/_CONTROLTEMPLATES/SPCountdown/SPCountdownWebPart/SPCountdownWebPartUserControl.ascx"; protected override void CreateChildControls() { var control = Page.LoadControl(_ascxPath); if (control != null) { ((SPCountdownWebPartUserControl)control).WebPart = this; } Controls.Add(control); } [Category("Countdown Settings"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Target Date/Time"), WebDescription("Please enter the target date/time for countdown.")] public DateTime TargetDate { get; set; } } } 

SPCountdownWebpartUserControl.cs

using System; using System.Web.UI; using Microsoft.SharePoint; namespace SPCountdown.SPCountdownWebPart { public partial class SPCountdownWebPartUserControl : UserControl { public SPCountdownWebPart WebPart { get; set; } public SPCountdownWebPartUserControl() { PreRender += SPCountdownWebPartUserControl_PreRender; } void SPCountdownWebPartUserControl_PreRender(object sender, EventArgs e) { // parse date from properties var targetDateString = string.Format("{0},{1},{2},{3},{4},{5}", WebPart.TargetDate.Year, WebPart.TargetDate.Month - 1, //uses 0-based index WebPart.TargetDate.Day, WebPart.TargetDate.Hour, WebPart.TargetDate.Minute, WebPart.TargetDate.Second); // js sources const string jqueryScript = "<script type='text/javascript' src='/_layouts/SPCountdown/js/jquery-1.10.2.min.js'></script>"; const string countdownScript = "<script type='text/javascript' src='/_layouts/SPCountdown/js/jquery.countdown.min.js'></script>"; // create javascript implementing countdown const string jsCountdownFormat = "<script type='text/javascript'>$(function () {{var targetDate = new Date({0});$('#defaultCountdown').countdown({{until: targetDate}});}});</script>"; var jsCountdown = string.Format(jsCountdownFormat, targetDateString); // register client scripts var cs = Page.ClientScript; if (!cs.IsClientScriptBlockRegistered("jQuery")) cs.RegisterClientScriptBlock( GetType(), "jQuery", jqueryScript); if (!cs.IsClientScriptBlockRegistered("jsCountdownScript")) cs.RegisterClientScriptBlock( GetType(), "jsCountdownScript", countdownScript); if (!cs.IsClientScriptBlockRegistered("jsCountdown")) cs.RegisterClientScriptBlock( GetType(), "jsCountdown", jsCountdown); } protected void Page_Load(object sender, EventArgs e) { } 

Can anyone help me with where I need to implement my code to get the List Item to be created when the Web Part properties are set?

1 Answer 1

0

You can add that feature by changing the below

private DateTime _targetDate; [Category("Countdown Settings"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Target Date/Time"), WebDescription("Please enter the target date/time for countdown.")] public DateTime TargetDate { get { return _targetDate; } set { _targetDate = value; //CODE TO INSERT NEW LIST ITEM GOES HERE } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.