ASP.NET 在参数 ControlParameter 中找不到对象

ASP.NET Cannot find object in parameter ControlParameter

我正在尝试从 DropDownList 获取值作为 SQL 查询的参数。我有一个 GridView 和 SqlDataSource。而且我无法在 GridView 中访问 DropDownList ("DDLName")。当 "DDLName" 在 GridView 之外时,一切正常,但是当我把它放在里面时,我得到一个错误,说找不到 "DDLName"。我想在 .aspx 中执行此操作。我看过很多使用 "$ 的提示,但我试图使用 "GridView1$DDLName" 但它不起作用。

有我的代码:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource2" AutoGenerateEditButton="True" ShowFooter="True">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
            <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
            <asp:TemplateField HeaderText="city">
                <ItemTemplate>
                    <asp:Label ID = "LabelName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID = "DDLName" runat="server" DataSourceID="DDLSource" DataTextField="name" ClientIDMode="Static"></asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="DDLSource" runat="server" ConnectionString="<%$ ConnectionStrings:MBase1ConnectionString %>" SelectCommand="SELECT [name] FROM [cities]">
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MBase1ConnectionString %>" SelectCommand="users_select_all_city" SelectCommandType="StoredProcedure" UpdateCommand="users_update_city" UpdateCommandType="StoredProcedure" DeleteCommand="users_delete" DeleteCommandType="StoredProcedure" InsertCommand="users_add" InsertCommandType="StoredProcedure">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:Parameter Name="name" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:ControlParameter ControlId="DDLName" Name="name" PropertyName="SelectedValue" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

很好 question.Took 我花了几个小时解决了这个问题,但我明白了 right.You 需要进行两处更改。

1) 将您的下拉菜单更改为如下所示:

<asp:DropDownList ID="DDLName" runat="server" 
    DataSourceID="DDLSource" 
    DataTextField="name" 
    DataValueField="name"
    SelectedValue='<%# Bind("name") %>'>
</asp:DropDownList>

2) 替换此行:

<asp:ControlParameter ControlId="DDLName" Name="name" PropertyName="SelectedValue" Type="String" />

有了这个:

<asp:Parameter Name="name" Type="String" />