DataBind code equivalent of Text='<%# Bind("MyColumn") %>' of Item Template in GridView

Discussion in 'ASP .Net Web Controls' started by Desmond Cassidy, Apr 7, 2007.

  1. Hi,
    ASP.NET 2.0

    I am trying to get a handle on writing the GridView's attriburtes in the code behind page.

    I am using a class which implements the ITemplate.InstantiateIn and have successfully

    1. Created most types of controls
    2. Associated them with ItemTemplate, FooterTemplate etc.
    3. Add to the Columns of the Grid.

    The code is reasonably straight forward.
    For Each Column of the Grid
    a) Create a Template
    b) Create a collection of WebControls
    c) Associate them with each other through the ITemplate implemented class

    However, whilst creating some controls there is a need to have the databinding attributes set against the Text properrty of e.g a Label Control.
    So, in the ASP page we have something like...

    <asp:GridView ID="MyGenericGrid" runat="server" ShowFooter="True">

    <Columns>

    <asp:TemplateField HeaderText="ProductID" InsertVisible="False" SortExpression="ProductID">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>

    </ItemTemplate>

    <ItemStyle HorizontalAlign="Center" />

    <FooterTemplate>

    <asp:Button ID="AddProduct" runat="server" CommandName="Insert" Text="Add" />

    </FooterTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    Now I have succesfully done all but the Text: in the Label. Question therefore - what is the way of coding Text='<%# Bind("ProductID") %>' in the code behind page - I have seen some code on various websites with bound fields and Databind.Eval but fail to understand what's going on ??? Suffice to say that the follwoing code does not work (i.e. doesn't BIND !) when I pass the quoted text above to the function.

    Private Function CreateLabel(ByVal ID As String, ByVal Text As String) As Label

    Dim lLabel As New Label

    lLabel.ID = ID

    lLabel.Text = Text

    Return lLabel

    End Function
    Desmond Cassidy, Apr 7, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kel Good
    Replies:
    2
    Views:
    13,131
    Kel Good
    Apr 25, 2004
  2. Kylin
    Replies:
    1
    Views:
    9,438
  3. Miguel Dias Moura
    Replies:
    2
    Views:
    9,299
    Yamini
    Apr 14, 2011
  4. mharness
    Replies:
    2
    Views:
    472
  5. Michael
    Replies:
    0
    Views:
    222
    Michael
    Dec 29, 2003
Loading...

Share This Page