There are time in where you may like to display a certain value inline, depending on the Eval() value that is data bound. At times like this, the ternary operator helps a great deal.
For example, I have a datetime object (“DatePaid”) that is being retrieved from the database using Linq to Sql. And every time the value is null it display the date on the user interface as such “1/01/0001”. I would really like a blank column displayed instead. I achieved this by using the ternary operator inline as below:
The above translates as such, if “DatePaid” is null then insert blank space “ ”, else show “DatePaid”
What if you had multiple conditions? Such as If-Else If-Else If-Else. In circumstances like this, it’s probably best handle the values server side, using a data bound event for example.
Hope this helps.
Prior to getting into the workings of it, do note, that I’m using data from Northwind Customers table. Essentially, all I am doing is retrieving the ContactName, CompanyName and Country from all Customers records and returning them as an IEnumerable<T> object in my Data Access Layer.
To begin, I have a GridView control defined with 3 literal controls. Do note, the SortExpression defined on each TemplateField. Also note the OnSorting and OnPageIndexChanging events.
Then on the code-behind, I’ve set up a property called SortOrder. This property keeps track of the ViewState[“SortOrder”] value that is saved on page postbacks.
The Page_Load method, sets the SortOrder property to an empty string, then calls a custom method to bind the data to the GridView control.
The custom bind method that is called from the Page_Load method takes in 2 string parameters. The parameters define the sort expression and the sort direction.
The method first checks if the sort expression is not an empty string. If it isn’t empty it then proceeds to use the DataView Sort property, prior to binding the data to the GridView control.
The custom method that returns the DataView object as shown in snippet above, is define in snippet below.
Now we come to the core part of this post. How do we perform sorting? Well, if you have followed the snippets along you would have noticed the custom bind method takes in 2 parameters. It then checks the sort expression parameter for a value, then uses the DataView Sort property to apply the sorting. Not too complex, but the easier bit is on the GridView OnSorting event as show below. Basically, calling the custom bind method by passing in both, the sort expression and the sort order.
Now to the next part of the post which is, How do we perform paging?. Well, on the GridView OnPageIndexChanging event, a new page index is set based on the GridViewPageEventArgs passed in, then the custom bind method is called again.
Hope the post helps show how easy it is to get Paging and Sorting operations working, through the use of a DataView object.
The IsPostback is probably one of the most commonly used property in ASP.NET development.
But what is it exactly?
It is a property that belongs to the Page class under the System.Web.UI namespace. Essentially it returns a boolean value indicating if a page is being requested for the first time or if is being loaded in respond to a postback.
Well, when we talk on the web, we use the HTTP protocol. The communication or exchange of information between a client (web browser) and an application or site (web browser) is initiated when we type in a site URL or click on a hyperlink. This is known as a Request (GET), the site or application then Responds to the client. This process is known as a round-trip.
However, we the client, also interact with a site or application through the execution of a particular web control (such as a button click). This is when data is sent to the site or application, a POST is made, and this process is known as a postback.
Important! – We can send data back using a GET as well, but this is not discussed here.
So, the IsPostback property on a ASP.NET page essentially checks if the request is either a GET or POST.
The code snippet below basically shows an array is bound to a drop-down list when a page loaded.
One of the simplest examples around and I keep forgetting it. It’s as below:
protected void gvControl_PageIndexChaging(object sender, GridViewPageEventArgs e)
this.gvControl.PageIndex = e.NewPageIndex;
this.BindData(); // private method that binds the data source
A big thank you to the author of this post.