Page.IsPostback is Sneaky

   I had an issue yesterday with a dropdown on a form. I was trying to post the values to a new form.  All the values would post fine except this one dropdown.  I was racking my brain.  What was happening is everytime I submitted the form, the value of the dropdown (varname.SelectedItem.Value) would be item[0] no matter what I choose!?  This was driving me crazy because all the other form variables were passing just fine. I even had another dropdown that was posting fine.  This one was giving me a problem.  What was the difference you may ask?  The only difference was that the dropdown that was not passing correctly was dynamically populated by a databind.  Well, it turns out, which now makes sense, is that when the page posts back, the original value is sent. This happens because the page load function runs first on post back. Hence the default value being posted. What you have to do is this;

 

If Not Page.IsPostBack Then

   varname.DataSource = DataTable

   varname.DataTextField = “value

   varname.DataValueField = “value

   varname.DataBind()

End If

 

Adding these two lines of code around my databind worked wonders. It’s the small things that count. Keep your eyes open and as always, if there are any questions or suggestions, they are welcome. Thank you.

Working With Number in C#

int i = 0;

// convert from a string

int i = int.Parse(“1″);

// convert froma string and don’t throw exceptions

if (int.TryParse(“1″, out i)) {}

i++; // increment by one

i–; // decrement by one

i += 10; // add 10

i -= 10; // subtract 10

i *= 10; // multiply by 10

i /= 10; // divide by 10

i = checked(i*2) // check for over flow

i = unchecked(i*2) // ignore over flow

Working With Numbers in VB.NET

Dim i As Integer = 0

i += 10 ‘ add 10 to i

i -= 5 ‘ subtract 5 from i

i *= 2 ‘ multiply i by 2

i /= 2 ‘ divide i by 2

‘ parse strings as numbers

‘ all number types have Parse and TryParse shared functions

Try

i = Integer.Parse(Console.ReadLine())

Console.WriteLine(“You typed a number”)

Catch ex As FormatException

Console.WriteLine(“Please type a number”)

End Try

If Integer.TryParse(Console.ReadLine(), i) Then

Console.WriteLine(“You typed a number”)

Else

Console.WriteLine(“Please type a number”)

End If

‘ money

Dim price As Decimal = 19.99

price = Decimal.Parse(“$39.95″ NumberStyles.Currency)