locked
Update the SqlDataSource for ListView RRS feed

  • Question

  • User675515096 posted

    Is it possible to pass a variable to the SelectCommand in the SqlDataSource for a ListView, lets say from a DropDownList?

    Right now I have a value in SelectCommand that I would like to pass a variable to so the ListView would update and display the results based on that variable. As an example, lets say that I have a list of companies that appear in the dropdown and when selected from the list the ListView updates to display information about the selected company. 

    Wednesday, August 21, 2013 5:58 PM

Answers

  • User2103319870 posted

    Hi,

    Please try the below implementation

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
                <asp:ListItem Text="10" Value="10"></asp:ListItem>
                <asp:ListItem Text="20" Value="20"></asp:ListItem>
                <asp:ListItem Text="30" Value="30"></asp:ListItem>
                <asp:ListItem Text="40" Value="40"></asp:ListItem>
                <asp:ListItem Text="50" Value="50"></asp:ListItem>
            </asp:DropDownList>
    <br />
            <br />
    
            <asp:ListView ID="TestListView" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                <ul class="style">
                   <li> <asp:Label ID="Label1" runat="server" Text='<%# Eval("UnitPrice")%>'>  
                    </asp:Label></li>
                </ul>
                </ItemTemplate>
                <EmptyDataTemplate>
                    <div>
                        NO Data to Display
                    </div>
                </EmptyDataTemplate>
            </asp:ListView>
    
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] WHERE ([UnitPrice] <= @UnitPrice)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="UnitPrice"
                        Type="Int32" DefaultValue="0" />
                </SelectParameters>
            </asp:SqlDataSource>
    

    Please Ensure

    • The parameter names that you use in your SQL command correspond to the names of the parameters in the associated collection.
    • The ControlID is properly assigned to ControlParameter

    http://msdn.microsoft.com/en-us/library/z72eefad(v=vs.100).aspx




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 9:08 PM

All replies

  • User-1360095595 posted

    You can use a controlparameter in the sqldatasource markup. Easy. 

    Wednesday, August 21, 2013 6:10 PM
  • User2103319870 posted

    Hi,

    Please try the below implementation

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
                <asp:ListItem Text="10" Value="10"></asp:ListItem>
                <asp:ListItem Text="20" Value="20"></asp:ListItem>
                <asp:ListItem Text="30" Value="30"></asp:ListItem>
                <asp:ListItem Text="40" Value="40"></asp:ListItem>
                <asp:ListItem Text="50" Value="50"></asp:ListItem>
            </asp:DropDownList>
    <br />
            <br />
    
            <asp:ListView ID="TestListView" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                <ul class="style">
                   <li> <asp:Label ID="Label1" runat="server" Text='<%# Eval("UnitPrice")%>'>  
                    </asp:Label></li>
                </ul>
                </ItemTemplate>
                <EmptyDataTemplate>
                    <div>
                        NO Data to Display
                    </div>
                </EmptyDataTemplate>
            </asp:ListView>
    
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>"
                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] WHERE ([UnitPrice] <= @UnitPrice)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="UnitPrice"
                        Type="Int32" DefaultValue="0" />
                </SelectParameters>
            </asp:SqlDataSource>
    

    Please Ensure

    • The parameter names that you use in your SQL command correspond to the names of the parameters in the associated collection.
    • The ControlID is properly assigned to ControlParameter

    http://msdn.microsoft.com/en-us/library/z72eefad(v=vs.100).aspx




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 9:08 PM
  • User-1716253493 posted

    BIND DropDownList SelectedValue property 

    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# BIND("FIELDNAME") %>'>

    If you use this dropdown to update the data in edit template, pass the value using UpdateParameters

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            UpdateCommand="UPDATE SET FIELDNAME=@FIELDNAME">
            <UpdateParameters>
                <asp:Parameter Name="FIELDNAME" />
            </UpdateParameters>
        </asp:SqlDataSource>

    Bind is two way binding, you can use it to bind some control properties with appropriate type like TextBox Text, CheckBox Checked, DropDownList SelectedValue, etc

    When you using BIND in Editing with DropDownList SelectedValue property, first time you go to edit, selectedvalue come from your selectcommand results. After you click update button, the value passed throught UpdateParameter. Give parameter name same with fieldname.

    Wednesday, August 21, 2013 10:02 PM
  • User675515096 posted

    This works perfectly! Thanks! I'm actually surprised that it does since the dropdown list is actually outside of the defined ListView. But I guess it gets tied in with the SelectParameters.

    Wednesday, August 21, 2013 10:14 PM