The asp.net Eval() and Bind() expressions are heavily used in asp.net 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 asp.net Label control whereas Bind can be used to bind the asp.net TextBox control so that it can be edited to meet some requirement. Of course, asp.net 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 asp.net 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%" />
<Fields>
<asp:BoundField DataField="Category_ID" HeaderText="ID" InsertVisible="false" ReadOnly="true"/>
<asp:TemplateField HeaderText="Category Name">
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("Category_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCategoryName" runat="server" Text='<%# Bind("Category_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="Active" HeaderText="Active"/>
</Fields>
</asp:DetailsView>
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 asp.net 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 asp.net GridView and DetailsView controls and an asp.net 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!
3 comments:
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!
San
Hi, San.
Could you try using asp.net 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.
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:
thanks!
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!