Get groups of entities from an entity collection with Linq and C#

If you want to get groups of entities from an entitycollection in C# you can use Linq:

Assignment class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rvl.HelperTools.Common.BE
{
public class Assignment
{
public DateTime Startdate { get; set; }
public DateTime Enddate { get; set; }
public string Customername { get; set; }
}
}

Create function

        public void CreateGroupsOfEntitiesFromEntityCollection()
{
List<Assignment> sourceAssignments = new List<Assignment>()
{
new Assignment() { Startdate = DateTime.Now.AddDays(1), Enddate = DateTime.Now.AddDays(2), Customername = "Microsoft" },
new Assignment() { Startdate = DateTime.Now, Enddate = DateTime.Now.AddDays(1), Customername = "Microsoft" },
new Assignment() { Startdate = DateTime.Now.AddDays(1), Enddate = DateTime.Now.AddDays(2), Customername = "IBM" },
new Assignment() { Startdate = DateTime.Now, Enddate = DateTime.Now.AddDays(1), Customername = "IBM" },
new Assignment() { Startdate = DateTime.Now.AddDays(1), Enddate = DateTime.Now.AddDays(2), Customername = "Google" },
new Assignment() { Startdate = DateTime.Now, Enddate = DateTime.Now.AddDays(1), Customername = "Google" }
};
var assignmentGroups = from a in sourceAssignments
orderby a.Customername, a.Startdate
group a by a.Customername into g select new { Customername = g.Key, Assignments = g };
foreach (var g in assignmentGroups)
{
Console.WriteLine(string.Format("Customername {0}", g.Customername));
foreach (var a in g.Assignments)
{
Console.WriteLine(string.Format("Assignment startdate {0}", a.Startdate));
}
}
}

Result

Customername Google

Assignment startdate 30-10-2009 11:17:47

Assignment startdate 31-10-2009 11:17:47

Customername IBM

Assignment startdate 30-10-2009 11:17:47

Assignment startdate 31-10-2009 11:17:47

Customername Microsoft

Assignment startdate 30-10-2009 11:17:47

Assignment startdate 31-10-2009 11:17:47

Open networkshare with a LinkButton by setting the href attribute of LinkButton with code behind.

If you want to open a network share with a LinkButton, you can set it’s href attribute manually with code behind:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Rvl.HelperTools.Website.Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" runat="server">Show network share with LinkButton</asp:LinkButton>
<a href="\\Myshare" id="showReportsButton">Show network share with Link</a>
</div>
</form>
</body>
</html>

        /// <summary>
/// Event fires on pageload
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
this.LinkButton1.Attributes["href"] = @"\\Myshare";
}

Use javascript in *.ascx user control for a Telerik RadGrid EditForm

If you use an *.ascx user control to add new rows to the Telerik RadGrid and want to use javascript dynamically loaded, you can use the Telerik “RadScriptBlock”:

    <telerik:RadScriptBlock ID="radScriptBlock" runat="server">
<script language="javascript" type="text/javascript">
alert('Hello');
</script>
</telerik:RadScriptBlock>

Prevent Internet Explorer, Google Chrome and Firefox from entering address bar after pressing TAB or SHIFT-TAB on the last or first element of the page

The normal behavior of a browser is to select the address bar after pressing tab on the last element of the page or shift tab on the first element on the page. Some customer who want to port a Windows application to a Web application want to copy the exact functionality of the Windows application (this is of course not a good idea) and want the tab order to stay in the web form. The following ASP .NET page shows how to make this functionality work on a web form, using CSS and JavaScript. 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rvl.HelperTools.Website._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Page</title>
<style type="text/css">
.FirstElementOnPage
{
position: absolute;
top: -120px;
border: solid 1px white;
height: 1px;
width: 1px;
}
.LastElementOnPage
{
position: absolute;
top: -100px;
border: solid 1px white;
height: 1px;
width: 1px;
}
.LastColumn
{
border-left: solid 10px white;
text-align: left;
width: 100%;
}
</style>
<script language="javascript" type="text/javascript">
var m_shiftKeyPressed = false;
// This function focusses the given element. The funcion has a parameter event,
// because the global event object is only defined in Internet Explorer.
function SetFocusToElement(event, elementId, shiftKeyValue) {
// Determine if Shift key is pressed.
if (event.shiftKey) {
// IE defines a global object event with a property shiftKey, use this property
shiftKeyIsPressed = event.shiftKey;
}
else {
// Google chrome and firefox don't define a global event object and the event onblur event parameter does not contain a property shiftKey.
// Use the determined value for m_shiftKeyPressed in the onKeyPressedDown event handler
shiftKeyIsPressed = m_shiftKeyPressed;
}
// Only focus when parameter shiftKeyValue == shiftKeyIsPressed
if (shiftKeyIsPressed == shiftKeyValue) {
// Find the element in DOM
var elementToFocus = document.getElementById(elementId);
// Must use setTimeout for google chrome, to set focus.
var statementsToExcecute = 'document.getElementById(\'' + elementId + '\').focus()';
setTimeout(statementsToExcecute, 1);
}
// Must return true, for firefox to work
return true;
}
function OnKeyUpFocusChange(event) {
// Save if shift key is pressed, for use in onblur eventhandler.
m_shiftKeyPressed = event.shiftKey;
return true;
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<input id="firstElementOnPage" type="text" class="FirstElementOnPage" />
<table>
<tr>
<td><asp:Label ID="customerLabel" runat="server" Text="Customer"></asp:Label></td>
<td class="LastColumn"><asp:TextBox ID="customerTextBox" runat="server" Text="Microsoft" onkeydown="OnKeyUpFocusChange(event);"
onblur="SetFocusToElement(event, 'submitButton', true);"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="cityLabel" runat="server" Text="City"></asp:Label></td>
<td class="LastColumn"><asp:TextBox ID="cityTextBox" runat="server" Text="Redmond"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="supplierLabel" runat="server" Text="Supplier"></asp:Label></td>
<td class="LastColumn"><asp:TextBox ID="supplierTextBox" runat="server" Text="IBM"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="productsLabel" runat="server" Text="Products"></asp:Label></td>
<td class="LastColumn"><asp:TextBox ID="productsTextBox" runat="server" Text="Windows"></asp:TextBox></td>
</tr>
</table>
<input id="submitButton" type="button" value="Submit" onblur="SetFocusToElement(event, 'customerTextBox', false);" />
<input id="lastElementOnPage" type="text" class="LastElementOnPage"  />
</form>
</body>
</html>

image

After pressing the tab key on when the Submit button has focus, the customer textbox will be focused.

Vertical align label (as block element) text and textbox text with CSS in ASP .NET

If you use a label and an input element as block element with CSS, you can’t use vertical-align, border-top, padding-top or margin-top for vertical alignment of the text values.  

image

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rvl.HelperTools.Website._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<!-- Load css styles -->
<style type="text/css">
.Main
{
background-color: white;
margin-left: auto; /* Center mainTable for >= IE6  */
margin-right: auto; /* Center mainTable for >= IE6  */
}
.LeftLabelColumn
{
display: block;
margin-left: 10px;
margin-right: 10px;
height: 22px;
padding-top: 5px;
}
.LeftValueColumn
{
display: block;
margin-left: 10px;
margin-right: 10px;
}
</style>
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="Main">
<div style="float: left; border-top: solid 1px white;">
<asp:Label ID="Label1" CssClass="LeftLabelColumn" runat="server" Text="Customer"></asp:Label>
<asp:Label ID="Label2" CssClass="LeftLabelColumn" runat="server" Text="City"></asp:Label>
<asp:Label ID="Label3" CssClass="LeftLabelColumn" runat="server" Text="Supplier"></asp:Label>
<asp:Label ID="Label4" CssClass="LeftLabelColumn" runat="server" Text="Products"></asp:Label>
</div>
<!-- Because we can't use border-top, margin-top or padding-top on a textbox, we surround the textboxes with div tags -->
<div>
<div style="padding-top: 5px;"><asp:TextBox ID="TextBox1" CssClass="LeftValueColumn" runat="server" Text="Microsoft"></asp:TextBox></div>
<div style="padding-top: 5px;"><asp:TextBox ID="TextBox2" CssClass="LeftValueColumn" runat="server" Text="Redmond"></asp:TextBox></div>
<div style="padding-top: 5px;"><asp:TextBox ID="TextBox3" CssClass="LeftValueColumn" runat="server" Text="IBM"></asp:TextBox></div>
<div style="padding-top: 5px;"><asp:TextBox ID="TextBox4" CssClass="LeftValueColumn" runat="server" Text="Windows, Office"></asp:TextBox></div>
</div>
</div>
</form>
</body>
</html>

Change div or table width and height, with CSS rules based on user screen resolution in ASP .NET by using javascript

If you want to change the width of your main div or main table element in ASP .NET, you can use margin-left: auto; and margin-right: auto; to dynamically change the width of these elements based on the browser width, but this width will vary when you change you’re browser screen and older browser (IE6) don’t support the min-width css statement. If you want to have an exact width of the main element you can set the width by using css, but then you will have to choose a fixed value for all user screen resolutions. With the use of javascript, you can change the css value dynamically based on the user screen resolution.

If the main table or main div has an exact width, you can base all other layout inside relative to this width, by using margin’s and padding’s.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rvl.HelperTools.Website._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<!-- Load css styles -->
<style type="text/css">
.Main
{
background-color:Silver;
margin-left: auto; /* Center mainTable for >= IE6  */
margin-right: auto; /* Center mainTable for >= IE6  */
width: 800px;
}
</style>
<!-- Correct css styles based on user screen resolution -->
<script language="javascript" type="text/javascript">
// Find the first css class based on parameter [className].
function GetStyleClass(className) {
// Loop all loaded css style sheets
for (var s = 0; s < document.styleSheets.length; s++) {
// Check if browser uses [rules]
var rules;
if (document.styleSheets[s].rules) {
rules = document.styleSheets[s].rules;
}
// Check if browser uses [cssRules]
if (document.styleSheets[s].cssRules) {
rules = document.styleSheets[s].cssRules;
}
// Check if browser supports css rules
if (rules) {
// Loop rules in css stylesheet
for (var r = 0; r < rules.length; r++) {
// Stop search and return rule, when classname is found
if (rules[r].selectorText == '.' + className) {
return rules[r];
}
}
}
}
// If css class not found, return null.
return null;
}
// Convert a css px value to int.
function ConvertCssPxToInt(cssPxValueText) {
// Set valid characters for numeric number.
var validChars = "0123456789.";
// If conversion fails return 0.
var convertedValue = 0;
// Loop all characters of
for (i = 0; i < cssPxValueText.length; i++) {
// Stop search for valid numeric characters,  when a none numeric number is found.
if (validChars.indexOf(cssPxValueText.charAt(i)) == -1) {
// Start conversion if at least one character is valid.
if (i > 0) {
// Convert validnumbers to int and return result.
convertedValue = parseInt(cssPxValueText.substring(0, i));
return convertedValue;
}
}
}
return convertedValue;
}
// Get main table css class
var mainClass = GetStyleClass('Main');
// Change main table width based on user screen resolution.
if (mainClass) {
// Use default css width, if screen width is less then default css width.
var width = ConvertCssPxToInt(mainClass.style.width);
// Use screen width - 50px if screen width is greather the default css width.
if (screen.width > width) {
width = screen.width - 45;
}
// Change the width of the main table
mainClass.style.width = width + 'px';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="Main">
<p>Some text</p>
<p>Some more text...</p>
</div>
</form>
</body>
</html>

Alter CSS class with javascript for usage in ASP .NET

To alter a CSS class with javascript, you can use the following function:

        // Find the first css class based on parameter [className].
function GetStyleClass(className) {
// Loop all loaded css style sheets
for (var s = 0; s < document.styleSheets.length; s++) {
// Check if browser uses [rules]
var rules;
if (document.styleSheets[s].rules) {
rules = document.styleSheets[s].rules;
}
// Check if browser uses [cssRules]
if (document.styleSheets[s].cssRules) {
rules = document.styleSheets[s].cssRules;
}
// Check if browser supports css rules
if (rules)
{
// Loop rules in css stylesheet
for (var r = 0; r < rules.length; r++) {
// Stop search and return rule, when classname is found
if (rules[r].selectorText == '.' + className) {
return rules[r];
}
}
}
}
// If css class not found, return null.
return null;
}

Usage of the function in ASP .NET:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rvl.HelperTools.Website._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<!-- Load css styles -->
<style type="text/css">
.Main
{
background-color:Silver;
margin-left: auto; /* Center mainTable for >= IE6  */
margin-right: auto; /* Center mainTable for >= IE6  */
width: 800px;
}
</style>
<!-- Correct css styles based on user screen resolution -->
<script language="javascript" type="text/javascript">
// Find the first css class based on parameter [className].
function GetStyleClass(className) {
// Loop all loaded css style sheets
for (var s = 0; s < document.styleSheets.length; s++) {
// Check if browser uses [rules]
var rules;
if (document.styleSheets[s].rules) {
rules = document.styleSheets[s].rules;
}
// Check if browser uses [cssRules]
if (document.styleSheets[s].cssRules) {
rules = document.styleSheets[s].cssRules;
}
// Check if browser supports css rules
if (rules) {
// Loop rules in css stylesheet
for (var r = 0; r < rules.length; r++) {
// Stop search and return rule, when classname is found
if (rules[r].selectorText == '.' + className) {
return rules[r];
}
}
}
}
// If css class not found, return null.
return null;
}
// Get main table css class
var mainClass = GetStyleClass('Main');
// Change main table background color if class was found
if (mainClass) {
mainClass.style.backgroundColor = 'Red';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="Main">
<p>Some text</p>
<p>Some more text...</p>
</div>
</form>
</body>
</html>

How to convert a CSS px width or height value to int with javascript

If you want to alter an elements CSS width or height, you must first convert this value to an integer, because the CSS value can have units like px, cm etc.
To convert this value to an integer in javascript, use:

        // Convert a css px value to int.
function ConvertCssPxToInt(cssPxValueText) {
// Set valid characters for numeric number.
var validChars = "0123456789.";
// If conversion fails return 0.
var convertedValue = 0;
// Loop all characters of
for (i = 0; i < cssPxValueText.length; i++) {
// Stop search for valid numeric characters,  when a none numeric number is found.
if (validChars.indexOf(cssPxValueText.charAt(i)) == -1) {
// Start conversion if at least one character is valid.
if (i > 0) {
// Convert validnumbers to int and return result.
convertedValue = parseInt(cssPxValueText.substring(0, i));
return convertedValue;
}
}
}
return convertedValue;
}