基于DropDownlist选择更新数据库

时间:2014-03-15 18:31:20

标签: c# asp.net

我正在使用网格视图来显示数据,并希望在下拉列表中选择索引更改时更新数据库数量列。再次要明确我不想使用任何更新按钮,它应该采取行动作为下拉列表选择项目更改。 Grid-view的代码是

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" 
    DataKeyNames="Cart_id" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:CommandField DeleteText="Remove" ShowDeleteButton="True" 
            ButtonType="Image" DeleteImageUrl="~/images/remove-to-cart-icon.jpg" 
            HeaderText="Remove" >
        <ControlStyle Height="30px" Width="50px" />
        </asp:CommandField>
        <asp:TemplateField HeaderText="Cart_id" InsertVisible="False" 
            SortExpression="Cart_id" Visible="False">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Cart_id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Cart_id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Product_name" HeaderText="Product_name" 
            SortExpression="Product_name" ReadOnly="True" />
        <asp:BoundField DataField="Product_cost" HeaderText="Product_cost" 
            SortExpression="Product_cost" ReadOnly="True" />
        <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    SelectedValue='<%# Bind("Quantity", "{0}") %>' 
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
                    CommandName="Change" AutoPostBack="True">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>10</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:shoppingConnectionString2 %>" 
    DeleteCommand="DELETE FROM [Cart] WHERE [Cart_id] = @Cart_id" 
    SelectCommand="SELECT * FROM [Cart] WHERE ([User_id] = @User_id)" 
    InsertCommand="INSERT INTO [Cart] ([User_id], [Product_id], [Product_name], [Product_cost], [Quantity]) VALUES (@User_id, @Product_id, @Product_name, @Product_cost, @Quantity)" 
    UpdateCommand="UPDATE [Cart] SET [User_id] = @User_id, [Product_id] = @Product_id, [Product_name] = @Product_name, [Product_cost] = @Product_cost, [Quantity] = @Quantity WHERE [Cart_id] = @Cart_id">
    <DeleteParameters>
        <asp:Parameter Name="Cart_id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="User_id" Type="String" />
        <asp:Parameter Name="Product_id" Type="Int32" />
        <asp:Parameter Name="Product_name" Type="String" />
        <asp:Parameter Name="Product_cost" Type="Int32" />
        <asp:Parameter Name="Quantity" Type="Int32" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="User_id" QueryStringField="uid" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="User_id" Type="String" />
        <asp:Parameter Name="Product_id" Type="Int32" />
        <asp:Parameter Name="Product_name" Type="String" />
        <asp:Parameter Name="Product_cost" Type="Int32" />
        <asp:Parameter Name="Quantity" Type="Int32" />
        <asp:Parameter Name="Cart_id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

我希望在更改dropdownlist值时将cartid值存储在后端的变量中,以便我可以更新我的数据库。

1 个答案:

答案 0 :(得分:0)

我认为这只是在Load事件中调用SQLDataSource1.Update()的问题。

从MSDN关于SQLDataSource.Update()方法: “如果数据已更改,则在回发期间,GridView,DetailsView和FormView控件会自动调用Update方法。对于已在其他控件中更改的数据,可以在Load事件期间在回发时显式调用Update方法。”

看来你已经把所有东西都正确连接好了。所以,我会在你的Page_Load事件中尝试这个。

void Page_Load (Object sender, EventArgs e) {
    if (!IsPostBack) {
        // non post back stuff
    }
    else {
        SQLDataSource1.Update ();
    }
}

我还会从DropDownList标记中取出CommandName =“Change”。我认为这不会做任何事情,可能会阻碍这项行动。

如果这不起作用,可能尝试定义DropDownList1_SelectedIndexChanged事件:

void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
    SQLDataSource1.Update ();
}

很抱歉,希望我能更加确定,但我现在无法在我的开发机器上测试它。

希望这有帮助。

汤姆

相关问题