There are times when you will want to set the color of the grid lines in your GridView - however there is not to my knowledge a way of doing this declaratively. A workaround is to do this by tapping into the GridView's RowDataBound event.

GridView Gridlines


First, set the OnRowDataBound property in the markup of the GridView:



Second, set the color in the OnRowDataBound handler method:

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
     foreach (TableCell tc in e.Row.Cells)
         tc.Attributes["style"] = "border-color: #c3cecc";


Happy coding :-)

Update 06-01-2009:
See Lee's suggestion in the comments on a quicker way to do this:
Add this to the page Load method: this.GridView1.Attributes.Add("bordercolor", "c3cecc");

Most people are by now familiar with the typical usage of the Eval() and Bind() methods in a GridView. You can even use a format string with the Eval() method:

<%# Eval("[Email]", "mailto:{0}") %>


However, sometimes you may want to obfuscate an email address when displaying a contact link in a GridView. This example displays a databased contact link in the GridView only if there is an email address in the database for a particular business.

GridView Business Listing


When the column is read-only, the Eval() one-way binding method is the most appropriate choice:

   <table cellpadding="5" cellspacing="10" >
     <td style="padding-left:10px;">
       <span class="formtext"><b><%# Eval("BizName")%></b></span><br />
       <span class="formtext">
         Address: <%# Eval("Street")%>,&#160;<%# Eval("City")%>
       </span><br />
       <span class="formtext">Phone: <%# Eval("Phone")%></span><br />
       <span class="formtext">
         Email: <%# BuildContactRequest((int)Eval("BizID"), (string)Eval("Email")) %>
       </span><br />
       <span class="formtext">Website:<a href='<%# Eval("BizURL") %>'
        target="_blank"> <%# Eval("BizURL") %></a></span><br />

 // Code-behind: URL with query parameter is returned
 protected string BuildContactRequest(int bizId, string email)
   string contactURL = "";

   // Check if email is blank
   if (email == "")
     return "";
   // Contruct the Contact URL with the BizID query parameter
     contactURL += "<a href=Contact.aspx?bizParam=" + bizId + ">Contact Us</a>";
     return contactURL;

When the user clicks on the "Contact Us" link, they are directed to a Contact form which displays the recipient business name and generates an email to that business's (confidential) email address.

Contact Form


The business ID is passed as a query parameter and used to retrieve the business email address:

  if (Request.QueryString["bizParam"] != null)
    bizId = Convert.ToInt32(Request.QueryString["bizParam"]);
    DataSet ds = new DataSet();

    // Get email address from DB and store it in session state
    ds = mbidBiz.GetContactDetailsByBizID(bizId);

    contactLabel.Visible = true;
    contactLabel.Text = "Contact: " + ds.Tables[0].Rows[0]["BizName"].ToString();

    Session["Email"] = ds.Tables[0].Rows[0]["Email"].ToString();

If security is a real concern, rather than passing a query parameter, you could alternatively store and retrieve it from session state. Many small businesses today do not have their own website and may be using personal email addresses. The above example is based on an actual website I developed recently. Enjoy!


Events Calendar

by admin 16. June 2007 20:43

One of the most common features of community websites these days is the events calendar. It's also the source of one of my pet hates, the stretching of calendar cells as event title links are added for a particular day. Nothing looks worse than an event calendar with an average of one or two events per day... with that one fifteen-event day stretching the entire Calendar control down the page. This is not just an issue of esthetics. If you only display a limited number of events for a given day, pretty soon the other event organizers will be calling to ask why their events are not being shown! In such situations, a compromise is frequently called for.

Recently, I worked on such an events calendar for my local town's Business Improvement District. The client  wanted an easy way for users to see when there were events without having to dig too much to find them.I did not want to use the Calendar control to display event links. I chose instead to implement a master Calendar highlighting days that had events, and an associated events listing implemented as a pageable GridView.

During development, I encountered the following exception:

ObjectDataSource 'sourceEvents' could not find a non-generic method 'GetEventsByMonth' that has parameters: startDate, endDate, startDate1, endDate1...

This occurred in the paging event handler for the GridView. I was adding the SQL parameters programmatically and implemented a quick fix as shown below:

// Manually remove any select parameters from ParameterCollection sourceEvents.SelectParameters.Remove(sourceEvents.SelectParameters["startDate"]); sourceEvents.SelectParameters.Remove(sourceEvents.SelectParameters["endDate"]);

Then I added the same parameters as before and everything worked fine.

If I ever have to do something similar again and there are a lot of events involved, I might have to look to AJAX for a more creative alternative. Has anyone found another way of doing this? If you would like the complete code, please email me.