sql >> Base de Datos >  >> RDS >> Sqlserver

¿Cómo asignar el valor seleccionado de DropDownList a SqlDataSource?

Si usa SqlDataSource , la forma más fácil será vincular los datos al frente.

Base de datos

Nota:Creé TypeId como entero.

ASPX

Si el valor seleccionado de DropDownList es -1 , Fuente de datos Sql devolverá todos los artículos.

<asp:DropDownList ID="BookListddl" runat="server" AutoPostBack="True">
    <asp:ListItem Text="All" Value="-1" />
    <asp:ListItem Text="Fiction" Value="1" />
    <asp:ListItem Text="None Fiction" Value="2" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [BookTbl] WHERE [TypeId] = @TypeId OR @TypeId = -1">
    <SelectParameters>
        <asp:ControlParameter ControlID="BookListddl"
            PropertyName="SelectedValue"
            Name="TypeId"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>