Code Nuggets: A Side-effect of Using DropDownList AppendDataBoundItems With Databound Items

Picture this: You have a webform with two DropDownLists, both of them are databound to some data you get from a database. Now the first DropDownList’s selection determines the contents of the second dropdown. So you simply do a AutoPostBack = true for the first dropdown and populate the second dropdown in the handler function. Right?

But wait. What if you are required to put in a static item in the dropdown? Something like “– Please Select –“ as the first item in the list to force the user to make a conscious choice. Hmmm, so you look around and find the nice little property named AppendDataBoundItems that will take care of that. All you have to do is declare the first (static) item in the Items collection in the designer (or put a <asp:ListItem> tag inside your <asp:DropDownList> tags) and set AppendDataBoundItems to true. This nice little property tells the DropDownList to add the databound items after the statically declared items, so you can have your happy little “– Please Select –” in your dropdown.

The Side-effect:

The side-effect becomes evident when you play around with your two dropdowns. Its immediately clear that something is not quite right. The AppendDataBoundItems property forces your dropdown’s items from the previous postback to be treated as static objects onthis display. Sort of where you get an ever-growing second dropdown with a hangover from the postback, which is clearly not what you wanted in the first place!

Continue Reading >>

(Tech)LifeDotCom

Though I moved from Blogger quite a while ago, but just for old times’ sake I had kept my account active. For those who are new to this blog, I maintained a blog at Blogger quite some time, which can still be found here. I have now decided to turn into the technical version of LifeDotCom, aptly titled (Tech)LifeDotCom.
(I believe that’s apt enough, don’t know what you guys would think.)

Initially I wanted a separate dynamic page on this WordPress blog, but it only allows you to have static pages, if you need. Means you can have only one blog-like dynamic page and several static pages (which you can see if you look at the top of this page). So putting posts on a static page in the manner of a blog was a major pain in the backside, which was gawking at me. The solution I finally arrived on? Reuse Blogger.

I gather that Blogger has quite mended its ways with the glitches that were apparent in the Beta stage. So (Tech)LifeDotCom would mainly contain some thoughts about the tech landscape, with some snippets of code, and possibly (some) full-fledged tutorials. I only wanted to build a repository of snippets and thoughts and efficient ways of (technically) doing things which would be accessible to me, but I realize that shunning the community on little goodies is not a good thing. I wouldn’t promise to update that blog as regularly as I do this one (does that mean never at all?), but I’ll be posting updates now and then.

Most of the people that (still) read my blog are not technical at all, but hey, I updated the brief description at the top!

Note to self (dynamic controls ASP .NET)

…when populating a dynamic control (such as a dropdownlist dynamically), keep in mind the states of the page. Add that population before the Page_Init event for it to work correctly…

Sending mail with System.Web.Mail (.Net 1.1)

If you thought that it was a serious shortcoming that .Net framework 1.1 wasn’t able to send email through SMTP servers that used authentication, you are in grave ignorance. On the surface of it, yes, you cannot send an email with the standard System.Web.Mail.MailMessage class; but you can of course play tricks on the Exchange server (yeah, that’s what I get out of it that this things works only on an Exchange server) to make it authenticate you.

There is a namespace called http://schemas.microsoft.com/cdo/configuration that does those funny (and highly interesting) tricks. First we would check how to use a simple SMTP server (no authentication), and then we will use authentication to send a message.

//using System.Web;
//using System.Web.Mail;

//The simple SMTP Server
MailMessage message = new MailMessage();
message.From = “src@someone.com”;
message.To = “dest@someother.com”;
message.BodyFormat = MailFormat.Html;
message.Subject = “This is a spooky mail!”;
message.Body = “Heheh, just kidding!”;

SmtpMail.SmtpServer = “your.server.address”
SmtpMail.Send(message);

Now lets check out a server that uses authentication. Microsoft provides the CDOSYS object to achieve just that. You wouldn’t think they were so dumb that they didnt incorporate some lousy authentication mechanism! Add the following lines before the SmtpMail lines.


message.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;

] = “your.server.address”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpserverport”%5D = 25;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendusing”%5D = 2;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”%5D = 1;

//Most of the times the @domain is necessary with the username
message.Fields[“http://schemas.microsoft.com/cdo/configuration/sendusername”%5D = “username@domain.com”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendpassword”%5D = “urpassword”;

//If only the server uses SSL, in this case, the smtpserverport field would be different too
message.Fields[“http://schemas.microsoft.com/cdo/configuration/smtpusessl”%5D = true;

And that would do just fine. There you go, I just saved your time and money!

[P.S. To those whom these lines look like Greek, I must say that this post appears as a record!]