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;