JQuery 1.6+ attr and prop issue

I used the following code to make sure all radio buttons where unchecked and the current one was set correctly. This worked well before jQuery 1.6.

1
2
3
4
5
6
7
8
$(document).ready(function() {
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).attr("checked", false);
});
current.checked = true;
};
});

Since jQuery 1.6, this code did not work and after doing some digging around, I found that you need to use the new method .prop() to get the code functioning correctly again.

1
2
3
4
5
6
7
8
$(document).ready(function() {
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).prop("checked", false);
});
current.checked = true;
};
});

Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.

References:

http://api.jquery.com/prop/

Finding StoreGeneratedPattern value in EdmProperty in T4 Template

If you need to find out if a primary key in your conceptual model which is using the Identity value from the property StoreGeneratedPattern, you can use the following code: (NOTE: This is used within a T4 template)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string inputFile = @"..\EntitiesModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string annotationNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) {
   foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity)) {
 
      MetadataProperty storeGeneratedPatternProperty = null;
      edmProperty.MetadataProperties.TryGetValue(annotationNamespace + ":StoreGeneratedPattern", false, out storeGeneratedPatternProperty);

      if (storeGeneratedPatternProperty != null && storeGeneratedPatternProperty.Value.ToString() == "Identity") {
         //We found an Identity property
      }
   }
}

Back to Basics – Deleting Cookies using ASP.NET

You cannot instruct ASP.NET to physically remove cookies from the user’s computer so you have to tell the user’s browser to modify the cookie in order to trigger the browser to remove the cookie.

See code example:

1
2
3
4
5
6
7
8
9
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i=0; i {
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}

 

References:

http://msdn.microsoft.com/en-us/library/ms178194.aspx#Y5776

handling multiple submit buttons in one form (asp.net webforms)

I came across an issue where I had the following setup with an asp.net website:

  1. I had a master page with one main form element
  2. I had a submit button in the master page which acts as a search button across the website
  3. I had a login page that also has a submit button

Naturally when the user presses the enter key, it naturally takes the first submit button on the webpage as being the default submit button.   Therefore when a user enters their credentials on the login page, they will always be taken to the search page which is defined by the input button on the master page.

I used jQuery to help solve this issue, you need to do two important things:

1) You need to set a default selector on the input button you want to submit, i.e on the login button I put:

1
 <input class="buttondefault" name="myButton" type="submit" />

 

2) Use the following code (Note this code can easily be refactored to be more efficient):

1
2
3
4
5
6
7
8
9
10
11
12
13
$("form input").keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
//if the search has text then submit that button first
if ($('.buttondefault').length > 0) {
$('.buttondefault').click();
return false;
}
$('#myMasterButtonClass input[type=submit]').click();
return false;
} else {
return true;
}
});