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.

The WEB.CONFIG File

I was working on improving an application the other day and I noticed something in the web.config file.  I do not know if it is by design just in VS 2008 or if it was included in VS 2005 but you can include all your Imports in the web.config file. I have not looked into how much this will cost the application to include everything all the time even though it is not being used but it makes for cleaner code. Here is what needs to be done:

 

<configuration>

            <system.web>

                        <pages>

                        <namespaces>

                        <clear/>

                        <add namespace=”System”/>

                        <add namespace=”System.Collection”/>

                        <!—user added namespaces below à

                        <add namespace=”System.Data”/>

                        <add namsspace=”System.Data.SqlClient”/>

                        </namespaces>

            </pages>

            ………

            </system.web>

</configuration>

 

As you can see by the above example, I separate the auto generated namespaces from the ones that I add. That is not required but I do it for my own benefit. Also there may be a place where one can indicate which namespaces to include by default. I have not found that yet. If I do I will let you know. As always, if there are any questions or suggestions, they are welcome. Thank you.

Working With Dates and Times Using ASP.NET C#

I know we have all have to use dates at some time or another.  I came across this posting and thought I would list it here for you.  Hope it helps.

DateTime today = DateTime.Today; // just the date

DateTime tomorrow = today.AddDays(1);

DateTime yesterday = today.AddDays(-1);

TimeSpan time = tomorrow – today;

int days = time.Days;

int hours = time.Hours;

int minutes = time.Minutes;

int seconds = time.Seconds;

int milliseconds = time.Milliseconds;

time += new TimeSpan(days, hours, minutes, seconds,

milliseconds);

As always, if there are any questions or suggestions, they are welcome. Thank you.

Working With Dates and Times Using ASP.NET VB

I know we have all have to use dates at some time or another.  I came across this posting and thought I would list it here for you.  Hope it helps.

Dim now As Date = Date.Now ‘ date and time

Dim today As Date = Date.Today ‘ just the date

Dim tomorrow As Date = today.AddDays(1)

Dim yesterday As Date = today.AddDays(-1)

‘ use parse functions to convert from strings

Dim vb1 As Date = Date.Parse(“1991-05-01″)

‘ use culture info to parse dates in regional formats

Dim gb As New CultureInfo(“en-GB”)

vb1 = Date.Parse(“01-05-1991″, gb.DateTimeFormat)

‘ format dates as strings

vb1.ToLongDateString() ‘ Wednesday, May 01, 1991

vb1.ToShortDateString() ‘ 5/1/1991

vb1.ToLongTimeString() ‘ 12:00:00 AM

vb1.ToShortTimeString() ‘ 12:00 AM

vb1.ToString(“u”) ‘ 1991-05-01 00:00:00Z

vb1.ToString(“yyyy-MM-dd”) ‘ 1991-05-01

‘ TimeSpan represents differnce between two dates

Dim time As TimeSpan = tomorrow – today

Dim days As Integer = time.Days

Dim hours As Integer = time.Hours

Dim minutes As Integer = time.Minutes

Dim seconds As Integer = time.Seconds

Dim milliseconds As Integer = time.Milliseconds

‘ Add time using other timespan instances

time.Add(New TimeSpan(days, hours, minutes, seconds,

milliseconds))

‘ or

time += New TimeSpan(days, hours, minutes, seconds,

milliseconds)

‘ use a timespan to add time to a date

today.Add(time)

‘ or

today += time

‘ timespan represented as Days.Hours:Minutes:Seconds:

‘ Subseconds

time = TimeSpan.Parse(“1.2:3:4:5″)

‘ or

If TimeSpan.TryParse(Console.ReadLine(), time) Then

Console.WriteLine(time)

End If

 

As always, if there are any questions or suggestions, they are welcome. Thank you.