Monday, April 14, 2008

Google CHARTS api

In case you may need a simple charting solution, Google offers a great API for simple charts. Check it out below: All this took was an <img> tag with the source pointing to Google's chart api. http://chart.apis.google.com/chart?cht=
<img src="http://chart.apis.google.com/chart?cht=p3&chd=t:60,30,10&chs=450x100&chl=Code|Dave|Code!" />

Tuesday, April 8, 2008

Geek Speak > Not as cool as you may think

I came across this article today on Google Reader. It teaches a great lesson for any deveoper who interacts directly with non-technical users/clients.
Much like the elusive African dancing monkey who does a fantastic version of the Electric Slide to confuse their natural enemies; developers have the ability to sway entire debates in their favor by confusing their enemies with technical jargon. After all, who can argue with someone when they have no clue what the other person is talking about?

Monday, February 25, 2008

Save server memory with Response.TransmitFile()

So, two common tasks that I encounter when sending files to the browser:
  1. Open file within the browser
  2. Open "file download" dialog box

I used to use the Response.Write() method, but recently came across the Response.TransmitFile() method. This method directly writes the file to the HTTP response stream and saves you from hogging up memory on the server.

1) Open file within the browser
Response.ContentType = "application/pdf";
//write file to the browser without saving file in server memory
Response.TransmitFile("c:\docs\pdfs\test1_1.pdf");
Response.End();
2) Open "file download" dialog box
Response.ContentType = "application/pdf";
//add 'attachment' header to open 'file download' dialog box
Response.AddHeader("content-disposition", "attachment; filename=test1_1.pdf");
//write file to the browser without saving file in server memory
Response.TransmitFile("c:\docs\pdfs\test1_1.pdf");
Response.End();

Some other types of "write" methods that exist in the System.Web.HttpResponse class are

  • BinaryWrite()- use this method to output a file that is stored in a Base64 string format
    Response.BinaryWrite(Convert.FromBase64String(EncodedDocument));
  • WriteFile()-use this method if you desire for the file to be stored in memory, or if the file is already in memory. This method is expensive because it stores the file object in memory, then transmits to the client.
  • Write() -there are a few overloads for the type of objects that Write() can send to the client. It is a flexible method and TransmitFile() or BinaryWrite() may better fit your specific needs

Thursday, February 14, 2008

Indiana Jones Trailer!!

The fourth installment of Indy! http://gizmodo.com/356366/indiana-jones-iv-trailer-makes-us-wet-our-pants

Tuesday, January 22, 2008

Change Master Page styles based upon the current page

In ASP.NET, a common practice is to create a master page, and expose content regions to the .aspx pages to hold page-specific content. One way that you can dynamically change the Masterpage content (i.e. add/remove asp controls, change style values) is to determine the actual page name (xyz.aspx) that is going to be displayed.
string filePath = Context.Request.FilePath;
string pageName = filePath.Substring(filePath.LastIndexOf('/') + 1);
//pageName will now hold the name of the file requested


Once we figure out which page is being requested, we can handle the behavior.

For example, if the page "NoBorderTables.aspx" should set all the tables to have no border, you can execute some code like this:
 protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!IsPostBack)
            {
                string filePath = Context.Request.FilePath;
                string pageName = filePath.Substring(filePath.LastIndexOf('/') + 1);
                
                switch(pageName.ToLower())
                {
                    case "NoBorderTables.aspx":
                         foreach (Table t in this.Page.Controls)
                         {
                           t.BorderStyle = BorderStyle.None;
                         }
                         break;
                 )
             )
         }
                    

Friday, January 18, 2008

Gridview - Delete a row

So, I recently developed an ASP:Gridview to display a list of documents. I was having some problems accessing the datakey of the row that the "delete" button resided in. I looked around a bit, and I found a resolution on the asp.net forums. It turns out that the GridViewDeleteEventArgs object contains the current row index. The DataKeys of the gridview are stored in the same order of the row indexes. You can access the DataKey (which in my case was the document ID number) by getting the GridView.DataKeys[e.RowIndex].

Codebehind event handler for when "delete" button is clicked notice how the event handler event args type is set to "GridViewDeleteEventArgs"
protected void DeleteDocument(object sender, GridViewDeleteEventArgs e)
{         

        int DocID = Convert.ToInt32(this.ListOfDocumentsGridview.DataKeys[e.RowIndex].Value);

        Document.DeleteByID(DocID);

}

Monday, January 14, 2008

C# ternary if

Just to get some exposure for pretty specific searches, I wanted to give some examples of the ternary "if" statement in C#: If you would like to set a variable by an "if...else" condition, it can be done with the ternary if statement. (Notice I am also using the new .NET 3.5 "var" to declare my variable). In the example below, I want to see if I am old enough (25 yrs old) to rent a car without the "underage" fee. Is today 25 years or more from my date of birth:
DateTime myBDay = "02/08/1982";
var AmIOldEnough = DateTime.Today >= myBDay.AddYears(25) ? true : false;