Wednesday, December 12, 2007

Accessing controls in a MasterPage

If you have a regular .aspx page that utilizes a MasterPage and you need to access certain controls, like a label, within that MasterPage, it's as simple as adding this sort of code to your .aspx code:

Dim someLabel As Label = Master.FindControl("lbl_someLabel")
someLabel.Text = "Clients"

Wednesday, November 21, 2007

Image references in your CSS not showing?

If you have a background image in your CSS called like this,

background-image: url(/images/background.gif);

...and when you run your Web application the background isn't showing up it could be a problem with your 'Virtual Path' setting. To fix this, click on the main project path in your Solution Explorer, at the very top, change the 'Virtual Path' property to read "/"

Thursday, November 15, 2007

Editing Meta Tags From Code Behind

Dynamically change the content of meta tags from the code behind. This allows you to feed the meta tags with content from a database or other dynamic content.

In your aspx source code do:

<meta id="metaDescription" name="description" content="" runat="server" />

In your aspx code behind do:

metaDescription.Content = "Description goes here"

The key is the runat="server" and the unique id within the meta tag. This allows you to "see" it in your code behind and manipulate it before sending it to the browser.

Thursday, November 8, 2007

Adding a value to a dropdownlist after *.DataBind()

If you have a DropDownList component that is getting fed via a query or an ObjectDataSource, you can insert items into the list in such a way:

DropDownList.DataBind()
DropDownList.Items.Insert(<listLocation>, New ListItem("<name>", <value>))

Where <listLocation> is an integer that tells where to put the item within the DropDownList, <name> being the name that's displayed within the DropDownList, and <value> being the value associated with that item. Example:

DropDownList.DataBind()
DropDownList.Items.Insert(0, New ListItem("Select All", 0))

ObjectDataSource SelectParameters

If you have a component being bound from an ObjectDataSource (like a drop-down menu, or a grid view) and you wish to change the value's parameters SelectParameters:

ObjectDataSource.SelectParameters(#).DefaultValue = value
GridView.DataBind()

Where # is which SelectParameter you'd like to change. If there's just one SelectParamter, default is 0. Then reference your object that uses that datasource, in this case a GridView, to rebind.

Sunday, October 28, 2007

Utilizing AppSettings

In web.config create a key and value:

<add key="keyName" value="keyValue" />


Access the AppSetting by calling it in your code behind:

ConfigurationManager.AppSettings.Get("keyName")

Tuesday, October 16, 2007

Hex to Dec (VbScript)

Convert hexidecimal color codes (ie #C0C0C0) to decimal RGB (ie 255,255,255) in vbscript

Public Function GiveDec(Hex)
  if Hex = "A" then
    Value = 10
  elseif Hex = "B" then
    Value = 11
  elseif Hex = "C" then
    Value = 12
  elseif Hex = "D" then
    Value = 13
  elseif Hex = "E" then
    Value = 14
  elseif Hex = "F" then
    Value = 15
  else
    Value = Hex
  end if
  GiveDec = Value
End Function

Public Function HexToDec(Hex)
  Dim a,b,c,d,e,f,x,y,z,fHex
  fHex = Replace(Hex,"#","")

  a = Left(fHex,1)
  a = GiveDec(a)
  b = Mid(fHex,2,1)
  b = GiveDec(b)
  c = Mid(fHex,3,1)
  c = GiveDec(c)
  d = Mid(fHex,4,1)
  d = GiveDec(d)
  e = Mid(fHex,5,1)
  e = GiveDec(e)
  f = Right(fHex,1)
  f = GiveDec(f)

  x = (a * 16) + b
  y = (c * 16) + d
  z = (e * 16) + f

  HexToDec = """" & x & "," & y & "," & z & """"
End Function

Monday, October 15, 2007

Limit text input to numbers (Javascript)

Limit the text field to only numbers (with decimals)

function format(input){
 var num = input.value.replace(/\,/g,'');
  if(!isNaN(num)){
   if(num.indexOf('.') > -1){
   num = num.split('.');
   num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
   if(num[1].length > 2){
    alert('You may only enter two decimals!');
    num[1] = num[1].substring(0,num[1].length-1);
   } input.value = num[0]+'.'+num[1];
  } else {
   input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') };
  } else {
   alert('You may enter only numbers in this field!');
   input.value = input.value.substring(0,input.value.length-1);
  }
}


Limit the text field to only numbers (no decimals)

function formatInt(input){
 var num = input.value.replace(/\,/g,'');
  if(!isNaN(num)){
   if(num.indexOf('.') > -1) {
    alert("You may not enter any decimals.");
    input.value = input.value.substring(0,input.value.length-1);
   }
  } else {
   alert('You may enter only numbers in this field!');
   input.value = input.value.substring(0,input.value.length-1);
  }
}


Call the function within the html input

<input id="text" name="text" size="5" onkeyup="format(this);">