Monday, April 29, 2013

How to create image link in ASP.NET MVC?

@Html.ActionLink("buttontext", "actionName", "controllerName", null, new { @class="alinkimage" })

CSS definition:

a.classname
{
background: url(../Images/image.gif) no-repeat top left;
display: block;
width: 150px;
height: 150px;
}

Saturday, April 27, 2013

How to encode url in ASP.NET MVC 3

HttpContext.Current.Server.UrlEncode(text)

Wednesday, April 24, 2013

The best interface is no interface




http://www.cooper.com/journal/2012/08/the-best-interface-is-no-interface.html/

How to validate Canada postal code by regular expression?

^[ABCEGHJKLMNPRSTVXY]\d[A-Z]\d[A-Z]\d$
Code example for ASP.NET webform  RegularExpression validator

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*" ControlToValidate="TextBoxPostalCode" ValidationExpression="^[ABCEGHJKLMNPRSTVXY]\d[A-Z]\d[A-Z]\d$"></asp:RegularExpressionValidator>

Basic Concept
http://en.wikipedia.org/wiki/Regular_expression
Online Regular Expression tester
http://www.regular-expressions.info/javascriptexample.html

Monday, April 22, 2013

How to convert ASP.NET textbox input value into upcase right way?

Add following javascript into ASP.NET textbox control

Friday, April 19, 2013

How to add ajax validation on field in ASP.NET MVC 3?
By Remote Attribute. This way is MS designed for you.

//In Model
        [Remote("CheckExistedEmail", "ControllerName")]
        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        [RegularExpression(@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",ErrorMessage="Invalid email address")]
        public string Email { get; set; }

//In Action 
        public JsonResult CheckExistedEmail(string Email)
        {
            if (EmailExisted(Email))
            {
                return Json("Existed Email.", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

http://msdn.microsoft.com/en-us/library/gg453903(v=vs.98).aspx

Thursday, April 18, 2013

How many ways to redirect URL in Javascript?

1. window.location.href=”login.htm";
2. window.navigate(”login.htm”);
3. self.location=”login.htm”;

Any other way?

Wednesday, April 17, 2013

How to disable browser back button in asp.net?

Should make ASP.NET page cache expired instead of disable browser back button

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Tuesday, April 16, 2013

How to get active users in last 10 minutes in ASP.NET membership database?

select top 300 * from dbo.aspnet_Users u
where u.LastActivityDate > DATEADD(minute, -10, GETUTCDATE())
order by u.LastActivityDate desc

Monday, April 15, 2013

How to fix problem: missing files when publish a website in Visual Studio for ASP.NET?

In Visual Studiio, Content files need to be marked for publishing
Right click on the file missed -> Properties -> Build Action set to Content.

Thursday, April 11, 2013

How to find common items in two set of items in C#?

            var list1 = new List() { 1, 3, 4, 5 };
            var list2 = new List() { 2, 3, 4, 5 };
            var commonItems = list1.Intersect(list2);

Friday, April 5, 2013

MVC 4: facebook twitter login with oAuth

http://www.dotnetexpertguide.com/2012/08/facebook-twitter-oauth-openid-login-with-aspnet-mvc-4-application.html

Wednesday, April 3, 2013

How to set Connection: Keep-Alive in C# HttpWebRequest class ?

Cannot set by: webrequest.KeepAlive = true;
Have to set this request header by reflection
var req = (HttpWebRequest)WebRequest.Create(someUrl);

var sp = req.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);

Reference:
http://stackoverflow.com/questions/7458556/c-sharp-connection-keep-alive-header-is-not-being-sent-during-httpwebrequest

Tuesday, April 2, 2013

How to validate a URL in ASP.NET?

By Regular expression data annotation in Model class

[RegularExpression(@"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$", ErrorMessage = "Invalid url")]
public string Url { get; set; }

How to set Max length for an input in ASP.NET MVC

By StringLength data annotation

[StringLength(20, ErrorMessage = "Name cannot be longer than 20 characters.")]
public string Name { get; set; }

Monday, April 1, 2013

How to use TextArea for @Html.EditorFor in ASP.NET MVC?

Two ways:
1. Add data annotation in Model class
[DataType(DataType.MultilineText)]
public string Text { get; set; }

2. @Html.TextAreaFor(model => model.Text)

How to change EditFor width in ASP.NET MVC 3?

Should be very simple? But need to two steps to do so:
1. Replace EditFor with TextBoxFor
2. Pass html property into TextBoxFor

@Html.TextBoxFor(m => m.Name, new {style = "width:50px"})

How to add data annotations to a class without changing it?

By MetadataType class
- Inherent from it, make it to be base class
- Then use metadata class to add validation data annotations in subclass
[MetadataType(typeof(ModelMetadata))]
public class Model : Base_Class {
}


internal class ModelMetadata {
    [Required]
    public string Name { get; set; }
}