我们受够了没有?DropDownList和ListBox使用js后出错
经常都会用到这种处理方式:在弹出窗口里修改数据,把数据返回,然后呢,在当前窗口的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里使用后清空
我给出我的两个页面的完整测试代码,在测试中,我测试了直接放在页面上的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>
         <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>");
    }
}