【问题标题】:C# - How to populate a DropDownList in EditItemTemplateC# - 如何在 EditItemTemplate 中填充 DropDownList
【发布时间】:2015-07-13 22:13:43
【问题描述】:

这里,我在 EditItemTemplate 中有一个 DropDownList:

<asp:TemplateField HeaderText="RequestedBy" SortExpression="RequestedBy">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ReqUserDDL" runat="server" AppendDataBoundItems ="True" DataSourceID="ReqUsersDataS" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name") %>' >
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="ReqUsersDataS" runat="server" ConnectionString="<%$ ConnectionStrings:itassetmgmtConnectionString1 %>" SelectCommand="SELECT Firstname + Lastname AS Name FROM Users"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Eval("RequestedBy") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

但是,我不断收到此错误:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.

有没有办法解决这个问题?

【问题讨论】:

    标签: c# gridview edititemtemplate


    【解决方案1】:

    使用Eval 代替BindBind 用于读取和/或写入,Eval 是只读的。

    编辑: Eval/Bind 错误不是来自 ReqUsersDataS 数据源。它试图在具有您显示的 TemplateField 的 GridView 的数据源上找到一个名为“名称”的列。您只需要确保其他数据源具有名为“名称”的列。

    【讨论】:

    • 'ReqUserDDL' 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值
    • 好吧,您需要确保您想要选择的任何值都实际存在于 DropDownList 中。如果 DropDownList 的值是 10、20、30,而您将 SelectedValue 设置为 40,则会出现该错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    相关资源
    最近更新 更多