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>
2
  • Where are you trying to access the value of the drop down?During which event? Commented Jun 8, 2016 at 4:29
  • @DenisWessels UpdateCommand="users_update_city" - while updating a row by stored procedure Commented Jun 8, 2016 at 5:44

1 Answer 1

1

Good question.Took me a few hours to solve this but I got it right.You need to make two changes.

1) Change your drop down to be like this:

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

2) Replace this line:

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

With this:

<asp:Parameter Name="name" Type="String" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.