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. Advertisements

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,896
    Kel Good
    Apr 25, 2004
  2. Kylin
    Replies:
    1
    Views:
    9,692
  3. Miguel Dias Moura
    Replies:
    2
    Views:
    9,526
    Yamini
    Apr 14, 2011
  4. =?Utf-8?B?SmVyb2QgSGF0bGV5?=

    GridView add multiple controls to edit item template

    =?Utf-8?B?SmVyb2QgSGF0bGV5?=, Oct 7, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,260
    =?Utf-8?B?SmVyb2QgSGF0bGV5?=
    Oct 8, 2006
  5. David

    Bind Text File To GridView

    David, Oct 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    717
    Eliyahu Goldin
    Oct 31, 2006
  6. mharness
    Replies:
    2
    Views:
    656
  7. shapper

    GridView Item Template

    shapper, Jul 3, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    511
    shapper
    Jul 3, 2007
  8. Michael
    Replies:
    0
    Views:
    445
    Michael
    Dec 29, 2003
Loading...