Sep 21

It is not the way to fight spams on blogs, but for professional look for websites, duplicate comments should not be allowed. This feature is common among most popular blogs and forums, so i bring this simple extension to add this feature to BlogEngine.

To enable this extension just upload it to your extension directory under App_Code.

The extension is attached with this post.

ValidateComments.cs (1,014.00 bytes)

Sep 04

BlogEngine.NET SendMailMessage issue

Posted By Ahmed El-Kilani On 04 Sep 2009 3 Comments »
BlogEngine.NET is one of the best .NET blogs over there. I have changed to blogengine for its simplicity and themes support and customizability.
Sending mail from BlogEngine sometimes does not work. Why?
BlogEngine uses different email addresses as "Sender email" i.e "From", for example: the email of the comment poster is used as the sender notification email.
Most of the SMTP severs like godaddy only replay emails sent from only addresses hosted at the same domain as the blog is hosted, for security and spam-fighting reasons.

To fix this issue add the following to BlogSettings class under BlogEngine.Core:
/// 
/// Gets or sets the e-mail address notifications are sent from.
/// 
public string SenderEmail { get; set; }
Then we need to add the following node to settings.xml under BlogEngine.Web/App_Data
sendername@example.com
And finally a little modification in SendMailMessage method in Util class under BlogEngine.Core
/// 
/// Sends a MailMessage object using the SMTP settings.
/// 
public static void SendMailMessage(MailMessage message)
{
	if (message == null)
		throw new ArgumentNullException("message");

	try
	{
        //Override sender email which was set from contact or comment forms:
        MailAddress senderEmail = new MailAddress(BlogSettings.Instance.SenderEmail, message.From.DisplayName);
        message.From = message.ReplyTo = message.Sender = senderEmail;
		message.IsBodyHtml = true;
		message.BodyEncoding = Encoding.UTF8;
		SmtpClient smtp = new SmtpClient(BlogSettings.Instance.SmtpServer);
        // don't send credentials if a server doesn't require it,
        // linux smtp servers don't like that 
        if (!string.IsNullOrEmpty(BlogSettings.Instance.SmtpUserName)) {
		    smtp.Credentials = new System.Net.NetworkCredential(BlogSettings.Instance.SmtpUserName, BlogSettings.Instance.SmtpPassword);
        }
		smtp.Port = BlogSettings.Instance.SmtpServerPort;
		smtp.EnableSsl = BlogSettings.Instance.EnableSsl;
		smtp.Send(message);
		OnEmailSent(message);
	}
	catch (SmtpException)
	{
		OnEmailFailed(message);
	}
	finally
	{
		// Remove the pointer to the message object so the GC can close the thread.
		message.Dispose();
		message = null;
	}
}
May 20

Modal Ajax UpdateProgress Control

Posted By Ahmed El-Kilani On 20 May 2008 No Comments »

UpdateProgress control which is included with AjaxExtentions works with UpdatePanel control to keep users informed about the progress of the partial postback.
UpdateProgress is important for users to know the current process which is processed by UpdatePanel , so that users is going to wait until the process has completed, clicking a button more than one time or refreshing the page is a bad action usually taken by users in the same situations.

So, I have developed this UpdateProgress to prevent users from preforming any action until UpdatePanel progress is complete. This class is a web control that overrides the UpdateProgress template and inject a modal template which is like AjaxModalPopupExtender, disables page controls until the process is complete.

namespace AjaxedControls
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.ComponentModel;

    /// 
    /// Summary description for AjaxUpdateProgress
    ///    
    public class AjaxUpdateProgress : Control
    {
        UpdateProgress ajaxUpdateProgress;
        
        protected override void  OnLoad(EventArgs e)
        {
            ajaxUpdateProgress = new UpdateProgress();
            ajaxUpdateProgress.ID = "ajaxUpdateProgress";
            ajaxUpdateProgress.DisplayAfter = 0;
            ajaxUpdateProgress.ProgressTemplate = new CustomProgressTemplate(_loadingHTMLText);
            this.Controls.Add(ajaxUpdateProgress);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //For flexible use (only one .cs file) i added the css style script here.
            //If you are to compile this control into a separate assembly, it is better to move this script to an external .css file.
            writer.Write(@"
                ");

            base.Render(writer);
        }

        #region Properties

        string _loadingHTMLText = "Loading...";

        [Description("HTML text displayed in the loading area.")]
        [DefaultValue("Loading...")]
        public string LoadingHTMLText
        {
            set
            {
                _loadingHTMLText = value;
            }
            get
            {
                return _loadingHTMLText;
            }
        }

        Boolean _renderCss = true;

        [Description("Gets or sets whether to render css styles for the loading text, else you can add your custom styles to the page.")]
        [DefaultValue(true)]
        public Boolean RenderCss
        {
            get { return _renderCss; }
            set { _renderCss = value; }
        }

        #endregion
    }



    /// 
    /// Progress template for AjaxUpdateProgress control.
    /// 
    internal class CustomProgressTemplate : ITemplate
    {

        internal CustomProgressTemplate() { }

        internal CustomProgressTemplate(string loadingHTMLText)
        {
            _loadingHTMLText = loadingHTMLText;
        }

        #region ITemplate Members

        public void InstantiateIn(Control container)
        {
            //Override UpdateProgress progress template.
            container.Controls.Add(new LiteralControl(@"
            
            
            
{0}
".Replace("{0}", _loadingHTMLText) )); } string _loadingHTMLText; public string LoadingHTMLText { set { _loadingHTMLText = value; } } #endregion } }
Jan 05

Ajax/CSS cool wizard

Posted By Ahmed El-Kilani On 05 Jan 2008 No Comments »

First see Online Demo
A cool look-and-feel wizard for navigation between steps is one of the most precious addin to your pages. I come today with a creative technique, which i have created a month ago, to feature it out.
Let's start out:
1- I have used jquery for its easy ajax support, if you do not know what jquery is I advise you to visit this.
2- CSS filter Apply and Play:

el.style.filter "progid:DXImageTransform.Microsoft.Wipe(GradientSize=0.2, duration=0.7, wipeStyle=0, motion='forward')"
el.filters[0].Apply()
el.innerHTML data.toString()
el.filters[0].Play();

Wipe filter creates the feel of gradient transition.
For more about transition filters click here.
-Apply() function saves the current state of the element "el" in the memory
-el.innerHTML = data.toString(); does not change the "el" state.
-Play() function plays the transition effect between the saved state (data before calling apply()) and the new one.

3-AjaxLoader.aspx
Each step is a user control which is loaded dynamically into AjaxLoader.aspx. Jquery $.post() function requests AjaxLoader.aspx and inject the response into "ajaxsite" DIV element.
AjaxLoader.aspx must not contain html, head, body or form tags since it will be loaded into "ajaxsite" div element.

4-AjaxPost():

function AjaxPost(url ,paramz, target , dir)
{        
    
//paramz: parameters to send to the page using POST method
    
$.post(url ,paramz, function(data){         
        
//Callback after the response of the ajax request
        
var el $('#' + target)[0];
        
        var 
motion 'forward';
        if
(dir == "rtl")motion 'reverse';
                
        
el.style.filter "progid:DXImageTransform.Microsoft.Wipe(GradientSize=0.2, duration=0.7, wipeStyle=0, motion='"+motion+"')";
               
        
el.filters[0].Apply();        
        
el.innerHTML data.toString();
        
el.filters[0].Play();        
    
});
}

This function call $.post function then updates the data into the target DIV by appying Wipe filter.
motion parameter specifies the direction of the transition.

Pretty cool, isn't it?

Demo with source code is available within the attachment.

jqAjax.rar (176.76 KB)
Nov 24

Display Arabic properly in your ASPX pages

Posted By Ahmed El-Kilani On 24 Nov 2007 2 Comments »

One of the problems that beginners to web hosting face when publishing Arabic contents is false encoding to charecters existing in .aspx or .ascx files.
Because of the default encoding in your system is Arabic visual studio IDE automatically saves web files into windows-1256 encoding, but webhost server does not usually has Arabic (windows-1256) as the default encoding. So each request or response to web files hosted on such hosts will be wrongly encoded.
To solve this issue we have two options:

1- Fortunetly ASP .Net can force encoding of requests and responses to any web application by adding the following line to the system.web section in the Web.config file:

<globalization requestEncoding="windows-1256" responseEncoding="windows-1256" fileEncoding="windows-1256"/>

But this solution will not work if this encoding is not supported by the server machine, so if not working go to the next option.

2- Make visual studio IDE save your files into unicode encoding, because unicode charecters are 2 bytes instead of 1, so unicode will work properly with any language. To do so with visual studio; from File menu click Save As:
 
Choose UTF-8 without signature from the encoding combo; then save the file.