<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
manage.aspx.cs
"
Inherits
=
"
useradmin_user_run
"
%>

<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>

<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head id
=
"
Head1
"
runat
=
"
server
"
>

<
script type
=
"
text/javascript
"
>
function SelectAll()
...
{
var lst1=window.document.getElementById("<%=ListBox1.ClientID %>");
var length = lst1.options.length;
var string = window.document.getElementById("<%=HiddenField1.ClientID %>")
for(var i=0;i<length;i++)
...{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementById("<%=ListBox2.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
string.value+=v;
}
}
function DelAll()
...
{
var lst2=window.document.getElementById("<%=ListBox2.ClientID %>");
var length = lst2.options.length;
for(var i=length;i>0;i--)
...{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
}
function SelectOne()
...
{
var string = window.document.getElementById("<%=HiddenField1.ClientID %>")
var lst1=window.document.getElementById("<%=ListBox1.ClientID %>");
var lst2=window.document.getElementById("<%=ListBox2.ClientID %>");
var lstindex=lst1.selectedIndex;
var length = lst2.options.length;
var isExists = false;
if(lstindex<0)
return;
else if(length != null)
...{
for(var i=0;i < length; i++)
...{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
...{
isExists = true;
}
}
}
else
...{
return;
}
if (isExists == false)
...{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
string.value+=v;
}
else
...{
alert("所选条目已经存在");
return false;
}
}
function DelOne()
...
{
var lst2=window.document.getElementById("<%=ListBox2.ClientID %>");
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
...{
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
}
}
</
script
>


<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
table width
=
"
400px
"
style
=
"
height: 270px; text-align:center;
"
>
<
tr
>
<
td style
=
"
width: 120px; height: 321px
"
rowspan
=
"
4
"
>
<
asp:ListBox ID
=
"
ListBox1
"
runat
=
"
server
"
Height
=
"
316px
"
Width
=
"
133px
"
></
asp:ListBox
></
td
>
<
td style
=
"
width: 136px; height: 60px
"
>
<
input id
=
"
Button1
"
type
=
"
button
"
value
=
"
SelectAll
"
onclick
=
"
SelectAll()
"
/></
td
>
<
td style
=
"
width: 137px; height: 321px
"
rowspan
=
"
4
"
>
<
asp:ListBox ID
=
"
ListBox2
"
runat
=
"
server
"
Height
=
"
320px
"
Width
=
"
141px
"
></
asp:ListBox
></
td
>
</
tr
>
<
tr
>
<
td style
=
"
height: 10px
"
>
<
input id
=
"
Button2
"
type
=
"
button
"
value
=
"
DelAll
"
onclick
=
"
DelAll()
"
/></
td
>
</
tr
>
<
tr
>
<
td
>
<
input id
=
"
Button3
"
type
=
"
button
"
value
=
"
SelectOne
"
onclick
=
"
SelectOne()
"
/></
td
>
</
tr
>
<
tr
>
<
td style
=
"
height: 84px
"
>
<
input id
=
"
Button4
"
type
=
"
button
"
value
=
"
DelOne
"
onclick
=
"
DelOne()
"
/></
td
>
</
tr
>
</
table
>
<
asp:HiddenField ID
=
"
HiddenField1
"
runat
=
"
server
"
OnValueChanged
=
"
HiddenField1_ValueChanged
"
/>
</
div
>
</
form
>
</
body
>
</
html
>
完整的出错信息:
出错原因有两种:
1、页面里有<form>的嵌套
2、用js动态给DropDownList控件添加了Load页面时DropDownList里面没有的ListItem
解决办法:
1、把嵌套的<form>去掉就可以了,我在做导出静态页面后的搜索时,遇过这种情况,我把<form>去掉,使用js在地址栏里提交参数了,这个方法写在这里(user1/9/archives/2006/3223.html)
2、这个我不知道我上面的描述对不对,我的意思是说,如果在页面Load的时候,DropDownList有三个ListItem
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
这时,如果使用JavaScript给这个DropDownList再加一项
这样就给下拉菜单加了一项:<asp:ListItem>4</asp:ListItem>了
这一项,在页面Load的时候,是没有的,就是没有写进隐藏域
name="__EVENTVALIDATION"
id="__EVENTVALIDATION"
value="/wEWBQKGg9abDQKd9sHMBgKc9s........"
/>
所以呢,在提交数据的时候,在检验数据的时候,就会发现这一项是没有的,就是没有注册的意思,是客户自己加在下拉菜单里的数据,有可能是有问题的数据,所以验证数据就不通过了
如果按照错误提示里说的,设置
你就会发现,服务器端读取不到下拉菜单的选中值了
我看了这三个东西,读的不是很懂
http://recordsome.blogsome.com/2006/05/30/aspnet-event-validation/http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspxhttp://odetocode.com/Blogs/scott/archive/2006/03/21/3153.aspx
所以我就想了另一个办法,很简单的办法,因为数据是在页面Load的时候注册的,所以我就把初始数据全部写DropDownList里,在页面Load完的时候再把不要的数据删除
这种解决方法有一个不好的地方,就是如果要用到的项是未知的,那就没有办法预先加载了,这种办法就用不上了
我遇到这种情况了的,就是那种字典表,如果我想要在用户录入的时候,让用户随便添加字典表里的内容,添加数据到字典表可以使用window.showModalDialog打开一个模式窗口添加,添加完了,数据不可以自动写回下拉菜单了
有两个问题:
1、如果使用模式窗口返回添加的数据,用JavaScript把返回的数据写入下拉菜单,这个新添加的ListItem就会因为没有注册而出错了
2、因为上面的原因,所以我没有用JavaScript添加ListItem,我加了一个小button在下拉菜单的旁边,用来刷新下拉菜单,在刷新下拉菜单的时候,如果直接用
会报错的,详细出错信息忘了,记不起来了,我的解决办法是重新一项一项读取
private void RefrestCheckBoxList(ObjectDataSource ods, string ddlna)
{
//刷新CheckBoxList
CheckBoxList mycbl = new CheckBoxList();
mycbl = (CheckBoxList)FormView1.FindControl(ddlna);
if (mycbl != null)
{
DataView dv = new DataView();
dv = (DataView)ods.Select();
//清空Items
mycbl.Items.Clear();
//添加Items
foreach (DataRowView drv in dv)
{
mycbl.Items.Add(new ListItem(drv.Row["dna"].ToString(), drv.Row["dna"].ToString()));
}
}
}
你的数组里的内容是程序在编写时就可以预知的吗?比如说是在数据库里的,可以直接读取数据库的,比如说像省市一样,是已经知道的内容
如果是这样的,你可以使用Render方法注册,使用这个方法后,取数据时使用Request.Form("DropDownList2")取值,下面这一段我没有放在vs.net2005里测试过,我想这样应该可以把每一项注册进去:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//读取数据库
//获取连接字符串
string connectionString = ConfigurationManager.ConnectionStrings["web.config里记录连接字串的节点名"].ConnectionString;
//创建并设置SqlConnection
SqlConnection dbConnection = new SqlConnection(connectionString);
string value="";
queryString = "select value字段的字段名 from 表名";
try
{
//取数据
dbConnection.Open();
SqlCommand MyCommand = new SqlCommand(queryString, dbConnection);
SqlDataReader dr = MyCommand.ExecuteReader();
DataRow row;
//循环写数据
while (dr.Read())
{
value = dr["value字段的字段名"].ToString();
ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, value);
}
}
catch (Exception)
{
//
}
finally
{
dbConnection.Close();
}
base.Render(writer);
}
这个方法只是注册,联动还是要通过js来做,就是使用了这个方法注册后,js修改的值,只要是这个方法里注册过的值,就不会再报错了
本文探讨了ASP.NET中常见的事件验证错误,并提供了详细的解决步骤。主要针对因动态添加控件选项导致的验证失败问题,介绍了多种解决方案,包括调整页面结构、注册验证及自定义数据绑定等。
923





