根据下拉列表值插入

时间:2012-08-14 11:26:45

标签: c# asp.net

好吧,基于下拉值,我需要将表单中的值插入到数据库表中..但它现在不能正常工作..

这是我的aspx页面 -

<asp:DropDownList ID="DropDownList1" class="box" runat="server" AutoPostBack="True">
        <asp:ListItem>Confirm Order</asp:ListItem>
        <asp:ListItem>Tentative Order</asp:ListItem>
    </asp:DropDownList>

然后我用了开关..

switch (DropDownList1.SelectedValue) 
{ 
    case "Confirm Order": 
        ... INSERT 
    break; 
    case "Tentative Order": 
        ...Insert 
    break;
}

但它给出了一个错误 -

No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type.

这样做的正确方法是什么?

6 个答案:

答案 0 :(得分:2)

确保您正在使用:

DropDownList1.SelectedValue

DropDownList1.SelectedItem.Value

DropDownList1.SelectedItem.Text

在INSERT查询中

(而不是DropDownList1.SelectedItem - 这将给出ListItem)

答案 1 :(得分:1)

应该是

switch (DropDownList1.SelectedItem.Text)
{
    //Rest Remains the same
}

答案 2 :(得分:0)

我想问题是你将DDL映射到类“box”,并且在你的switch / case语句中你没有使用这个映射。

答案 3 :(得分:0)

尝试使用:

DropDownList1.SelectedItem.Value

答案 4 :(得分:0)

您可以将ddl.SelectedItem.Value作为参考,并将值传递给所需的方法。

String tempString = yourDDL.SelectedItem.Value

答案 5 :(得分:0)

为什么不使用ID值 例如

<asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem runat="server" Text="Confirm Order" Value="1"></asp:ListItem>
 </asp:DropDownList>

并切换

switch (DropDownList1.SelectedValue) 
{ 
    case "1": 
        ... INSERT 
}
相关问题