This keyword was introduced in .NET 4.0. It provides the access to DLR
(Dynamic Language Runtime) using this specific keyword. It intimates the
compiler to perform dynamic calling which is resolved at the run time. It uses
namespace. It simplifies tasks with interoperability providing access to COM
API's and Dynamic API's.
Now, we will not spend more time in studying theory, It time to go and do a
simple practical demo.
So, Firstly I have created simple database which looks like this:
Let's create new project with unique name:
The Complete Code of Default.aspx file looks like this:
<%@
Page Language="C#"
AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="Dynamic_Keyword.WebForm1"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
id="Head1"
runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<center>
<asp:GridView
ID="GridView1"
runat="server"
BorderColor="#CC9966"
BorderStyle="None"
BorderWidth="1px"
CellPadding="4">
<FooterStyle
BackColor="#FFFFCC"
ForeColor="#330099"
HorizontalAlign="Center"
/>
<RowStyle
BackColor="White"
/>
<HeaderStyle
BackColor="#990000"
Font-Bold="True"
ForeColor="#FFFFCC"
/>
<PagerStyle
BackColor="#FFFFCC"
ForeColor="#330099"
/>
<SelectedRowStyle
BackColor="#FFCC66"
Font-Bold="True"
ForeColor="#663399
/>
<SortedAscendingCellStyle
BackColor="#FEFCEB"
/>
</asp:GridView>
</center>
<SortedAscendingHeaderStyle
BackColor="#AF0101"
/>
<SortedDescendingCellStyle
BackColor="#F6F0C0"
/>
<SortedDescendingHeaderStyle
<br
/> <br
/>
<center><asp:Button
ID="Button1"
runat="server"
Text="Click Here"
onclick="Button1_Click"
/>
</center>
</div>
</form>
</body>
</html>
We are now using dynamic keyword by adding some properties and method which
enables to retrieve database values:
dynamic p
= new ExpandoObject();
p.Title = "Simple
GridView Using Dynamic";
p.Data = new Action(()
=>
{
The Complete Code of Code Behind files for this looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;using System.Web.UI.WebControls;
using System.Dynami;
using System.Data.SqlClient;
using System.Data;
namespace Dynamic_Keyword
{
partial class
WebForm1 : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
}
protected void
Button1_Click(object sender,
EventArgs e)
{
dynamic p =
new ExpandoObject();
p.Title = "Simple GridView Using
Dynamic";
p.Data = new
Action(() => {
DataSet ds = new
DataSet();
SqlConnectio
con = new
SqlConnection(@"Data
Source=VIJAY-PC\SQLEXPRESS;InitialCatalog=Candidate;Integrated Security=True");
SqlDataAdpter da =
new SqlDataAdapter("Select
* from Student", con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind(); });
Response.Write("<center><b>" +
p.Title + "</center></b>"); p.Data();
}
}
}
The output of the Program looks like this:
After Button Click:
I hope this article is useful for you.. I look forward for comments and feedback..Thanks Vijay Prativadi.