Code Monkey

29 Jul

Problems with Multiple ListBoxes and SelectedIndexChanged

I have a layout with master pages and at the top some ListBoxes that all have autopostback = true and corresponding SelectedIndexChanged events on them.

For some reason when one listbox changed all the events fired in a set order, I haven’t figured out how to stop that from happening, basically they fired from left to right, so when the one on the right changed, listbox 1 fired first, causing unwanted results.  I have no idea how to stop that but figured out a way to handle it.

In each SelectedIndexChanged event add this wrapper code:

//ListBox name is ViewList

Boolean isSource = false;
string caller= Page.Request.Params.Get(”__EVENTTARGET”);
Control control = null;

if (caller!= null && caller!= string.Empty)
{
control = Page.FindControl(caller);
if (control.ID == ViewList.ID)
{
isSource = true;
}
}

if (isSource)
{
//perform normal actions here.
}

30 May

How to speed up FireFox

Found this on cnet and it makes firefox run even faster then before.  I highly recommend these with firefox 3.0 and you’ll be zooming on the internet

Open firefox:

In a tab type about:config

right click in the pain and add new integer, type in ndlayout.initialpaint.delay and give it a value of 0

then in the filter type: network.http

choose network.http.pipeliningdouble click to make it true
choose network.http.proxy.
pipeliningdouble click to make it true
choose
network.http.pipelining.maxrequests and set it to 30

restart firefox and it should be faster for you.

21 Feb

Modifying a Select Box In Javascript

When messing with pages using AJAX, its often necessary to change a select box’s value.  Here is how to clear it’s value.

var listBox = document.getElementById(”listBox”);
for (var i = listBox.options.length - 1; i >= 0; i–)
{
listBox[i].parentNode.removeChild(listBox[i]);
}

Then to add items:

var item = document.createElement(”option”);
item.value = val;
item.text = txt;
listBox.appendChild(item);

19 Feb

Detecting Ad Block Plus

Ad blocking plus is great for browsing sites, but what if you want to actually be supported by ads and ad blocker is stealing all your page views.

Well the solutions is very easy.

on your site have javascript file lets call it script.js

in in that page just have one line:

AdsShowing = true;

then in you page that displays the ads have this code:

<script type=“text/javascript” language=“javascript”>
var AdsShowing = false;
</script>
<script  type=“text/javascript” src=“http://www.yoursite.com/script.js?link=http://pagead2.googlesyndication.com/pagead/show_ads.js”></script>
<script type=“text/javascript”>
 if (!AdsShowing)
{
alert(“BLOCKED”);
}
</script>

Ad blocker will think that your javascript file is an ad link and that code won’t be executed.  Hence AdsShowing will  be false and cause the alert to trigger.

So you can either block all code from executing or the page, give them a mean spirited alert, or a place a box saying hey jerko look at my ads.  You can handle it anyway you want.

18 Feb

Date Formats

The nice thing about C# is its date format in the ToString() method.

so you have a date object with the value of February 9th, 2008 4:35:04 PM

dateObject.ToString(MM/dd/yyyy hh:mm:ss tt“);

outputs 02/09/2008 04:35:04 PM;

24 hour clock use HH for hour:

dateObject.ToString(MM/dd/yyyy HH:mm:ss“);

outputs 02/09/2008 16:35:04;

06 Feb

TSQL - Sorting with a Case Statement and Paging

Typically when I use a stored procedure to return tabular data, I pass in a sort flag.  I have found the easiest way to do that sort is with a case statement.  I typically have the columns have a numeric value and pass that on click, where is what I get combined with paging the results.

–these would be passed into the stored procedure
 Declare @sortby int
set @sortby = 4
Declare @pagenum int
set @pagenum = 1
Declare @pagesize int
set @pagesize = 50

 Declare @pagestart int;
Declare @pagestop int;
set @pagestart = (@pagenum -1) * @pagesize;
set @pagestop = @pagestart + @pagesize;

;With Final as (Select *, ROW_NUMBER() over (Order By
     Case When @sortby = 1 THEN taskid End ASC,
     Case When @sortby = 2 THEN taskid End Desc,
     Case When @sortby = 3 THEN tasktitle End ASC,
     Case When @sortby = 4 THEN tasktitle End Desc
) as Row From Tasks)
Select * From Final where row > @pagestart and row <= @pagestop;

04 Feb

Forcing Vertical Scroll Bars

In developing centered pages I’ve noticed that if you go from one page to another and one page is longer then the screen and the other is shorter, the page jumps around and doesn’t remain centered.  Why?  Because most browsers (IE being the exception) hide scroll bars if they aren’t needed.

 So how can you fix this?  Easy,

in your style sheet just add this:

 html{height: 100%; margin-bottom: 1px;}

 so the user will always see the scroll bar and it only scrolls down a tad.  Much better then adding hundreds of br’s with spaces now isn’t it.

01 Feb

C# Switch Statement

The syntax for a switch statement (Select Case in VB) is as follows

Inside each case you can use any jump statement you wish: including gotos and continues:

 http://msdn2.microsoft.com/en-us/library/d96yfwee(VS.71).aspx

switch (value)

{
     case “a”:
          //dosomething
          break; //kicks out of the switch statement
     case “b”:
          //dosomething else
          break;
      case “c”:
          //dosomething again
          break;
      default;
          //do stuff
          break;
}

23 Jan

Populate a list box from an Enum

Have you ever needed to populate a listbox from an Enum and have the numeric value of the enum be the value for the listbox?  Well here’s how.

public enum Status
{
     Progress = 1,
     Modify = 2,
     Review = 3,
     Completed = 4,
     Terminated = 5
}

private void loadStatus()
{
     Array arr = System.Enum.GetValues(typeof(Status));
     for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0);i++)
     {
          string txt = Enum.GetName(typeof(Status), arr.GetValue(i));
          int nval = (int)arr.GetValue(i);
          string val = nval.ToString();
          ListItem li = new ListItem(txt,val);
          StatusListBox.Items.Add(li);
     }
}

19 Jan

Welcome

Welecome to the new Code Monkey blog.  I plan on sticking code samples up here to help others and to help myself to code.  This will probably be javascript code and c#, since thats what I’ll be working on.

© 2009 Code Monkey | Entries (RSS) and Comments (RSS)

Global Positioning System Gazettewordpress logo