如何使用dropdownlist项将值插入数据库,我想要的是选择Item然后当我按下提交按钮时,数值将使用asp.net vb.net代码保存到sql server数据库,问题是,它总是在sqlserver数据库中保存0值,即使我选择1,2,3或4,问题是javascript,如果我删除javascript它可以选择我想要的值并且没有问题,但我需要如果它已经被选中,它将禁用该值,以避免重复数据库,有人可以帮助我弄清楚javascript,如果我尝试按下保存按钮,你可以看到下拉列表返回到其默认值0然后重置,而不是保存我从下拉列表中选择的值,感谢.............
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" Debug="true"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID = "frst" runat="server" AutoPostBack="true">
<asp:ListItem value="0">Select</asp:ListItem>
<asp:ListItem value="1">1</asp:ListItem>
<asp:ListItem value="2">2</asp:ListItem>
<asp:ListItem value="3">3</asp:ListItem>
<asp:ListItem value="4">4</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID = "secnd" runat="server">
<asp:ListItem value="0">Select</asp:ListItem>
<asp:ListItem value="1">1</asp:ListItem>
<asp:ListItem value="2">2</asp:ListItem>
<asp:ListItem value="3">3</asp:ListItem>
<asp:ListItem value="4">4</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSave" runat="server" Text="Save"
OnClick="btnSave_Click" />
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</div>
<script type="text/javascript">
var ids = ['frst', 'secnd'];
NodeList.prototype.forEach = HTMLCollection.prototype.forEach =
Array.prototype.forEach;
var selected = [];
ids = makeid(ids);
var opts = find(ids);
updateS(opts);
massdisabler(opts);
assignFunc(ids, disabler);
function assignFunc(i, func) { //done
var x;
for (x = 0; x < i.length; ++x) {
i[x].onchange = function () {
func(this);
};
}
}
function disabler(i) {
console.log(selected);
updateS(opts);
massdisabler(opts);
}
function makeid(i) { //done
var o = [];
i.forEach(function (v) {
o.push(document.getElementById(v))
});
return o;
}
function find(ids) { //done
var o = {};
for (var i = 0; i < ids.length; ++i) {
o[i] = {};
ids[i].children.forEach(function (v, n) {
o[i][n] = v;
});
}
return o;
}
function massdisabler(op) { //done
var y = Object.keys(op).length;
for (var x = 0; x < y; x++) {
var t = 0,
l = Object.keys(opts[x]).length;
for (var v = 0; v < l; v++) {
t = op[x][v];
if (selected.some(function (k) {
if (t.value == k) {
return true;
}
})) {
t.setAttribute("disabled", "disabled");
} else {
t.removeAttribute("disabled");
}
}
}
}
function updateS(op) { //done
var s = [],
y = Object.keys(op).length;
for (var x = 0; x < y; x++) {
var t = 0,
l = Object.keys(opts[x]).length;
for (var v = 0; v < l; v++) {
t = op[x][v];
var yn = s.some(function (k) {
if (t.value == k) {
return true;
}
});
if (t.selected && (!yn)) s.push(t.value);
}
}
selected = s;
}
</script>
</form>
</body>
</html>
有人可以帮助我正在尝试修改我的代码请不要downvote我只是一个初学者,谢谢这是整个代码请帮助我我这样做几个星期:
Protected Sub btnSave_Click(sender As Object, e As EventArgs)
Dim cs As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cn As SqlConnection = New SqlConnection(cs)
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into Names values(@FirstValue,@SecondValue)"
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@FirstValue", frst.SelectedItem.Value)
cmd.Parameters.AddWithValue("@SecondValue", secnd.SelectedItem.Value)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
cmd.ExecuteNonQuery()
cn.Close()
lblmsg.Text = "Data entered successfully!!!"
End Sub
答案 0 :(得分:0)
取viewstate
或hiddenfield
并将dropdownlist
SelectedValue
分配给其中任何一个。
您的问题将得到解决。