In ASP.NET Web Forms, you often need to display data from the backend (e.g., from a database) in the frontend (HTML). This can be done using data binding syntax, which allows you to embed dynamic data directly within the HTML elements of your page.
The code you provided is a good example of data binding and how ASP.NET Web Forms allows you to use backend data inside your HTML with conditional logic, formatting, and custom methods. Let’s break down the specific example and understand its components:
Code Breakdown
<div class="box-2 text-center">
<%# DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString() != ""
? FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString(), "dd MMM yy") + "-" + FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issueto").ToString(), "dd MMM yy")
: "To be Announced" %>
<p class="show-head-mobile">Bidding Dates</p>
</div>
1. Data Binding with <%# %> Syntax
2. DataBinder.Eval(Container.DataItem, "field_name")
DataBinder.Eval: This method is used to evaluate an expression against the data item (usually the current record or row in a data-bound control). It allows you to access data fields that are part of the data source (such as database records, lists, etc.).
Container.DataItem: Refers to the current data item in the container (for example, a row in a Repeater or GridView).
"ipo_issuefrm" and "ipo_issueto": These are the names of the fields or properties that you are retrieving from the backend data (likely a database).
3. Conditional Logic (? :)
? :: This is the ternary operator in C#. It’s used for conditional logic, which is a shorthand for an if-else statement.
DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString() != ""
? FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString(), "dd MMM yy") + "-" + FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issueto").ToString(), "dd MMM yy")
: "To be Announced"
Condition: If "ipo_issuefrm" has a value, it formats the ipo_issuefrm and ipo_issueto dates using the M_FormatDate() method and concatenates them with a hyphen (-).
Else: If the "ipo_issuefrm" is empty, the fallback value "To be Announced" is displayed.
4. Date Formatting (M_FormatDate)
5. Displaying the Formatted Data
6. <p class="show-head-mobile">Bidding Dates</p>
What This Code Does
If the ipo_issuefrm field has a valid date (not empty), the backend code formats ipo_issuefrm and ipo_issueto as dd MMM yy (e.g., 01 Jan 23 - 15 Jan 23) and displays it inside the <div>.
If ipo_issuefrm is empty, it displays "To be Announced" instead.
The paragraph <p class="show-head-mobile">Bidding Dates</p> is always rendered below this date range or fallback message.
How Data Binding Works in ASP.NET
Data binding allows you to connect frontend controls with backend data sources. The most common data-bound controls in ASP.NET Web Forms are:
GridView: Displays tabular data.
Repeater: Provides more flexible and customizable templates for data.
ListView: Similar to Repeater but with more advanced features.
DetailsView: Displays a single data record in detail form.
FormView: Displays a single record in a customizable template.
In all these controls, you can use data-binding syntax like <%# %> to display values from your data source.
Example: Using This Syntax in a Repeater Control
If you had a Repeater control that displayed data from a list of IPOs, it would look something like this:
<asp:Repeater ID="repeaterIPO" runat="server">
<ItemTemplate>
<div class="box-2 text-center">
<%# DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString() != ""
? FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issuefrm").ToString(), "dd MMM yy") + "-" + FormatFunctions.C_Format.M_FormatDate(DataBinder.Eval(Container.DataItem, "ipo_issueto").ToString(), "dd MMM yy")
: "To be Announced" %>
<p class="show-head-mobile">Bidding Dates</p>
</div>
</ItemTemplate>
</asp:Repeater>
In this case, each record in the Repeater would display the formatted dates or the fallback message based on the data from the backend.
Key Points to Remember
Data Binding: The <%# %> syntax allows you to bind backend data to the frontend dynamically.
Evaluation: The DataBinder.Eval(Container.DataItem, "field") method evaluates and retrieves the value of the specified field from the data item.
Conditional Logic: You can use conditional logic (?: or if statements) to control what gets displayed based on the data.
Custom Functions: You can use custom methods (like M_FormatDate()) to format the data as needed.
Backend Integration: This approach allows seamless integration of backend data (such as database values) into HTML markup, giving you control over how that data is presented.