Monday, November 17, 2008 Eval() and Bind() expressions in data bound controls like GridView and DetailsView

The Eval() and Bind() expressions are heavily used in GridView and DetailsView data bound controls. The difference between these two is that Eval is used for read only purpose and Bind is used for read/edit purpose. For example Eval can be used to bind the Text property of an Label control whereas Bind can be used to bind the TextBox control so that it can be edited to meet some requirement. Of course, GridView and DetailsView controls make large use of these Eval and Bind expressions. And I have put some cases here.

I have already talked about the DataFormatString in GridView and DetailsView controls when using Eval expression. You can take these two examples and customize these expressions as your requirement demands.

<asp:DetailsView Width="100%" ID="dvwCategory" runat="server" AutoGenerateRows="False" HeaderText="Product Category Details">    

        <FieldHeaderStyle Width="10%" />


            <asp:BoundField DataField="Category_ID" HeaderText="ID" InsertVisible="false" ReadOnly="true"/>

            <asp:TemplateField HeaderText="Category Name">  


                    <asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("Category_Name") %>'></asp:Label>



                    <asp:TextBox ID="txtCategoryName" runat="server" Text='<%# Bind("Category_Name") %>'></asp:TextBox>



            <asp:CheckBoxField DataField="Active" HeaderText="Active"/>



Then why use Eval and Bind since the BoundField can has DataField property (like DataField="Category_ID")? Yeah, it's mostly useful when we have no other choices but templatefileds in gridview and detailsview controls. We can format the date and currency in various formats, and so on. But most important is it provides us much flexibility in playing between the nature of GridView and DetailsView controls and an programmer's requirement. You can share yourself by commenting this post. You are heartily requested to share this post by bookmarking this post (You can just add by clicking in the social bookmak buttons just below this post! It's that easy! Thanks for your interest and support). Happy programming!

Shout it


Anonymous said...

I tried Eval() and bind() expressions and it worked really good.But I am able to add TextBox but not textarea, the reason I have the content which is not single line statement.

So how do I get a multiple Lined Edit and insertified that supports updating and insertion.

P.S: I tried adding textarea instead of textbox and got Error

Thanks in advance!

Unknown said...

Hi, San.
Could you try using TextBox and setting TextMode="MultiLine"? That would give you go with multiple lines.

Please let me know if this does not work for you. Hope this helps. Thanks.

Anonymous said...


Nikhil B Singh said...

I trying to bind a column in grid view using eval() method but it doesn't work properly, as it is binding the column's data...(as my requirement is to search any keyword given n text box and then display it and highlight too).. but shows only provided keyword e.g. us instead of user.
code snippets is as follows:


Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles