Search

Categories

Send mail to the author(s) E-mail

# Thursday, August 05, 2010

I’m getting data using LINQ to SQL, then showing on the front end.  If I want a null coming from the database to default to something:

<telerik:GridTemplateColumn HeaderText="Image" UniqueName="Image">  
<ItemTemplate>
<asp:Label ID="asdf" runat="server" Text='<%# String.IsNullOrEmpty(Convert.ToString(Eval("Image1"))) ? "noImage.jpg" : Eval("Image1")%>'></asp:Label>
<a href="<%=VirtualPathUtility.ToAbsolute("~/")%>showFrontEndMaterialDetail.aspx?materialId=<%# Eval("Id")%>">
<img src="<%=VirtualPathUtility.ToAbsolute("~/")%>Images/Uploaded/Thumbs/<%# Eval("Image1") ?? "noPhoto.jpg" %>"></img></a>

</ItemTemplate>
</telerik:GridTemplateColumn>


Thanks to these people:
 

http://stackoverflow.com/questions/3410942/linq-to-sql-default-value-nullable

Comments [0] | | # 

When displaying a repeater on an aspx page:

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "FileName")%>
</ItemTemplate>
</asp:Repeater>

or shorter:

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#Eval("FileName")%>
</ItemTemplate>
</asp:Repeater>

It is important to remember to make FileName a property and not just a public field ie this works

public class ImageThing
{
public string FileName {get; set; }
}

private void DisplayThumbnailImages()
{
ImageThing imageThing1 = new ImageThing();
ImageThing imageThing2 = new ImageThing();
imageThing1.FileName = "asdf.jpg";
imageThing2.FileName = "aaa.jpg";

List<ImageThing> imagesToRender = new List<ImageThing>();
imagesToRender.Add(imageThing1);
imagesToRender.Add(imageThing2);

Repeater1.DataSource = imagesToRender;
Repeater1.DataBind();
}

this doesn’t

public class ImageThing
{
public string FileName;
}

A property encapsulates a field.

Comments [0] | | # 
# Wednesday, June 23, 2010

While trying to figure out what is meant by the 3rd new section in a view in MVC

<%: Html.ActionLink("Edit", "Edit", new { id=item.DinnerID }) %>

So it is really creating an object with a single property id, which is an int, equal to that of the item, which is a Dinner.

image

“Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type”

class Program
{
static void Main(string[] args)
{
// Anonymous types provide a convenient way to encapsulate a set of read-only properties
// into a single object without having to first explicitly define a type
var person = new { Name = "Terry", Age = 21 };
Console.WriteLine("name is " + person.Name);
Console.WriteLine("age is " + person.Age.ToString());

// Anonymous types just get rid of Person1 really.
Person1 person1 = new Person1 { Name = "Bill", Age = 55 };
Console.WriteLine("name is " + person1.Name);
Console.WriteLine("age is " + person1.Age.ToString());
}
}

class Person1
{
public string Name { get; set; }
public int Age { get; set; }
}

Many thanks to the question here for clarifying:
 
Comments [0] | | # 
# Thursday, February 11, 2010

Static classes are usually used as ‘utility’ classes

You don’t need an instance

 

class Program
{
static void Main()
{
double result = thing.daveSubtract(10);
Console.WriteLine(result);
}
}

static class thing
{
static public double daveSubtract(double number)
{
return (number - 2);
}
}

Extension Methods

From: http://msdn.microsoft.com/en-us/library/bb383977.aspx

Extension methods enable you to "add" methods to existing types without creating a new derived type,

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

For client code, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

 

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "hello extension methods are good";
Console.WriteLine(s);
int i = s.WordCount();
Console.WriteLine("number of words is {0}", i.ToString());
}
}

public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}

Comments [0] | | #