本文介绍了由于javascript,下拉列表选择的值不保存所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用dropdownlist项将值插入数据库,我想要的是选择Item然后当我按下提交按钮时,数值将使用asp.net vb.net代码保存到sql server数据库,问题是,它总是在sqlserver数据库中保存0值,即使我选择1,2,3或4,问题是javascript,如果我删除javascript它可以选择我想要的值并且没有问题,但我需要如果它已经被选中,它将禁用该值,以避免重复数据库,有人可以帮助我弄清楚javascript,如果我尝试按下保存按钮,你可以看到下拉列表返回到其默认值0然后重置,而不是保存我从下拉列表中选择的值,谢谢....



我尝试过:



<%@ Page Language =VBAutoEventWireup =falseCodeFile =Default.aspx.vb

Inherits =_ DefaultDebug = true%>



&l t;!DOCTYPE html>



< html xmlns =http://www.w3.org/1999/xhtml>

< head runat =server>



< title>< / title>

< / head>

< body>



< form id =form1runat =server>

How can I Insert value to database using dropdownlist item, What I want is to select Item then when I press submit button, the numeric Value will save to sql server database using asp.net vb.net code, the problem is ,it always save 0 value in sqlserver database, even if I selected 1, 2 , 3, or 4 , the problem is the javascript, if I remove the javascript it can select the value that I want and there is no problem, but I need it to disable the value if its already selected, to avoid duplicates to the database, can someone help me to figure it out the javascript, if I try press the save button you can see that the dropdownlist is returning to its default value 0 then reset, and not saving the value that I selected from the dropdownlist thanks....

What I have tried:

<%@ 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">



< asp:DropDownList ID =frstrunat =serverAutoPostBack =true>

< asp:ListItem value = 0>选择

< asp:ListItem value =1> 1

< asp:ListItem value =2> 2

< asp:ListItem value =3> 3

< asp:ListItem value =4> 4



< asp:DropDownList ID =secndrunat =server>

< asp:ListItem value =0>选择

< asp:ListItem value =1> 1

< asp:ListItem value =2> 2

< asp:ListItem value =3> 3

< asp:ListItem value =4> 4







< asp:Button ID =btnSaverunat =serverText =Save

OnClick =btnSave_Click/>

< asp:Label ID =lblmsgrunat =server>




<asp:DropDownList ID = "frst" runat="server" AutoPostBack="true">
<asp:ListItem value="0">Select
<asp:ListItem value="1">1
<asp:ListItem value="2">2
<asp:ListItem value="3">3
<asp:ListItem value="4">4

<asp:DropDownList ID = "secnd" runat="server">
<asp:ListItem value="0">Select
<asp:ListItem value="1">1
<asp:ListItem value="2">2
<asp:ListItem value="3">3
<asp:ListItem value="4">4



<asp:Button ID="btnSave" runat="server" Text="Save"
OnClick="btnSave_Click" />
<asp:Label ID="lblmsg" runat="server">





< 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);



函数assignFunc(i,func){//完成

var x;

for(x = 0; x < i.length; ++ x){

i [x] .onchange = function(){

func(this);

};

}

}



函数disabler(i){

console.log(已选择);

updateS(opts);

massdisabler(opts);

}



函数makeid(i){//完成

var o = [];

i.forEach(function(v){

o.push(document.getElementById(v))

});

返回o;

}



函数查找(ids){//完成

var o = {};

for(var i = 0;我< ids.length; ++ i){

o [i] = {};

ids [i] .children.forEach(function(v,n){

o [i] [n] = v;

});

}



返回o;

}



function massdisabler(op){//完成

var y = Object.keys(op )。长度;

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){

返回true;

}

})){

t.setAttribute(disabled,disabled);

}其他{

t.removeAttribute(禁用);

}

}

}

}



函数更新(op){//完成

var s = [],

y = Object.keys(op).length;



for(var x = 0; x< Ÿ; 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;

如果(t.selected&&(!yn))s.push(t.value);

}

}

选择= s;

}

< / script>



< / form>



< / body>

< / html>



受保护的子btnSave_Click(发件人为对象,e为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 =插入名称值(@ FirstValue,@ SecondValue )

cmd.Parameters.Clear()

cmd.Parameters.AddWithValue(@ FirstValue,frst.SelectedItem.Value)

cmd.Parameters.AddWithValue(@ SecondValue,secnd.SelectedItem.Value)

如果cn.State = ConnectionState.Closed那么

cn.Open()

结束如果

cmd.ExecuteNonQuery()

cn.Close()

lblmsg.Text =数据输入成功! !!



End Sub



<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>

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

推荐答案


这篇关于由于javascript,下拉列表选择的值不保存所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:11