what is the use of Eval() in asp.net

后端 未结 3 1983
独厮守ぢ
独厮守ぢ 2020-12-02 22:36

What is the use of Eval() in ASP.NET?

相关标签:
3条回答
  • 2020-12-02 23:19

    Eval is used to bind to an UI item that is setup to be read-only (eg: a label or a read-only text box), i.e., Eval is used for one way binding - for reading from a database into a UI field.

    It is generally used for late-bound data (not known from start) and usually bound to the smallest part of the data-bound control that contains a whole record. The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.

    0 讨论(0)
  • 2020-12-02 23:20

    While binding a databound control, you can evaluate a field of the row in your data source with eval() function.

    For example you can add a column to your gridview like that :

    <asp:BoundField DataField="YourFieldName" />
    

    And alternatively, this is the way with eval :

    <asp:TemplateField>
    <ItemTemplate>
            <asp:Label ID="lbl" runat="server" Text='<%# Eval("YourFieldName") %>'>
            </asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    

    It seems a little bit complex, but it's flexible, because you can set any property of the control with the eval() function :

    <asp:TemplateField>
        <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" 
              NavigateUrl='<%# "ShowDetails.aspx?id="+Eval("Id") %>' 
              Text='<%# Eval("Text", "{0}") %>'></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    
    0 讨论(0)
  • 2020-12-02 23:29

    IrishChieftain didn't really address the question, so here's my take:

    eval() is supposed to be used for data that is not known at run time. Whether that be user input (dangerous) or other sources.

    0 讨论(0)
提交回复
热议问题