Thursday, 3 May 2012

Eval Vs Bind in .Net


For read-only controls they are the same. For 2 way databinding, using a datasource in which you want to update, insert, etc with declarative databinding, you'll need to use “Bind.”

e.g.
      a GridView with a “
ItemTemplate” and “EditItemTemplate”. If you use “Bind” or “Eval” in the “ItemTemplate”, there will be no difference. If you use Eval in the EditItemTemplate, the value will not be able to be passed to the Update method of the DataSource that the grid is bound to.
     
   <asp:GridView
            ID="gvTest"
            runat="server"
            AutoGenerateEditButton="true"
            AutoGenerateColumns="false"
            DataSourceID="mySrc">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox
                            ID="edtName"
                            runat="server"
                            Text='<%# Bind("Name") %>'
                        />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    <asp:ObjectDataSource
        ID="mySrc"
        runat="server"
        SelectMethod="Select"
        UpdateMethod="Update"
        TypeName="MyCompany.CustomDataSource" />

No comments:

Post a comment