Archive for May, 2011

Changing the Bing Maps cursor in JavaScript

I know what you’re thinking. You were at the bar last night, getting your groove on, and a hot lady asks you how to use JavaScript change the cursor when hovering over a Bing Map. I feeling you, dawg. (A little Randy Jackson lingo.)

The next time this happens to you, you can grab a napkin and write out this code:

document.getElementById("mapDiv").childNodes[0].style.cursor = "crosshair";
document.getElementById("mapDiv").childNodes[0].style.cursor = "pointer";

mapDiv, in case you are wondering, is the div tag that stores the map:

<div id='mapDiv' style="position:relative;height:550px;"></div>

This has assumed you have loaded the map into your map control in the first place:

          function GetMap() {
              map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
                           { credentials: "my-creds",
                               center: new Microsoft.Maps.Location(42.73, -84.52),
                               mapTypeId: Microsoft.Maps.MapTypeId.road,
                               zoom: 14

That’s it! Go represent, because now you’ve got mad knowledge.


Leave a comment

Ajax PageRequestManagerParserErrorException with a Response.Write

I’ve been messing around with Ajax for a new site I’m working on, and my page requires an UpdatePanel control.  Inside the UpdatePanel, I have a LinkButton, and since I was just testing to make sure my functionality was functioning (who wants nonfunctioning functionality, right?), I added this code to make sure all was well in the world of Ajax postbacking, and such:

        protected void btnViewRuns_Click(object sender, EventArgs e)

So I click the button and blammo! I get this:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed

The problem is that I had put that Response.Write in my click event. Apparently, that is not a welcome piece of code when dealing with an UpdatePanel. Removing it and putting in a Label control to hold my testing messages was sufficient. In case you are wondering, my final awesome code looked like this:

        <asp:UpdatePanel ID="pnlMain" runat="server">
                        <asp:LinkButton ID="btnView" Text="Vie" runat="server"    onclick="btnView_Click" />
                        <asp:Label ID="lblTest" runat="server" />

Code behind:

        protected void btnView_Click(object sender, EventArgs e)
            lblTest.Text = "Howdy";

The wonder of it all.

Leave a comment

Pesky spaces in GridView cells

Those pesky GridView cells. I was pulling data from a GridView and putting them into another variable to later be displayed in a TextBox. But the TextBox was displaying “&nbsp;” instead of an empty string, which is what was apparently shown in the GridView. Of course, the GridView actually displays “&nbsp;”, it’s just looks like nothing is there when viewed in a browser.

To get around this, I created a teeny tiny function to return an empty string if the “&nbsp;” is found:

        private string ReplaceNbspWithEmptyString(string Value)
            return Value == "&nbsp;" ? string.Empty : Value;

(As I always say, ladies love guys that know how to use a ternary operator.)

And then I call the function whenever I need to reference one of the cells that might be equal to” &nbsp;”:

  string newValue = ReplaceNbspWithEmptyString(row.Cells[1].Text)

A simple fix for a simple problem.

Leave a comment

Merging Excel cells with NPOI

I often get messages like this:

“Scott, you are the best, most awesome guy in the whole world. Every day, I think of only you and your cool coding site, and eagerly await each and every post. How did you get so awesome?”

So of course, I wanted to put this into an Excel sheet. Usually, I use Microsoft Excel Object Library for my Excel needs, but in this case, I’m using NPOI because the client wants to use it to say how great I am. (This happens a lot.)

HSSFWorkbook hssfworkbook = new HSSFWorkbook();
NPOI.SS.UserModel.Sheet WS = hssfworkbook.CreateSheet("Sheet1");
Row row1 = WS.CreateRow(0);
Cell r1c1 = row1.CreateCell(0);
r1c1.SetCellValue("Scott, you are the best, most awesome guy in the whole world. Every day, I think of only you and your cool coding site, and eagerly await each and every post. How did you get so awesome?");
r1c1.CellStyle = red10;
r1c1.CellStyle.WrapText = true;
r1c1.Row.Height = 800;
NPOI.SS.Util.CellRangeAddress cra = new NPOI.SS.Util.CellRangeAddress(0,0,0,5);

OK, a little explanation of what just happened.

The first four rows created the sheet, row, and cell to hold my ego-boosting string. The next four rows set the value and style the cell. An item of interest is row 8, which reads:

r1c1.Row.Height = 800;

It doesn’t seem that interesting, but when a cell is merged using the NPOI code, it doesn’t automatically resize the row to show the string, so that has to be done manually. Otherwise, some of the cell text could be hidden.

The last two lines create a cell range. The first of these holds the CellRangeAddress function, which takes four parameters. In order, they are the row to start the range, the row to end the range, the column to start the range, and the column to end the range. This sets the area of cells that you want to merge into one cell. And then the last line  adds the range to the worksheet.

So there we have it, merging cells with NPOI.

1 Comment

Display all your albums

I wanted to create a list of CDs I had burned on my laptop. Instead of just looking at them and writing them down, I, of course, wrote a program to do it. It literally took my five minutes. OK, maybe six.

I created an ASP.NET page, because the project where my test code is an ASP.NET project, and it’s easy to copy the resulting text out of a web page. First, the controls in the aspx page:

        <asp:TextBox ID="txtPath" runat="server" />
        <asp:Button ID="btnSubmit" Text="Go" runat="server" onclick="btnSubmit_Click" />

 And now the code behind. Don’t’ forget to include the System.IO namespace:

         protected void btnSubmit_Click(object sender, EventArgs e)
            if (txtPath.Text.Length == 0)
                Response.Write("<br>Put something in the textbox, chief.");
            string[] ArtistList = Directory.GetDirectories(txtPath.Text);
            foreach (string Artist in ArtistList)
                string[] AlbumList = Directory.GetDirectories(Artist); 
                foreach (string Album in AlbumList)
                    Response.Write("<br>Artist = " + Artist + ", album = " + Album);

 See, I even put an in-depth data validation in there!

 And the output of this code is like this:

Artist = d:\music\Buddy Guy, album = d:\music\Buddy Guy\Buddy’s Baddest The Best of Buddy Guy
Artist = d:\music\Evanescence, album = d:\music\Evanescence\Fallen
Artist = d:\music\Switchfoot, album = d:\music\Switchfoot\Hello Hurricane
Artist = d:\music\U2, album = d:\music\U2\All That You Can’t Leave Behind

Yep, I could easily make it better, but so could you, so get to it.

Leave a comment

Null is not null? Sure it is – a JavaScript tale

Under the category of “huh?” (which I should definitely add to the Categories list in WordPress), I give you this. I was trying to update a SPAN tag with JavaScript code. Simple enough. Here is the JS code I was using:

   var Message = document.getElementById('txtTest');
   Message.innerText = 'With my last breath, I spit at thee';

(I’m pretty sure that quote is in one of those “Glad we’re getting a divorce” cards from Hallmark.)

 The element I was trying to update was defined as so:

   <span id="txtTest" />

 And when I ran the script, I got this error:

      Error: 'null' is null or not an object 

Well, yes, null is in fact null, so what’s the problem?

 Anyways, I was able to solve the issue by creating a closing tag for my SPAN:

      <span id="txtTest"></span>

Feel free to enlighten me on this, but to me, it seems odd.

1 Comment