Custom Search

ASP.NET 4.0 (Active Server Pages): For Advanced users

This page is dedicated to intermediate to advanced users. I created this section to make it easier to navigate to a quick guide such as WCF, MVC, Ajax, etc. This way you are not left helplessly scrolling through an endless amount of information you already know and can get to the important concepts for learning. Although I am still a Junior Developer, I will be providing some examples to get you started using LINQ.

LINQ (Inline code example)

If you want to execute queries using objects then LINQ (Language Integrated Query) is an idea choice. It works by creating a set of collections that you want to perform a query on. Then it can enumerate through those objects to provide query results. This can be effectively used to search through a GridView list which is exactly what the example does below. As always, first I will show you the code and then an explanation will follow.

linq using c#


The first part of this should be self explanatory regarding the XML. If not please visit this link and check out the ASP.NET Beginner's Page. So I am going to start with the part that shows <script runat="server">. This part is basically saying that the code below will process on the server instead of on the client page. This is necessary since a GridView will be reading data from our collections list.

Now look at this section: linq entity framework tutorial

Object list

This is setting up a class called WebSiteData first. Below this are the declarations for the LINQ object list that is created further down. In order to be able to retrieve the property values for our variables it is necessary to use get and set to capture that information. Just know that you must initialize a variable for each object you build in the list. So the objects in this case are called Website, Owner, CopyrightYear, Listing, and WebSiteStats.

Arrays and Method Reference

The new protected void Button1_Click(object sender, EventArgs e) waits for the user to press a button for a search to take place that will be explained later.

In the next area an array named websiteArray is setup to contain the website address names that will be searched later. If you need help on arrays just know that an array can hold a series of characters or numbers in an arranged list. Kinda of like you might keep a shopping list. This allows that list to be retrieved and used throughout your program.

After this a variable (indicated by var) called websites creates a reference to the method GetWebSite(). This is used in the next part to scan the object list. The variable discussed here shows websites = GetWebSite(). After this a variable called search keeps track of the items that are created in the GetWebSite() method below. linq example

Checkbox query using GridView

This next part is using a condition to check for matches that are found with the website names that were setup earlier in the websiteArray array. If you need some help with conditions then I welcome you to visit my conditions page. You can also get this by clicking on the navigation panel on the left.

The first statement shows if (chkOption1.Checked). This means it is checking to see if the first checkbox was selected. You can also learn about checkboxes on the ASP.NET Beginner's Page.

For each loop

The next part is scanning through the object list to see if it matches an entry found in the websiteArray. The part that shows lp.Website is iterating through an enumerated count for the WebSite string that will show the website address, such as "", etc. The search.Add(lp) adds it to the object list once it has been verified by the for each search. If you examine the (chkOption2.Checked) and the (chkOption3.Checked) notice that the only variation is the array reference in websiteArray. Each time it scans from [0] - [3]. This keeps track of all three checkboxes. Other than that everything else is the same.

Call object from Method and bind to GridView

The following section below shows the object reference to the method called ShowGridView(search). Next a variable called search is setup to reference the Method called List GetWebSite(). After this the data that is retrieved from the object earlier in the part that shows (object found). This is passing the object by reference so it can then be bound to the GridView. Finally the return found saves the value to be used in the ShowGridView(search) above.

The Generic List setup

The next part builds the generic object list (also known as a collection list) into the data below. The first part that shows return new List is used to pass a value back to the search variable above. This allows the method to communciate with the Button method above.

Look at the line that shows new WebSiteData {Website="", Owner=" Steve Morrow", CopyrightYear="2012", WebSiteStats=DateTime.Parse("2/18/2012"), Listing=89}. That line is building the generic list that will be used in the query search above to be binded to the GridView afterward. The next few lines show other generic lists that will be evaluated during the search. apps

The rest of the code after </script> will place the labels, checkboxes, button, and the GridView on the page.

I hope you enjoyed this section as I am planning on expanding the LINQ section out later when I have time.

Finally be sure to check out this link to see an example of LINQ in action. Thank you.

Subscribe to my newsletter
Privacy Notice: Programmer Mind will not compromise your privacy