您所在的位置: 程序员家园 -> 家园博客 ->
 
在哪里摔倒
就在哪里自己爬起来

用户登录

查  找

最新评论

最新留言

常用网站

网易邮箱 GMAIL  

百度搜索 MSDN

霏凡软件 BT精品

影视帝国 射 手 网

电驴下载 全 库 网

友情连接

茄菲的窝 冰冰博客

枫叶飘零 玫  瑰

ACEN 云 豹 子

统  计



使用模态窗口返回后修改原窗口的列表控件,解决回发或回调参数无效
狼子 发表于 2007-4-4 16:49:00 阅读全文 | 回复(0) | 引用通告 | 编辑  asp.net查看本站使用过本标签的日志查看本站使用过本标签的用户

我们受够了没有?DropDownList和ListBox使用js后出错

回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。

经常都会用到这种处理方式:在弹出窗口里修改数据,把数据返回,然后呢,在当前窗口的DropDownList或者ListBox控件里添加在编写程序时是不确定的ListItem

这种方式经常用到,就像发送邮件时可以在弹出的窗口里添加附件,然后把附件的信息返回一样,从一个模态窗口返回不确定的项,要把这些项,使用js添加到<select>标签里很容易,就是如果要让这些不确定的项,通过上面的回发时的验证,好难好难。。。

添加的项,不可以通过js添加,一定要通过服务器端的代码来添加!

不要和我说有Render方法可以注册,在编写程序的时候,根本就不知道要注册的内容是什么,注册什么东西?

先记下这四个连接:

2006-11-15 14:32:00《使用showModalDialog打开模态窗口添加数据后刷新原窗口》:user1/9/archives/2006/3024.html

2006-12-28 19:13:00《模板列里出现“DropDownList 有一个无效 SelectedValue”的解决方法》:user1/9/archives/2006/3237.html

2007-01-10 19:24:00《回发或回调参数无效》:user1/9/archives/2007/3265.html

2007-04-02 20:28:00《在微软中文社区请教“回发或回调参数无效”》:user1/9/archives/2007/3584.html

从2006-11-15开始,到今天,2007-04-04,五个月了。。。

我就是用了一句form1.submit();

先记录这个js

    <script type="text/javascript">
        function openwin()
        {
            var win = showModalDialog("dropdownlist2.aspx", "", "dialogWidth:350px; dialogHeight:300px; status:0; help:0");
         if (win != null)
         {
          alert(win);
          var control,option;
          var tv=new Array();
          var text=new Array();
          var value=new Array();
          controlID="<% =this.DropDownList1.ClientID %>";
          control=document.getElementById(controlID);
          tv=win.split('||');
          text=tv[0].split('|');
          value=tv[1].split('|');
          var i,n;
          n=text.length;
          for(i=0;i<n;i++)
          {
              control.options[control.options.length]=new Option(text[i],value[i]);
          }
         }
     }
    </script>

我不知道使用这个js后,我给DropDownList1添加了option后,要怎么注册,就是这个js让我面对回发时验证出错的

现在我不用js添加option了,我让服务器自己做,让服务器自己注册,我使用了一句form1.submit();

<script type="text/javascript">
        function openwin()
        {
            var win = showModalDialog("dropdownlist2.aspx", "", "dialogWidth:350px; dialogHeight:300px; status:0; help:0");
         if (win != null)
         {
          alert(win);
          var controlID, control;
          controlID="<% =this.hfAddListItem.ClientID %>";
          control=document.getElementById(controlID);
          control.value=win;
          form1.submit();
         }
     }
    </script>

在这里,我使用了一个HiddenField控件做为开关变量,在js里给他赋值,在cs里使用后清空

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string tvStr;
            tvStr = hfAddListItem.Value;
            if (tvStr.Length > 0)
            {
                string[] tv;
                string[] text;
                string[] value;
                tv = tvStr.Split('*');
                text = tv[0].Split('|');
                value = tv[1].Split('|');
                int i, n;
                n = text.Length;
                for (i = 0; i < n; i++)
                {
                    DropDownList1.Items.Add(new ListItem(text[i], value[i]));
                }
                //刷新FormView里的DropDownList
                DropDownList ddl = new DropDownList();
                ddl = (DropDownList)FormView1.FindControl("DropDownList2");
                if (ddl != null)
                {
                    for (i = 0; i < n; i++)
                    {
                        ddl.Items.Add(new ListItem(text[i], value[i]));
                    }
                }
                //把hidden控件置空
                hfAddListItem.Value = "";
            }
        }
    }

我给出我的两个页面的完整测试代码,在测试中,我测试了直接放在页面上的DropDownList和放在FormView里的DropDownList:

dropdownlist.aspx

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

<!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>使用模态窗口返回数据,刷新DropDownList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <script type="text/javascript">
        function openwin()
        {
            var win = showModalDialog("dropdownlist2.aspx", "", "dialogWidth:350px; dialogHeight:300px; status:0; help:0");
         if (win != null)
         {
          alert(win);
          var controlID, control;
          controlID="<% =this.hfAddListItem.ClientID %>";
          control=document.getElementById(controlID);
          control.value=win;
          form1.submit();
         }
     }
    </script>
        <input id="Button1" type="button" value="打开模态窗口" onclick="openwin();" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        数据:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="请输入数据"></asp:RequiredFieldValidator><br />
        服务器端列表:<asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>第一项</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="刷新" /><br />
        <asp:Button ID="Button2" runat="server" Text="提交数据到服务器" OnClick="Button2_Click" />
        <asp:HiddenField ID="hfAddListItem" runat="server" />
        <asp:SqlDataSource ID="sdsAdd" runat="server" ConnectionString="<%$ ConnectionStrings:TTOAConnectionString %>"
            InsertCommand="insert into t_table (bb) values (@bb)" SelectCommand="select tid,bb from t_table where tid=0">
            <InsertParameters>
                <asp:Parameter Name="bb" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="tid" DataSourceID="sdsAdd"
            DefaultMode="Insert">
            <InsertItemTemplate>
                数据:<asp:TextBox ID="bbTextBox" runat="server" Text='<%# Bind("bb") %>'></asp:TextBox>
                <br />
                列表:<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                <br />
                <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入"></asp:Button>
            </InsertItemTemplate>
        </asp:FormView>
    </div>
    </form>
</body>
</html>

dropdownlist.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_dropdownlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string tvStr;
            tvStr = hfAddListItem.Value;
            if (tvStr.Length > 0)
            {
                string[] tv;
                string[] text;
                string[] value;
                tv = tvStr.Split('*');
                text = tv[0].Split('|');
                value = tv[1].Split('|');
                int i, n;
                n = text.Length;
                for (i = 0; i < n; i++)
                {
                    DropDownList1.Items.Add(new ListItem(text[i], value[i]));
                }
                //刷新FormView里的DropDownList
                DropDownList ddl = new DropDownList();
                ddl = (DropDownList)FormView1.FindControl("DropDownList2");
                if (ddl != null)
                {
                    for (i = 0; i < n; i++)
                    {
                        ddl.Items.Add(new ListItem(text[i], value[i]));
                    }
                }
                //把hidden控件置空
                hfAddListItem.Value = "";
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //提交数据
        string s;
        s = TextBox1.Text;
        s += "<br>共有列表:" + DropDownList1.Items.Count.ToString();
        Label1.Text=s;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string tvStr;
        tvStr = TextBox1.Text;
        if (tvStr.Length > 0)
        {
            string[] tv;
            string[] text;
            string[] value;
            tv = tvStr.Split('*');
            text = tv[0].Split('|');
            value = tv[1].Split('|');
            int i, n;
            n = text.Length;
            for (i = 0; i < n; i++)
            {
                DropDownList1.Items.Add(new ListItem(text[i], value[i]));
            }
        }
    }
}

dropdownlist2.aspx

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

<!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>dropdownlist的模态窗口</title>
    <script language="JavaScript">
        window.name="MyModalDialog";
    </script>
</head>
<body>
    <form id="form1" runat="server" target="MyModalDialog">
    <div>
        &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" />
        <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="确认返回" />
        <input id="Button5" type="button" value="关闭" onclick="if(confirm('确认放弃吗?')) window.close();" /><br />
        <asp:ListBox ID="ListBox1" runat="server" Rows="10" Width="300px"></asp:ListBox>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="删除选择" />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="全部删除" /></div>
    </form>
</body>
</html>

dropdownlist2.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_dropdownlist2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //添加
        string s;
        s = TextBox1.Text;
        ListBox1.Items.Add(new ListItem(s, s));
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //删除选择
        ListItem li = new ListItem();
        li = ListBox1.SelectedItem;
        if (li != null)
        {
            ListBox1.Items.Remove(li);
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        //删除全部
        ListBox1.Items.Clear();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        //确认返回
        //window.returnValue = ""; window.close();
        string text = "", value = "";
        foreach (ListItem li in ListBox1.Items)
        {
            text += li.Text + "|";
            value += li.Value + "|";
        }
        if (text.Length > 0)
        {
            text = text.Remove(text.Length - 1);
            value = value.Remove(value.Length - 1);
            text = text + "*" + value;
        }
        Response.Write("<script type='text/javascript'>window.returnValue = '" + text + "'; window.close();</script>");
    }
}

发表评论:

    昵称:
    密码:
    主页:
    标题:
Powered by Oblog.