DataBinder.Eval() Method
DataBinder.Eval() method saves you from writing complex expressions.
For example, make your datagrid column to template field.
Now, lets understand it various use with different example.
Example1: Concat two datagrid column into one.
In .ASPX File
asp:TemplateColumn HeaderText="Address"
ItemTemplate
asp:Label id="Label1" runat="server" Text='%#
ConcatColumns(DataBinder.Eval(Container.DataItem,"City"),DataBinder.Eval(Container.DataItem,
"Country")) %'lblAddress/asp:Label
/ItemTemplate
/asp:TemplateColumn
In .CS File
public static string ConcatColumns(object objText1, object objText2)
{
return objText1.ToString() + " " + objText2.ToString();
}
Example2: Opening a default Email Program on clicking datagrid column.
In .ASPX File
asp:TemplateColumn HeaderText="ContactName"
ItemTemplate
asp:Label id="Label2" runat="server" Text='%#
GetEmailLink(DataBinder.Eval(Container.DataItem,"ContactName"),DataBinder.Eval(Container.Dat
aItem,"Email")) %'lblCompanyName/asp:Label
/ItemTemplate
/asp:TemplateColumn
In .CS File
public static string GetEmailLink(object DisplayText,object EmailAddress)
{
return "a href='mailto:" + EmailAddress.ToString() + "'" + DisplayText.ToString() +
"/a";
}
Example3: Opening a Pop-up Link on clicking datagrid column.
In .ASPX File
asp:TemplateColumn HeaderText="Contact"
ItemTemplate
asp:Label id="Label2" runat="server" Text='%#
OpenPopupLink(DataBinder.Eval(Container.DataItem,"ContactName"),DataBinder.Eval(Container.Da
taItem,"CustomerId")) %'lblContactName/asp:Label
/ItemTemplate
/asp:TemplateColumn
In .CS File
public static string OpenPopupLink(object displayText,object displayId)
{
return "a href='http:Webform2.aspx?NameField=" + displayId.ToString() + "'" +
displayText.ToString() + "/a";
}
Example4: Displaying no. of characters to be displayed for a particular datagrid column.
Here displaying 5 characters.
In .ASPX File
asp:TemplateColumn HeaderText="Address"
ItemTemplate
asp:Label id="Label1" runat="server" Text='%#
GetFormatedText(ConcatColumns(DataBinder.Eval(Container.DataItem,"City"),DataBinder.Eval(Con
tainer.DataItem,"Country")),5) %'lblCompanyName/asp:Label
/ItemTemplate
/asp:TemplateColumn
In .CS File
public static string GetFormatedText(object objText, object objChars)
{
if (objText.ToString().Length int.Parse(objChars.ToString()))
return objText.ToString().Substring(0, int.Parse(objChars.ToString()));
else
return objText.ToString();
}
Example5: Displaying Calculation, Calculating UnitPrice into Quantity and displaying Total
In .ASPX File
asp:TemplateField HeaderText="Total"
EditItemTemplate
asp:TextBox ID="TextBox1" runat="server"/asp:TextBox
/EditItemTemplate
ItemTemplate
asp:Label ID="Label1" runat="server" Text='%#
CalculateTotal(DataBinder.Eval(Container.DataItem,"UnitPrice"),DataBinder.Eval(Container.Dat
aItem,"Quantity")) %'/asp:Label
/ItemTemplate
/asp:TemplateField
In .CS File
public static string CalculateTotal(object UnitPrice, object Quantity)
{
double Total = double.Parse(UnitPrice.ToString()) * long.Parse(Quantity.ToString());
return Total.ToString();
}
Example6: Applying conditional formatting to datagrid cell, assigning different colors to cell depends on its InputValue.
In .ASPX File
asp:TemplateField HeaderText="Total"
EditItemTemplate
asp:TextBox ID="TextBox1" runat="server"/asp:TextBox
/EditItemTemplate
ItemTemplate
asp:Label ID="Label1" runat="server" Text='%#
CalculateTotal(DataBinder.Eval(Container.DataItem,"UnitPrice"),DataBinder.Eval(Container.Dat
aItem,"Quantity")) %' ForeColor='%#
ConditionalFormating(CalculateTotal(DataBinder.Eval(Container.DataItem,"UnitPrice"),DataBind
er.Eval(Container.DataItem,"Quantity"))) %'/asp:Label
/ItemTemplate
/asp:TemplateField
In .CS File
public static Color ConditionalFormating(object Amount)
{
double dAmount = double.Parse(Amount.ToString());
if (dAmount 1000)
{
return Color.Red;
}
else if (dAmount 500)
{
return Color.Blue;
}
else
{
return Color.Green;
}
}
However, using this method does impose a performance penalty on your code because all the work it does is late-bound.
10 comments:
You can find more on Databinder.Eval Method() : http://weblogs.asp.net/rajbk/archive/2004/07/20/188868.aspx
DataBinder.Eval Method on MSDN, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIDataBinderClassEvalTopic.asp
Thanks, it had help me
Nice Article, Thanks
Nice Article, Thanks
Excellent article...
Just what that I was looking for.
Thank you. :-)
Thanks. Excellent article.
nice link
thanks for the nice article its helped a lot to me
Post a Comment