Thursday, July 31, 2008

Session is null in ashx

If the HttpContext.Current.Session is null in your .ashx handler, add System.Web.SessionState.IReadOnlySessionState to your class declaration, like this:
public class MyHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState

That's it!

ASP.NET GetElementById for server control

This comes up a lot. You have a server control with an id like "txtName" and you need to reference that with Javascript, but when the page renders, if that textbox is within another server control, the id will change to something like "ct100$something$txtName". There are two ways to deal with this.

Deal with it at the server
If you're creating the JavaScript in the code-behind on the fly, then you can use the txtName.UniqueId, but sometime's there are colons in it that need to be underscores, so you can get the id like this:
txtName.UniqueID.Replace(":","_")
You can then build your JavaScript like this:
ClientScript.RegisterStartupScript(this.getType(),
"blah","alert('"
+ txtName.UniqueID.Replace(":","_")
+ "');",true);
Or, Deal with it at the client
Sometimes you need to have your JavaScript pre-written, like in a .js file. So, to find the control that has the id you want, what you really need to do is find the control who's id ends with the thing you're looking for.

Here's how I do it...
function GetElementByIdEndsWith(tagName,endsWith)
{
    var elements =
             document.getElementsByTagName(tagName);
    
    for(var i = 0; i < elements.length; i++)
    {
        if (elements[i].id.endsWith(endsWith))
        {
            return elements[i];
        }
    }
    
    return null;
}

String.prototype.endsWith = function(txt,ignoreCase)
{
    var rgx;

 if(ignoreCase)
 {
  rgx = new RegExp(txt+"$","i");
 }
 else
 {
  rgx = new RegExp(txt+"$");
 }

 return this.match(rgx)!=null;       
}

You can call it like this:
var x = GetElementByIdEndsWith("input","txtName");

Tuesday, July 22, 2008

SharePoint debugging on a Tuesday

I'm working on some SharePoint customization from my home office... You know how when you're trying to remember something or figure something out and you sort of look up? Yeah...I just did that. The spider in the picture below looked right back at me. Suddenly SharePoint wasn't my problem. I had more immediate debugging to do. Had I not seen this type of spider before I would've been more in the smashing squishing mode than a picture taking mode, but I have, so I took a few shots. You can't tell from this angle, but he's smiling. I've searched for this guy online before. They're pretty harmless, but scary as heck. It's called the "giant house spider"...yeah, duh. I'm sure it took no more than 3 seconds to name it when the first person saw one.
You can't tell the actual size because there's nothing in the picture to compare it to, but I can tell you it was about the size of a 50-cent piece.
The first time I saw one of these was about five years ago. We had some other bug problems in the house so we bought the house-fogger-bug-killer-can-things. I think that's what they're called... ha ha. They killed every bug in the house...except these guys. Basically we killed all of their food, so they all came up from the dank darkness of the basement and underside of the house looking for something to eat. All they found was scared people and ultimately...death by shoe, fly swatter, rock, etc. I'm glad I don't live there anymore, but I guess they're all around since one came to visit today. I'm not sure how to spell this but I'll try... Eeeewwwww!