首页 > 解决方案 > Eval()、XPath() 和 Bind() 等数据绑定方法只能在 Gridview 中的数据绑定控件的上下文中使用

问题描述

我正在尝试构建一个包含几列的gridview。但是我遇到了这个错误“Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。”

在此处输入图像描述

我不确定为什么会这样。我尝试将 Eval("pid") 的语法更改为 (DataBinder.Eval(Container.DataItem,"pid")) 但无济于事。我可能会错过什么?这是我在其他几列中的一列的 aspx 代码。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                    ShowFooter="false" ShowHeaderWhenEmpty="true"
                    DataKeyNames="pid"
                    OnRowCommand="GridView1_RowCommand"
                    OnRowEditing="GridView1_RowEditing"
                    OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowUpdating="GridView1_RowUpdating"
                    AllowPaging="True" PageSize="30"
                    Width="100%" AllowSorting="True"
                    OnRowDataBound="GridView1_RowDataBound">    

                    <Columns>                           
                       <asp:TemplateField HeaderText="Project Number" ItemStyle-HorizontalAlign="center">
                            <ItemTemplate>                          
                                <asp:Label ID="lblPID" Text='<%# Eval("pid")  %>' Width="130px" runat="server"></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="txtProjectNumber" Text='<%# Eval("pid") %>' ToolTip='<%# Eval("pid") %>' Width="130px" runat="server"></asp:Label>
                            </EditItemTemplate>                               
                        </asp:TemplateField>
                   </Columns>
</asp:GridView>

任何帮助都非常感谢,因为我长期以来一直被困在这个问题上。提前致谢。

标签: c#asp.netgridview

解决方案


推荐阅读