banner



How To Dynamically Create Controls In Asp Net With C#

In this article I will explain with an example, how to use IF ELSE condition with EVAL function in ASP.Net controls such as GridView, DetailsView, DataList, Repeater, etc. using C# and VB.Net.

Concept

In database tables, we have some specific codes which have specific meaning. Programmers are aware of the significance of these codes but end users won't understand what a particular code means. In such case we will have to dynamically change the code value to some meaningful and easy to understand words.

For example, consider a scenario of Meeting attendance. The Status column in database consists of two values A and P where A means Absent i.e. the person has not attended meeting and P means Present i.e. the person has attended the meeting.

Now end users won't understand what is A and P and hence by making use of IF ELSE condition with EVAL function, A will be replaced with Absent and P will be replaced with Present which now makes it more meaningful.

Using IF ELSE Condition with EVAL function in ASP.Net GridView

The following HTML markup consists of an ASP.Net GridView consisting of two BoundField columns and one TemplateField column populated using EVAL function.

Using Inline Expression, the value of the EVAL function is compared and if the Status value is A then the string Absent is displayed else Present.

Note : The syntax of using EVAL function within Inline Expression is different for C# and VB.Net.

< asp : GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="false">

< Columns >

< asp : BoundField DataField ="Id" HeaderText ="Id" ItemStyle-Width ="50" />

< asp : BoundField DataField ="Name" HeaderText ="Name" ItemStyle-Width ="150" />

< asp : TemplateField HeaderText ="Status" ItemStyle-Width ="100">

< ItemTemplate >

< asp : Label Text =' <% # Eval("Status").ToString() == "A" ? "Absent" : "Present"%> '

runat ="server" />

</ ItemTemplate >

</ asp : TemplateField >

</ Columns >

</ asp : GridView >

VB.Net

< asp : GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="false">

< Columns >

< asp : BoundField DataField ="Id" HeaderText ="Id" ItemStyle-Width ="50" />

< asp : BoundField DataField ="Name" HeaderText ="Name" ItemStyle-Width ="150" />

< asp : TemplateField HeaderText ="Status" ItemStyle-Width ="100">

< ItemTemplate >

< asp : Label Text =' <% # If(Eval("Status").ToString() = "A", "Absent", "Present")%> '

runat ="server" />

</ ItemTemplate >

</ asp : TemplateField >

</ Columns >

</ asp : GridView >

Namespaces

You will need to import the following namespace.

C#

VB.Net

Binding the ASP.Net GridView control

The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.

C#

protected void Page_Load(object sender, EventArgs e)

{

if (!this.IsPostBack)

    {

DataTable dt = new DataTable();

        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Status") });

        dt.Rows.Add(1, "John Hammond", "A");

        dt.Rows.Add(2, "Mudassar Khan", "P");

        dt.Rows.Add(3, "Suzanne Mathews", "P");

        dt.Rows.Add(4, "Robert Schidner", "A");

        GridView1.DataSource = dt;

        GridView1.DataBind();

    }

}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

If Not Me.IsPostBack Then

Dim dt As New DataTable()

        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Status")})

        dt.Rows.Add(1, "John Hammond", "A")

        dt.Rows.Add(2, "Mudassar Khan", "P")

        dt.Rows.Add(3, "Suzanne Mathews", "P")

        dt.Rows.Add(4, "Robert Schidner", "A")

        GridView1.DataSource = dt

        GridView1.DataBind()

End If

End Sub

Screenshot

Using IF ELSE Condition with EVAL function in ASP.Net GridView using C# and VB.Net

Demo

Downloads

How To Dynamically Create Controls In Asp Net With C#

Source: https://www.aspsnippets.com/Articles/Using-IF-ELSE-Condition-with-EVAL-function-in-ASPNet-GridView-using-C-and-VBNet.aspx

Posted by: gillenwatersquill.blogspot.com

0 Response to "How To Dynamically Create Controls In Asp Net With C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel