由DropDownList中取值,并存入数据库后,在通过数据库取出相应的值,重新付给DropDownList显示的时候,最好加个判断,这样避免出现赋值异常错误,比如:
在page_load中加入
DropDownList1.Items.Add(New ListItem("okok1", "N1"))
DropDownList1.Items.Add(New ListItem("okok2", "N2"))
DropDownList1.Items.Add(New ListItem("okok3", "N3"))
赋值的时候直接用
DropDownList1.SelectedValue="N2"
来赋值,但是如果出现了偏差,比如写成了DropDownList1.SelectedValue="N21"就会出现错误,所以,因该在赋值前进行判断,比如
IF DropDownList1.Items.FindByValue("N2").Selected = true THEN
DropDownList1.SelectedValue="N2"
End IF
当然还有另外一种非常有效的办法,就是用Try...Catch...Finally来进行约束即可!
另外,如果对DropDownList加入直接写入的功能,可以参考:
在page_load中加入
DropDownList1.Items.Add(New ListItem("okok1", "N1"))
DropDownList1.Items.Add(New ListItem("okok2", "N2"))
DropDownList1.Items.Add(New ListItem("okok3", "N3"))
赋值的时候直接用
DropDownList1.SelectedValue="N2"
来赋值,但是如果出现了偏差,比如写成了DropDownList1.SelectedValue="N21"就会出现错误,所以,因该在赋值前进行判断,比如
IF DropDownList1.Items.FindByValue("N2").Selected = true THEN
DropDownList1.SelectedValue="N2"
End IF
当然还有另外一种非常有效的办法,就是用Try...Catch...Finally来进行约束即可!
另外,如果对DropDownList加入直接写入的功能,可以参考:
<script language="JavaScript">
//使DropDownList可以进行输入操作
//调用 onkeydown="catch_keydown(this);" onkeypress="catch_press(this);"
function catch_keydown(sel) { switch(event.keyCode) {
case 13: //Enter;
sel.options[sel.length] = new Option("","",false,true);
event.returnValue = false;
break;
case 27: //Esc;
alert("text:" + sel.options[sel.selectedIndex].text + ", value:" + sel.options[sel.selectedIndex].value + ";");
event.returnValue = false;
break;
case 46: //Delete;
if(confirm("刪除當前內容!?")) {
sel.options[sel.selectedIndex] = null;
if(sel.length>0) { sel.options[0].selected = true; } }
event.returnValue = false; break;
case 8: //Back Space;
var s = sel.options[sel.selectedIndex].text;
sel.options[sel.selectedIndex].text = s.substr(0,s.length-1);
event.returnValue = false; break; } }
function catch_press(sel) {
sel.options[sel.selectedIndex].text = sel.options[sel.selectedIndex].text + String.fromCharCode
(event.keyCode); event.returnValue = false;
}
</script>
//使DropDownList可以进行输入操作
//调用 onkeydown="catch_keydown(this);" onkeypress="catch_press(this);"
function catch_keydown(sel) { switch(event.keyCode) {
case 13: //Enter;
sel.options[sel.length] = new Option("","",false,true);
event.returnValue = false;
break;
case 27: //Esc;
alert("text:" + sel.options[sel.selectedIndex].text + ", value:" + sel.options[sel.selectedIndex].value + ";");
event.returnValue = false;
break;
case 46: //Delete;
if(confirm("刪除當前內容!?")) {
sel.options[sel.selectedIndex] = null;
if(sel.length>0) { sel.options[0].selected = true; } }
event.returnValue = false; break;
case 8: //Back Space;
var s = sel.options[sel.selectedIndex].text;
sel.options[sel.selectedIndex].text = s.substr(0,s.length-1);
event.returnValue = false; break; } }
function catch_press(sel) {
sel.options[sel.selectedIndex].text = sel.options[sel.selectedIndex].text + String.fromCharCode
(event.keyCode); event.returnValue = false;
}
</script>