Tuesday, November 10, 2009

How to make a DIV that is static and non scrolling

This is an easy, yet highly effective trick.

If you want to create a region of your page that has text or a picture that does not move when the page scrolls, you can try using something like this in your CSS/style definition:


<style>

div.LockedInPlace
{
margin: 0px;
border: 2px solid #000000;
text-align: left;
width: 250px;
height: 70px;
position: fixed;
top: 3px;
left: 3px;
background-color: #CCCCFF;
font-weight: bold;
}

</style>


<div class="LockedInPlace">

This block will not scroll with the page.

</div>


Using this code as a template, you should see a blue box that is 250x70 in the upper left hand corner of your page.

Determining Rowcount or Recordcount of a Gridview

If you have enabled Paging for a Gridview and attempt to get a count of all the records in the Gridview, you are in for a surprise.

Trying to use Gridview.Rows.Count will only show the number of Rows returned in the CURRENT page.  So if you have these properties set:

AllowPaging="True"
PageSize="10"

The highest value the Gridview.Rows.Count will return is 10.

Assuming you have used a SQLDataSource to populate the Gridview, you can use the SQLDataSource's Selected action to get the total record count that was retrieved.  For example:

Protected Sub sqlData_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles sqlData.Selected

GridView2.Caption = "Total records returned: " & e.AffectedRows.ToString

End Sub

How to find the full name of a control to use with the FindControl function

When I was first starting out, I found the FindControl function to be very confusing.

Under what circumtances would I need to use this? How come sometimes Intellisense kicks in and recognizes the name of the control as you type it and other times you have to manually dig through the control hierarchy using the FindControl function?

From what I can figure out, if the control you want to manipulate is at the top level of the page (the Page object), Intellisense will detect it as you type. However, if the control you want to manipulate is in a Masterpage or other container, Intellisense can not dig through the hierarchy to find the control.

There are several posts about this issue out there, and some people have even written recursive FindControl functions that take some of the work out of it. Supposedly, such a function will have a performance hit, especially in complex hierarchies. So if you don't want to use this, here is an easy way to determine the full name of the control so you can get on on with your day:

As my project has evolved, the hierarchy of objects has necome more complex.  I find myself using stacked FindControls to find my controls.  For example, I am trying to programmatically reach the textbox control named txtClientName on my page.  Here is the code I need to use for this:

Dim myClientName As TextBox = Page.Master.FindControl("MainContent").FindControl("Defectview1").FindControl("Formview1").FindControl("txtClientName")

myClientName.Text = "My new text is here!"


To determine your control's full hierarchy name, enable the Trace directive atop your page and then load it in a web browser.  Example:

<%@ Page Title="" Language="VB" Trace="true" MasterPageFile="~/test/MasterPage.master" AutoEventWireup="false" CodeFile="findcontrol.aspx.vb" Inherits="test_findcontrol" %>


When the page loads in the browser, scroll down the page and find your control.  You can use the browsers Find feature (Usually Control F) and type in the ID of the control.  The ID was set in Visual Studio, "txtClientName" in my example.    This textbox has the full control name, as determined by using "Trace=True". 

ctl00$MainContent$Defectview1$FormView1$txtClientName

In order to programatically access that control, remember the FindControl line I am using is:

Dim myClientName As TextBox = Page.Master.FindControl("MainContent").FindControl("Defectview1").FindControl("Formview1").FindControl("txtClientName")


So each $ is a delimiter. 

Note my FindControl starts with Page.Master while the control hierarcy actually begins with ctl00.  Apaprently ctl00 is used by .NET to identify the Master Page.  In my code I could have used ctl00 as my first step, but Page.Master is a shortcut.

Monday, November 9, 2009

About Me

Just a little something about me...

I am yet another out of work software engineer. (Think Office Space.) I have worked for over a decade in Technical Support, QA and QA Management.

I have been out of work since January 2009 and it is now the second week of November. I would have never imagined it would have taken this long to find a job. I have applied for dozens of jobs over the past year and my resumes all seem to disappear into the black hole.

During this downtime, I have decided to try to make myself more marketable by digging deeper into .NET and attempting to create a fully featured Defect Tracker. Yes, I know, there are lots of those out there. I just think that the best way to learn something is to dig in, make mistakes, fumble, and research. Reading books and tutorials will only get you so far. By building a defect tracker, I am going to try to emulate some of the better features from the defect trackers I have used during my experience as a QA Engineer.

With this blog, I will share my findings that will hopefully be helpful to other people who are attempting to program with ASP.NET. ASP.NET is full of quirks, yet there are some features that make life a lot easier. As with all languages and platforms, there are compromises.

Hopefully, you can learn something from my research and ramblings.