Assigning a Custom Label Value in a ASP.NET GridView Using ObjectDataSource

In my previous post, Assigning a DropDownList Value in a ASP.NET GridView Using ObjectDataSource, I discussed how to put a DropDownList value in a GridView.  As I explained, my motivation was to answer a common question that appears on often on the forums.  If you are wondering what my process is for deciding what to answer, there really is no process besides I see yet another question on the forum about this, and rather than answer it again, I write a post explaining it, then point my answer to the question at the post.  This is what I did yesterday and below is the response to this forum post.

x

So, I have to say one of my main motivations for doing posts is I enjoy helping others through problems.  It gives me a certain satisfaction to know that I have knowledge others may not at any give time and I have a great way to share that knowledge.  It also makes me feel good when others go out of their way to appreciate the effort.  I know when I post questions on forums and get good answers how happy I am, it’s good to know others appreciate my efforts.

So, continuing down that path, Here is the code necessary to answer Zimbran’s last question.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ODS DDL Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetMembers"
            TypeName="BusinessObject"></asp:ObjectDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
               DataKeyNames="Id"
            DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
                   SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:TemplateField HeaderText="Approved" SortExpression="Approved">
                    <ItemTemplate>
                        <asp:Label ID="LabelYesOrNo" runat="server" 
                           Text='<%# (bool) Eval("Approved") ? "Yes" : "No"  %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
About Peter Kellner

Follow me:


Comments

  1. Best you could change the webpage title Assigning a Custom Label Value in a ASP.NET GridView Using ObjectDataSource | PeterKellner.net to something more suited for your content you make. I loved the the writing nevertheless.

  2. Upendra says:

    Hi,
    I need to be able to set the text of a lable or value of texbox to the single string returned by an object datasource.
    Can you help ?

Follow

Get every new post delivered to your Inbox

Join other followers: