Friday, October 9, 2009

ResolveUrl

If you ever need to resolve the location of a resource in your code behind, use this sweet little function. This is especially useful in a Master Page or a User Control where the relative location of a resource will change.

Here is an example that dynamically adds some JavaScript onmouseout code for an image rollover:

imgImage.Attributes.Add("onmouseout", "src='" + ResolveUrl("~/Images/something_active.gif") + "'");

Friday, September 11, 2009

How to Add an Event Handler to Control Created in Code Behind

I hope that title makes sense to you. Basically I'm building a big form in the code behind. Sucks. But in my case I had to do it and also had to set AutoPostback and add an EventHandler to some checkboxes.

cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += new System.EventHandler(this.CB_CheckedChanged);


"CB_CheckedChanged" is a method I created to handle the cb checks. You can basically use the same code for RadioButtonLists etc.

Thursday, September 10, 2009

Format Date in GridView

I always forget how to do this one. If you need to format a date a certain way in a
GridView, try this out:

<asp:BoundField DataField="the_date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" />

Friday, September 4, 2009

Syntax error: Missing operand after 's' operator.

Don't bother trying to filter a DataView by setting the RowFilter property to a string with a single quote. Make sure you escape the quotes first:

dv.RowFilter = "Something = 'O'Malley'";

becomes...

dv.RowFilter = "Something = 'O''Malley'";

Thursday, August 27, 2009

Oracle Client on Windows 7

Oracle sucks, but we're forced to use it at work. Of course Oracle can't get off their collective asses and put out a compatible client even after Windows 7 has, for all intents and purposes, been released. Here's a potential fix though:

http://msutic.blogspot.com/2009/08/how-to-instal-oracle-client-11g-on.html

Monday, August 24, 2009

Disable a Button (Client Side)

This code is great for when you want to make sure a person doesn't click a button more than once - but do it in a prettier way (you of course should verify everything in your code-behind as well, but this makes it pretty damn clear to your users):

StringBuilder sbValid = new StringBuilder();
sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
sbValid.Append("this.value = 'Please Wait...';");
sbValid.Append("this.disabled = true;");
sbValid.Append("document.forms[0].ctl00_ContentPlaceHolder1_btnPaymentSubmit.disabled = true;");
sbValid.Append(this.Page.GetPostBackEventReference(this.btnPaymentSubmit));
sbValid.Append(";");
btnPaymentSubmit.Attributes.Add("onclick", sbValid.ToString());

Thursday, August 20, 2009

Reset Scroll Position When Using MaintainScrollPositionOnPostback

If you're using MaintainScrollPositionOnPostback="true" to, well, maintain your scroll position on postback, sometimes you still want to bump the scroll up to the top (if you're using multiple panels or whatever. Here is a nice and easy fix.

1. Put this method in your code-behind:

    private void ResetScrollPosition()
{
if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CreateResetScrollPosition"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateResetScrollPosition", "function ResetScrollPosition() {" + Environment.NewLine +
" var scrollX = document.getElementById('__SCROLLPOSITIONX');" +
Environment.NewLine + " var scrollY = document.getElementById('__SCROLLPOSITIONY');" +
Environment.NewLine + " if (scrollX && scrollY) {" +
Environment.NewLine + " scrollX.value = 0;" +
Environment.NewLine + " scrollY.value = 0;" +
Environment.NewLine + " }" +
Environment.NewLine + "}", true);

ClientScript.RegisterStartupScript(this.GetType(), "CallResetScrollPosition", "ResetScrollPosition();", true);
}
}


2. Call ResetScrollPosition(); in any method where you want to reset the scroll position.