Never fear, help is here!

Last time, I’d just realized I didn’t know enough to get the ball rolling on my new ASP.NET Web Forms project. I’d begun to watch some videos on Pluralsight and try to get associated with the fundamentals, but lost focus quickly and couldn’t retain very much information. I was going nowhere fast, and really needed to do well on this project so I could amaze my boss and company so I could get more opportunities in this field.

Considering going to school would’ve taken quite a bit of time (I’m still considering going anyway), I decided to ask for help. Utilizing all the contacts I had on Twitter, I put out a broad tweet for some help. I’d gotten replies from quite a few, but went with the first guy that I was most acquainted with, which was Matt Groves. He and I utilized Skype to speak to each other, and Join.Me for him to give me direct advice. I also received some help from Jonathan Stevens and John Nastase from CodeandCoffee, looked up some things myself, found answers on StackOverflow, and posted some questions myself. All this help and information went into making the application that I’ll be explaining in this post and some of the ones that follow. Without further ado, here goes…

I wanted to give my users the ability to add items to a list and then select and delete them if any mistakes were made. Through some searching, I’d found the CheckBoxList class, which is made of a list and check boxes beside each item that can select multiple items individually. This has to bind to a data source to display the items of that source, so Matt showed me how to add items to a list of strings and then return the items to the CheckBoxList to display them.

The following code shows the button click event for my add button on my form. On this event, I’m setting the text in a field of my form as a variable (so I don’t have to type FRCID.Text every time), then using Trim() to remove all the leading and trailing whitespace from the text input (just in case someone accidentally hits the spacebar when inputting). I then run some validation that makes sure the data being put in is exactly 8 characters long (won’t go into too much detail with this; let me know if you want more info and I’ll elaborate. I will say 2 things though: RegularExpressionValidator and JavaScript’s value.length).

void btn_submit_Click(object sender, EventArgs e)
        {
            var itemCode = FRCID.Text;
            itemCode.Trim();
            Page.Validate();
                if (Page.IsValid == false)
                {
                    Response.Write("Not enough characters");
                }
                string UpcCode = itemCode.TrimNullSafe();
                

Anyway, moving on. In the next area, I had an if statement that was checking for null or empty values. The ! before String.IsNullorEmpty means not, so this line can be read “if the string in FRCID.Text is not null or empty, then move on”. It then adds the item to the list _items and calls PopulateItemsList. In that, the checkboxlist is cleared (UPCList.Items.Clear()) as well as the textbox (I set FRCID.Text as an empty string, which empties the field, so the user doesn’t have to empty the field themselves to add in another item), a new list is created, and then a foreach runs through all the items in _items and adds them to the checkboxlist.

if (!String.IsNullOrEmpty(itemCode))
                {
                    _items.Add(itemCode);
                    PopulateItemsList();
                }
        }
        void PopulateItemsList()
        {
            UPCList.Items.Clear();
            FRCID.Text = string.Empty;
            if (_items == null)
                _items = new List(_itemNumberRepository.GetAllItems());
            foreach (var itemNumber in _items)
                UPCList.Items.Add(itemNumber);
        }

I clear the UPCList because the foreach will add the entire list in _items every time it runs. If I didn’t clear it, it would look something like this:

Type in 321321 -> click Add -> 321321 shows in checkboxlist
Type in 321322 -> click Add -> 321321, 321321, and 321322 shows in checkboxlist
Type in 321323 -> click Add -> 321321, 321321, 321322, 321321, 321322 and 321323 
shows in checkboxlist...and so on.

There’s also a few other things that has been done to this project, but considering this post is getting a bit long, I’ll hold off until next week. Please let me know if you have any questions or comments either here or on Facebook/Twitter…and once again, thanks for reading!

Advertisements