Template Column DataBinding

S

Sean Patterson

Greets all,

I've been doing some .NET web apps for a little while now and I _LOVE_
template columns within datagrids since I can make things really
flexible for which content I use and which data I want to display.

Typically when I setup a basic datagrid, I'll use the following
structure for my columns on the aspx side:

<asp:TemplateColumn HeaderText="Title">
<HeaderStyle Width="125px"></HeaderStyle>
<ItemTemplate>
<%# Container.DataItem("Title") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditTitle" runat="server" MaxLength="15"
Columns="16" Text='<%# Container.DataItem("Title") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>

That way on my codebehind page. All I have to do is set the data source
and call the databind method. By doing it this way, I can also use a
template column and create all the custom ItemCommands I want, all I
have to do is add a CommandName and CommandArgument to my datagrid and
have the events processed in a single method.

Similarly, I can do some "fancy" things with RadioButtons and image
displays by late binding methods on the aspx side as well. For instance,
I can display an e-mail icon through the following code:

<asp:ImageButton id="btnSendMessage" runat="server"
Visible='<%# GetEmailVisibility(Container.DataItem("MemberID")) %>'
ImageUrl="images/MessageButton.gif" CommandName="SendMessage"
CommandArgument='<%# Container.DataItem("MemberID") %>'>
</asp:ImageButton>

My code behind page compares the Member's ID to the data table to see if
they have an e-mail address on record. If so, then the visibility is set
to "True" through the string it returns. Otherwise it is set to "False"
to hide it.

I give you all these to ask this question. Is this the best practice for
databinding with TemplateColumns, or for databinding in general? I know
the typical method is to separate as much user interface from server
side code as possible through the .vb file, but sometimes processing the
e.Item.FindControl...convert to this web control, get this parameter
seems to be more hassle than its worth.

What do you guys think? I really want to get this stuff right and to
have it line up the way the "best practice" intends. Thanks for bearing
with this lengthy post in the first place and for any feedback you can
provide!
 
S

Scott M.

I'll tell you that, generally, late binding is a bad idea. It opens the
door to run-time exceptions and causes a performance hit.
 

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

Top