2

I'm trying to get value from DropDownList as a parameter for SQL query. I have a GridView and SqlDataSource. And I can't reach DropDownList ("DDLName") inside the GridView. When "DDLName" is outside the GridView, everything is working, but when I put It inside, I got an error which says that can't find "DDLName". I want to do this in .aspx. I've seen a lots of tips with using "$ but I was trying to use"GridView1$DDLName" and It's not working.

There is my code:

     <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>
4

1 回答 1

1

好问题。我花了几个小时来解决这个问题,但我做对了。你需要做两个改变。

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" />
于 2016-06-08T08:08:14.317 回答