Register | Password Reset | Forum

Search

Tag Cloud

.net actual api arsenal arsene wenger books browser c# campaign captcha chuck cleartype code desktop economy font Football galatasaray games google graphics internet explorer kewell lambda library linux live cd margaret weis metal moba Music opengl political recaptcha rpg sign of the horns silverlight stream supernatural tv series utility windows 7 windows vista wpf xaml

Language

  • en English
  • tr Türkçe
RSS feed

Archive

Archive for the ‘Coding’ Category

How to dynamically change MasterPageFile in ASP.Net?

utku October 19th, 2009 1 comment

I have needed to change master page depending on the value which comes from QueryString in one of my ASP.Net project. I had to check the QueryString to choose MasterPageFile of the ASP.Net page according to my scenario. I wrote a little code to achieve this goal;

Here is the example;

Firstly, I’ve created my master pages and a test page;

ScreenShot

MasterPage.master

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<asp :ContentPlaceHolder id="head" runat="server">
</asp>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<strong>This is first Master Page</strong>
</td>
<td>
<asp :ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp>
</td>
</tr>
</table>
</form>
</body>
</html>

MasterPage2.master

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<asp :ContentPlaceHolder id="head" runat="server">
</asp>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<strong>This is second Master Page</strong>
</td>
<td>
<asp :ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp>
</td>
</tr>
</table>
</form>
</body>
</html>

Here is the TestPage.aspx.cs. I overwrite the PreInit method in the constructor of the page.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public TestPage()
{
this.PreInit += new EventHandler(Page_PreInit);
}
protected void Page_PreInit(object sender, EventArgs e)
{
if ((string)Request.QueryString["IsFrameless"] == "True")
{
MasterPageFile = "~/MasterPage2.master";
}
else
{
MasterPageFile = "~/MasterPage.master";

}
}
}
Bookmark and Share
Categories: Coding Tags: asp.net, masterpage

Text Clarity in WPF

laroux January 1st, 2009 No comments

WPF Font Rendering

I think everyone has noticed the font rendering difference between WPF and native Win32 forms. The TextBlock/Label objects are become more blurry by the effect of anti-aliasing when you place it beside an image together. Although it sounds like a harmless visual distortion, it has an importance nowadays when the people are getting used to next-generation look and feel.

Instead of awaiting next release of .Net Framework, you can read the article called “Give me back my ClearType” to overcome this problem.

Bookmark and Share
Categories: Coding Tags: .net, cleartype, font, graphics, wpf

Access Denied While Signing Assembly

laroux December 31st, 2008 No comments

I coped with a digital signature problem on a project I’ve been working on. After a short search on the web, I realized I’m not the only one who experienced this signing issue.

After .Net Framework 3.5 SP1, when you try to sign an assembly which is signed once, you’ll get “Cryptographic failure while signing assembly” or “Failed to generate a strong name key pair” error.

According to Zupancic Perspective, the matter is lack of ACL authorization in “%ALLUSERSPROFILE%\Application Data\Microsoft\Crypto\RSA\MachineKeys” path. This solution worked for me, when I gave read/write permissions to related users.

Bookmark and Share
Categories: Coding Tags: .net, assembly, signature

Lambda Functions in C#

laroux December 29th, 2008 No comments

This time I’m writing about a programming presence which I don’t like or prefer to use. I always find lambda functions being too complicated to evaluate while you’re taking a look at code. I’d rather use inline delegates which are introduced with C# 2.0 instead of these syntax-killers.

Nevertheless, if you think you’re a good programmer, you don’t want to reject knowledge about coding. I looked for a reference for lambda functions apart from gloomy MSDN documentation. Finally, I glad to share CodeHappy’s Introducing Lambdas in C# article.

Bookmark and Share
Categories: Coding Tags: .net, c#, code, lambda

Dynamic Code with XAML

laroux November 13th, 2008 No comments

During a XAML-related research, I came across with sample of running an additional code from external source. It would be useful if you need to write banner rotation or generated animation which is dynamically created by variables such as user’s input, time of day, a counter, etc.

using Sys = System;
using SysIO = System.IO;
using SysWinMarkup = System.Windows.Markup;
using SysXml = System.Xml;
using SysXmlLinq = System.Xml.Linq;

public static class XamlUtils {
public static object GetXamlObject(string source) {
SysXml.XmlDocument _xmlDocument = new SysXml.XmlDocument();
_xmlDocument.LoadXml(source);

return Xaml.GetXamlObject(_xmlDocument);
}

public static object GetXamlObject(SysXml.XmlDocument xmlDocument) {
const string xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
const string xmlns_x = "http://schemas.microsoft.com/winfx/2006/xaml";

if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns"))) {
xmlDocument.DocumentElement.SetAttribute("xmlns", xmlns);
}
if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns:x"))) {
xmlDocument.DocumentElement.SetAttribute("xmlns:x", xmlns_x);
}

SysIO.TextReader _stringReader = new SysIO.StringReader(xmlDocument.OuterXml);
SysXml.XmlReader _xmlReader = new SysXml.XmlTextReader(_stringReader);

return SysWinMarkup.XamlReader.Load(_xmlReader);
}
}

Usage:

System.Windows.Controls.Grid _gridObject = XamlUtils.GetXamlObject("<grid><textblock>ok</textblock></grid>") as System.Windows.Controls.Grid;
this.Content = _gridObject;

As you can see, you’re able to use a string variable that contains your dynamic XAML code to will be processed by XamlUtils.GetXamlObject. A thing to be noticed, you must cast object returned from GetXamlObject to class of xaml code’s root element.

Bookmark and Share
Categories: Coding Tags: .net, c#, code, wpf, xaml

C# Type Casting

laroux October 20th, 2008 No comments

Most people use C-based (Type)Variable syntax for casting types between. As we know .NET CLR brought another usage of type casting as (Variable as Type). We can count readability and no-exception-on-failure as differences of two casting methods. At least, so far…

Everything was okay with these until I’ve noticed Microsoft Code Analysis’s advices. I couldn’t realize there is a performance difference between two methods. I’ve always presumed choosing among these is an option. According to advice, .NET casts types more than one time when we use classic type casting method. So it wants us to choose (Variable as Type) method for better performance and preventing double-check.

Now I ask myself why Microsoft can’t build more intelligent managed code handling if I have to carry even about type castings? Whatever, after a little research I found performance comparison reports on another blog that named .NET BLOG. Also if you want to check it on MSDN, there’s an article about How to: Safely Cast by Using as and is Operators.

Bookmark and Share
Categories: Coding Tags: c#, code, type casting

Reading a Stream

laroux September 22nd, 2008 No comments

Evaluating a stream’s size with its length property is a common mistake sometimes coders do in .NET. Because length property doesn’t calculate remaining data accurately if the data transfer is still ongoing. While reading streams, we usually have to use old style “fread loops” which is always guaranteed to work.

// Such a bad idea.
var _stream = System.IO.File.OpenRead("file.bin");
var _data = new byte[_stream.Length];
_stream.Read(_data, 0, _data.Length);

The most accurate solution is writing your own utility method to copy stream into byte[]. I found an article in yoda.arachsys.com which has offered some sample methods for solution. For example:

/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
/// <param name="initialLength">The initial buffer length</param>
public static byte[] ReadFully (Stream stream, int initialLength)
{
    // If we've been passed an unhelpful initial length, just
    // use 32K.
    if (initialLength < 1)
    {
        initialLength = 32768;
    }

    byte[] buffer = new byte[initialLength];
    int read=0;

    int chunk;
    while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)
    {
        read += chunk;

        // If we've reached the end of our buffer, check to see if there's
        // any more information
        if (read == buffer.Length)
        {
            int nextByte = stream.ReadByte();

            // End of stream? If so, we're done
            if (nextByte==-1)
            {
                return buffer;
            }

            // Nope. Resize the buffer, put in the byte we've just
            // read, and continue
            byte[] newBuffer = new byte[buffer.Length*2];
            Array.Copy(buffer, newBuffer, buffer.Length);
            newBuffer[read]=(byte)nextByte;
            buffer = newBuffer;
            read++;
        }
    }
    // Buffer is now too big. Shrink it.
    byte[] ret = new byte[read];
    Array.Copy(buffer, ret, read);
    return ret;
}
Bookmark and Share
Categories: Coding Tags: c#, code, stream
Page 1 of 11

Archives

  • March 2010
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008

Recent Posts

  • A liqueur, a queen and a travel
  • Another youngster gets his chance for first-team
  • thinking design, without concept
  • The Blues Approach Of Stevie Ray Vaughan
  • How to dynamically change MasterPageFile in ASP.Net?
  • carry on wayward .com.