Tuesday, January 12, 2010

Q. ASP.NET code behind function into grid view template field?

Q. How to call asp.net code behind function into gridview template field?
Answer: we can call code behind function in the gridview as follows
In asp.net markup:

<asp:TemplateField HeaderText="IsEnable">
<ItemTemplate>
<center>
<asp:Image ID="imgUserState" runat="server" Width="10px" Height="10px" ImageUrl='<%#GetImage(Eval("IsEnable")) %>'></asp:Image>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IsApprover">
<ItemTemplate>
<center>
<asp:Image ID="imgApproverState" runat="server" Width="10px" Height="10px" ImageUrl='<%#GetImage(Eval("IsApprover")) %>'></asp:Image>
</center>
</ItemTemplate>
</asp:TemplateField>



In c# code behind file
    protected string GetImage(object ob)
    {
        bool IsActive = Convert.ToBoolean(ob);
        if (IsActive) return @"~/images/yes.png";
        else return @"~/images/no.png";
    }

Output is:





No comments: