public void item_bound(object sender,DataGridItemEventArgs e)
{
try
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))`enter code here`
{
string Status =(string)DataBinder.**Eval**(e.Item.DataItem,"customer_status");
Image Image = new Image();
Image = (Image)e.Item.Cells[1].FindControl("imgbtnstatus");
//Image.Command +=new CommandEventHandler(Image_Command);
//Image.Attributes.Add("SEmpId ", SEmpId );
if (Status == "Active")
Image.ImageUrl = "~/images/activeuser.png";
else
Image.ImageUrl = "~/images/inactiveuser.png";
}
}
catch(Exce开发者_运维知识库ption ex){Response.Write(ex.ToString());
}
}
here is the code for runtime binding the active and inactive image for the users in datagrid...
i want to know what is the use of Eval in above code...
Eval takes and evaluates the "customer_status" value from the current item in the data source.
I agree with McKay's answer; I just thought I'd expand a bit on this:
The DataBinder object also as a GetIndexedPropertyValue function that allows you to retrieve things like elements of an array or a dictionary.
For instance, you could do DataBinder.GetIndexedPropertyValue(e.Item.DataItem, "[0]") to get the first element of an array, or you could do DataBinder.GetIndexedPropertyValue(e.Item.DataItem, "[name]") to get a value from a dictionary using the "name" key.
Though there might be a reason for doing this in the code-behind, much of the code from the question could be done in the ASP.Net markup instead:
<asp:Image ID="imgbtnstatus" runat="server" ImageUrl='<%# ((string)DataBinder.Eval(Container.DataItem,"customer_status") == "Active") ? "~/images/activeuser.png" : "~/images/inactiveuser.png" %>' />
Also, since .Net 2.0 an Eval function has been available that replaces DataBinder.Eval(Container.DataItem, ...). The ASP markup in my example could be written as follows:
<asp:Image ID="imgbtnstatus" runat="server" ImageUrl='<%# ((string)Eval("customer_status") == "Active") ? "~/images/activeuser.png" : "~/images/inactiveuser.png" %>' />
The Eval function can also be used to retrieve values similar to the DataBinder.GetIndexedPropertyValue function by passing a string enclosed in square brackets.
精彩评论