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

  • Thread starter Desmond Cassidy
  • Start date
D

Desmond Cassidy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top