Wednesday, March 25, 2009

The magic of Dynamic Headers in a GridView

Very basic, but useful if you have a GridView with multiple views. I used this when I needed to Load a GridView with one set of information and then using an OnSelectedChange event show another set of info...all the while changing the HeaderText.

In your ASPX page don't declare a HeaderText field.

In your cs. file use this...(adjust column value to suit your GridView)
this.GridMyRequests.Columns[2].HeaderText = "Requestor";

Wednesday, March 11, 2009

Finally :: Auto Adjusting TextBox Height

It took a while to find what I was looking for, but I've finally found it! A way to Auto Adjust the Height of a Multiline TextBox or TextArea. The solution that I found involves a small bit of javascript, but does some pretty cool automagic auto adjusting. Give it a try HERE.

Here's the code, adjust to your Specs using textarea or ASP:TextBox Controls:

<body onload="cleanForm();">
<script type="text/javascript">
<!--
function countLines(strtocount, cols) {
var hard_lines = 1;
var last = 0;
while ( true ) {
last = strtocount.indexOf("\n", last+1);
hard_lines ++;
if ( last == -1 ) break;
}
var soft_lines = Math.round(strtocount.length / (cols-1));
var hard = eval("hard_lines " + unescape("%3e") + "soft_lines;");
if ( hard ) soft_lines = hard_lines;
return soft_lines;
}
function cleanForm() {
var the_form = document.forms[0];
for ( var x in the_form ) {
if ( ! the_form[x] ) continue;
if( typeof the_form[x].rows != "number" ) continue;
the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
}
setTimeout("cleanForm();", 300);
}
// -->
</script>

<form action="index.php" method="get">
<p><textarea cols="60" rows="2" name="reason">Some text</textarea></p>
</form>

Tuesday, March 3, 2009

Use Compare Validator to check your Integers!

Here's an easy way to check to make sure that the User enters an Integer in an Integer field.
<asp:CompareValidator ID="CompareValidatorErrors" ErrorMessage="Please Enter an Integer" Type="Integer" Operator="DataTypeCheck" ControlToValidate="TxtNumberErrors" runat="server"></asp:CompareValidator>

You can specify the ValidationTypeProperty within the Type property to specify (Currency, DateTime, Double, Integer, String) to Compare against.

Monday, March 2, 2009

Calculate DateDiff and Convert Seconds to Hours/Minutes


You can use this simple DATEDIFF function to calculate the difference, in seconds, between a SubmitDate and CompleteDate.

SELECT DATEDIFF(s, SubmitDate,CompleteDate)
FROM Table
WHERE Column1=ID Value


This will allow you to use the result from above (seconds) to convert the seconds into HH:MM format.

DECLARE @NumberOfSeconds INT
SET @NumberOfSeconds = Result from above
SELECT CAST(@NumberOfSeconds / 3600 AS VARCHAR(3)) + ':' +
RIGHT('0' + CAST(@NumberOfSeconds % 3600 / 60 AS VARCHAR(2)), 2)