这是我第一次去微软中文社区,第一次在那里提问,学到了好多东西,我在这里记下这个第一次
mountain315 2007/3/23 21:07
我在js里添加过DropDownList的ListItem,出现了这个错误。
我试过一些方法解决问题:
1、设置<pages enableEventValidation="false"/>,这样页面上别的控件不可以提交
2、把初始数据全部写DropDownList里,在页面Load完的时候再把当前用不上的ListItem用js删除,在程序里需要一些项时,再用js添加,这种方法是叫他自己注册的,就是如果我有未知项,我就没有办法先注册了
我现在想请问怎么使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据
v-jicwan@prcvap.microsoft.com 2007/3/25 21:13
关于ClientScriptManager.RegisterForEventValidation的使用,请参考下述代码:
   <%@ Page EnableEventValidation="true" Language="VB"  %>
<!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">
    <title>Untitled Page</title>
    <script runat="server">
    </script>
    <script type="text/javascript">
        function InitializeDDL()
        {
            var oOption = document.createElement("OPTION");
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Canada";        
            oOption = document.createElement("OPTION");           
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Mexico";
            oOption = document.createElement("OPTION");               
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "United States"; 
        }
    </script>
</head>
<body onload="InitializeDDL();">
    <form id="form1" runat="server">
    <div>
        <select size="0" ID="DropDownList1" runat="server">
        </select>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <div id="Result"></div>
        <div id="Context"></div>
    </div>
    </form>
</body>
</html>
mountain315 2007/3/26 5:19
v-jicwan@prcvap.microsoft.com 2007/3/27 23:33
我已经在我的电脑上测试了下述代码,发现都没有问题,请您参考一下(我发现您的错误代码与我提供的不一样,在我的上次代码实例中我使用的是Select HTML控件):
1.<%@ Page EnableEventValidation="true" Language="VB"  %>
<!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 runat="server">
    <title>Untitled Page</title>
    <script runat="server">                      //注意这里登记事件验证
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            ClientScript.RegisterForEventValidation("DropDownList1", "Canada")
            ClientScript.RegisterForEventValidation("DropDownList1", "Mexico")
            ClientScript.RegisterForEventValidation("DropDownList1", "United States")
            MyBase.Render(writer)
        End Sub
    </script>
    <script type="text/javascript">
        function InitializeDDL()
        {
            var oOption = document.createElement("OPTION");
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Canada";           
            oOption = document.createElement("OPTION");           
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Mexico";
            oOption = document.createElement("OPTION");               
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "United States";
        }
    </script>
</head>
<body onload="InitializeDDL();">
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
2.<%@ Page EnableEventValidation="true" Language="VB"  %>
<!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">
    <title>Untitled Page</title>
    <script runat="server">
    </script>
    <script type="text/javascript">
        function InitializeDDL()
        {
            var oOption = document.createElement("OPTION");
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Canada";        
            oOption = document.createElement("OPTION");           
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "Mexico";
            oOption = document.createElement("OPTION");               
            document.all("DropDownList1").options.add(oOption);
            oOption.innerText = "United States"; 
        }
    </script>
</head>
<body onload="InitializeDDL();">
    <form id="form1" runat="server">
    <div>                             //注意这里使用的是Select,而不是DropDownList
        <select size="0" ID="DropDownList1" runat="server">
        </select>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <div id="Result"></div>
        <div id="Context"></div>
    </div>
    </form>
</body>
</html>
希望上述代码能够给您一些帮助。
mountain315 2007/3/28 5:53
您好,谢谢您给我的帮助,现在已经不报错了,就是不可以在服务器端取到新添加的值,只可以在客户端使用js取值,我的测试代码是这样的:
test.aspx页面:
<%@ Page Language="C#" EnableEventValidation="true" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!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">
    <title>无标题页</title>
    <script type="text/javascript">
        function InitializeDDL()
        {
            var oSelect;
            oSelect=document.all("DropDownList1");
            oSelect.options[oSelect.options.length]=new Option("Canada","Canada");
            oSelect.options[oSelect.options.length]=new Option("Mexico","Mexico");
            oSelect.options[oSelect.options.length]=new Option("United States","United States");
            
            oSelect=document.all("DropDownList2");
            oSelect.options[oSelect.options.length]=new Option("Canada","Canada");
            oSelect.options[oSelect.options.length]=new Option("Mexico","Mexico");
            oSelect.options[oSelect.options.length]=new Option("United States","United States");
        }
        function getValue()
        {
            var oSelect;
            oSelect=document.all("DropDownList1");
            alert(oSelect.value);
            oSelect=document.all("DropDownList2");
            alert(oSelect.value);
        }
    </script>
</head>
<body onload="InitializeDDL();">
    <form id="form1" runat="server">
    <div>
        服务器端控件:
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>aaa</asp:ListItem>
            <asp:ListItem>bbb</asp:ListItem>
            <asp:ListItem>ccc</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        客户端控件:
        <select id="DropDownList2" runat="server">
        </select>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
        <asp:Label ID="Label2" runat="server"></asp:Label>
        <br />
        <input type="button" value="get value" onclick="getValue();" />
    </div>
    </form>
</body>
</html>
test.aspx.cs页面:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "Canada");
        ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "Mexico");
        ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "United States");
        base.Render(writer);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedValue;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
        //Label2.Text = DropDownList2.Items[DropDownList2.SelectedIndex].Value;
    }
}
v-jicwan@prcvap.microsoft.com 2007/3/29 0:27
v-jicwan@prcvap.microsoft.com 2007/3/29 2:49
Rimifon 2007/3/29 19:01
v-jicwan@prcvap.microsoft.com 2007/3/29 19:23
Rimifon 2007/3/30 0:23
您的专长应该不是Web这块,我觉得既然是微软新闻组的在线技术支持工程师,应该各个新闻组都由比较熟悉该新闻组相关技术的人员担任比较合适。
随便逛了一下,发现多个新闻组都有Jasson Wang在热心的为广大网友FAQ,在这里只能说声:Jasson Wang,辛苦了。
v-jicwan@prcvap.microsoft.com 2007/3/30 2:39
您好:
   非常感谢您的建议,我们将会在以后的工作中考虑您的建议并改进我们的工作,MSDN新闻组是一个开放式的论坛,如果你对论坛中某些板块的内容感兴趣的话,也可以参与进来。MSDN新闻组是为了帮助所有使用微软技术的人,我们无法承诺解决所有的问题,如果您的问题表述清楚,我们会尽量帮助您解决。如果您需要更加有效而快速的解决方案的话,您可以向微软的技术支持中心以Case的形式提交您的问题,具体的联系方式如下:
如果遇到过于复杂而需要快速有效解决方案的问题,我们建议您直接拨打以下电话联系微软的电话技术支持以便于更好的沟通和交流:
中国大陆地区: 800-820-3800
香港地区:       23889600
由于MSDN新闻组的问题涉及的内容比较广,我们每个人无法覆盖所有的技术领域,只能尽可能的根据个人所知来回答客户的问题。对于这些问题的解决,如果您有更好的方法,请提交给我们,我们将会慎重的考虑您的建议。
上述观点不代表微软的官方观点,仅限于与网友讨论。
Jasson Wang
在线技术支持工程师
微软全球技术支持中心
mountain315 2007/3/30 4:33 PST
啊,非常感谢您给我的帮助!
我在学asp.net1.1的时候,当我想要把表单从a.aspx提交到b.aspx,当我想要从b.aspx取a.aspx的控件值时,我没有办法用Request.Form取到,一定要在a.aspx把控件注册成public类,设置get和set,还要在b.aspx页面使用<@ Referenct Page="a.aspx">,才可以取到,所以从那时候开始,我就一直没有在asp.net里使用过Request.Form了
我真的想不到,这种情况下,可以使用Request.Form取到值,真的太谢谢你了
就是我还想继续提问,因为现在这种注册的方法,要使用js修改的项是一定要已经知道的才可以,如果我有未知的项,我就没有办法注册了
还有就是,如果是一个级联的菜单,我设第1个菜单下有2个菜单,第2个菜单下有200个菜单,如果使用这种方法注册,为了使级联菜单有效,我就要在一开始的时候,在Render事件里,从数据库读取202个数据注册,这个对只选择第1个菜单的用户好不公平啊,他们是不需要读取这么多数据的啊
有没有办法可以做到真的是动态注册呢?不是程序代码事先规定的,是真的可以动态的注册项
还有就是对于您提到的Request.Form,我想到了另一个问题,我测试了一下,<form id="form1" runat="server" method="post" action="test2.aspx">,我在aspx页面里这样设置,我是希望这个表单可以提交到test2.aspx页面,我是想测试一下在test2.aspx页面使用Request.Form可以不可以取到值,就是我这样的设置,生成的html语句还是这样的:<form name="form1" method="post" action="test.aspx" id="form1">,他不管我的action设置,这是为什么呢?只可以自提交的吗?
Rimifon 2007/3/30 17:56 PST
测试了一下,确实有这个问题。
不过可以使用客户端js脚本修改action。
<script type="text/javascript">
onload = function()
{
    document.form1.action="test2.aspx";
}
</script>
mountain315 2007/3/31 4:15 PST
嗯,我测试了,是可以使用js修改提交,提交后在target页面还可以使用Request.Form或者Request.QueryString取值
就是要设置<%@ Page enableviewstatemac="false" %>,如果不设置这个,会提示“验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用AutoGenerate。”
谢谢你
现在就差DropDownList项的注册了,我是在想,是不是一定要使用ajax才可以做到这样的注册啊?
如果我在页面里使用window.showModalDialog打开了一个页面,我在这个页面里为主页面的DropDownList添加了项目,这些项目肯定是未知的,由用户输入的,这种情况下,我是没有办法在主页面一开始时就使用Render方法注册的,这种情况下,我应该怎么注册这些新添加的项呢?
Rimifon 2007/3/31 21:10 PST
服务端接收到Request.Form数据后,应该可以对DropDownList的Items进行Add或Remove操作,可以在这时候注册你的ListItem项。
另外,Request.Form[index]返回的已经是字符串了,不建议再使用ToString()方法转换成字符串。而且,当Request.Form[index]值为null时,便会引发异常。
mountain315 2007/4/1 3:52 PST
谢谢你的建议
嗯,我是想要知道不直接在服务器端操作ListItem时的注册方法,因为像我昨天说的那种情况,要添加的项是未知的,还有就是弹出窗口里不可能注册每一个调用它的引用的页面,所以我没有办法在弹出窗口的服务器端修改parnet窗口的DropDownList,我可以用js返回值,这样就只可以用js修改DropDownList的ListItem项了
我原来不懂得使用Rander方法,现在知道怎么使用了,就是发现使用这种方法注册的ListItem的值是已知的,所以我现在不知道对未知的ListItem怎么注册了
v-jicwan@prcvap.microsoft.com 2007/4/1 23:01 PST
建议您阅读一下下述博客:
   http://recordsome.blogsome.com/2006/05/30/aspnet-event-validation/
     http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx
     http://odetocode.com/Blogs/scott/archive/2006/03/21/3153.aspx
如果您有任何问题,请通过MSDN新闻组继续与我们交流。
mountain315 2007/4/2 5:05 PST
啊,谢谢你!又一次转到这三个博客上来了,我就是看了这种个博客才知道可以使用代码的方法注册的,就是我没有测试成功
我现在已经知道如果要实现这种东西,要使用ajax了,谢谢你给我的帮助,还有另一个朋友,在这里学到好多东西
我想请问一下,页面中dropdownlist1、dropdownlist2是联动的,其ListItem是动态的,以数组形式生成,其格式为:var Arrays =  new Array([0,'aaa,3], [0,'bbbbb',4]...)(注:数据库字段——ParentID,Name,ID),怎么添加ClientScript.RegisterForEventValidation的值呀????谢
我在这里回复你了:user1/9/archives/2007/3265.html