当前位置:编程学习 > C#/ASP.NET >>

如何将模态窗口中listbox的text和value传给父窗口的textbox和listbox

如何将模态窗口中listbox的text和value传给父窗口的textbox和listbox

两天了 --------------------编程问答-------------------- 这个只能从js进行传递。或者放在服务器端的Cookie,Session里面进行重新刷新页面实现 --------------------编程问答-------------------- 高手有木有啊 。。 --------------------编程问答-------------------- 我用了js
就是传值的话只有listbox的value能传过去 对应的text是怎么传得,
而且传过去的值在textbox中不能显示

父页面:
    function studyShowModalDialog() {
             var m = window.showModalDialog("SelectStas.aspx?temp=" + Math.random(),"", "dialogHeight:300px,center:yes,resizable:yes,status:no");

             if (m != null) {
                 alert(m);
                 window.document.getElementById("MuliSignT").value = m;

          }

                 window.location.reload();
                
             }


子页面:
  function GetDataAndClose() {
        var objList = document.getElementById("lbStaffRight");
    var str = " ";
    var strs = "";
     for(var   i=0;i <objList.options.length;i++){
         str = str + objList.options[i].value;
      
      }
      window.returnValue = str;
        window.close();

    }
--------------------编程问答-------------------- 有人么 接下 --------------------编程问答-------------------- 方法很多,下面是一个完整的例子。你只需拷贝转贴进行进行测试即可
test.aspx

<%@ Page Language="C#" %>

<!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></title>
  <script type="text/javascript">
    function add(t, v) {
      ls = document.getElementById("ListBox1");
      st = t.split(",")
      sv = v.split(",")
      ls.options.length = 0;
      for (i = 0; i < st.length; i++) {
        ls.options[ls.options.length] = new Option(st[i], sv[i]);
      }
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="window.showModalDialog('b.aspx',window);return false" />
  </form>
</body>
</html>


B.aspx
<%@ Page Language="C#" %>

<!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></title>
  <script type="text/javascript">
    function SendTo() {
      ls = document.getElementById("ListBox1");
      st = []
      sv = [];
      for (i = 0; i < ls.options.length; i++) {
        st.push(ls.options[i].text);
        sv.push(ls.options[i].value);
      }
      window.dialogArguments.add(st.join(","), sv.join(","))
      window.close();
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  
  <asp:ListBox ID="ListBox1" runat="server">
  <asp:ListItem>A</asp:ListItem>
   <asp:ListItem>B</asp:ListItem>
    <asp:ListItem>c</asp:ListItem>
  </asp:ListBox>
  
  <asp:Button ID="Button1" runat="server" Text="传递" OnClientClick="SendTo();return false" />
  </form>
</body>
</html>
--------------------编程问答-------------------- objList.options[i].text
就是text内容 --------------------编程问答-------------------- 子页面:
 if (tvCorp.SelectedNode.Value.Length > 1)
            {
                string pk = tvCorp.SelectedNode.Value;
                string name = tvCorp.SelectedNode.Text;
                string ary = pk + ":" + name;
               
                Response.Write("<script language=javascript>");
                Response.Write("window.returnValue='" + ary.ToString() + "';");
                Response.Write("window.close();");
                Response.Write("</script>");
            }
            else
            {
                Frame.Window w = new Window();
                w.Alert("不能选择此部门!", this);
            }

主页面:
//传递两个TB控件名,Corp公司PK,Dept部门
function JSPerson_TB_getAllDept(tb1,tb2)
{
    var result;
      result = window.showModalDialog("/Bussiness/UC/GetAllDept.aspx?Corp="+Corp+"&Dept=1",'','dialogHeight:600px; dialogWidth:330px; center: yes; help: no;status:no');
if (result != 'undefined' && typeof(result)!='undefined')
{
var rv = result.split(":");
if (rv.length == 2);
{
    document.getElementById(tb1).value =rv[0];
document.getElementById(tb2).value =rv[1];
}
}   
}

--------------------编程问答-------------------- window.showModalDialog和window.open关闭子页面时刷新父页面 --------------------编程问答-------------------- 方法2

<%@ Page Language="C#" %>

<!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></title>
  <script type="text/javascript">
    function add() {
      a = window.showModalDialog('b.aspx')
      t = a.split("|")[0];
      v = a.split("|")[1];
      ls = document.getElementById("ListBox1");
      st = t.split(",")
      sv = v.split(",")
      ls.options.length = 0;
      for (i = 0; i < st.length; i++) {
        ls.options[ls.options.length] = new Option(st[i], sv[i]);
      }
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="add();return false;" />
  </form>
</body>
</html>


b.aspx
<%@ Page Language="C#" %>

<!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></title>
  <script type="text/javascript">
    function SendTo() {
      ls = document.getElementById("ListBox1");
      st = []
      sv = [];
      for (i = 0; i < ls.options.length; i++) {
        st.push(ls.options[i].text);
        sv.push(ls.options[i].value);
      }
      window.returnValue = st.join(",") + "|" + sv.join(",");
      window.close();
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem>A</asp:ListItem>
    <asp:ListItem>B</asp:ListItem>
    <asp:ListItem>c</asp:ListItem>
  </asp:ListBox>
  <asp:Button ID="Button1" runat="server" Text="传递" OnClientClick="SendTo();return false" />
  </form>
</body>
</html>
--------------------编程问答-------------------- 更新记录后关闭子窗口并刷新父窗口的Javascript  --------------------编程问答-------------------- window.showModalDialog和window.open关闭子页面时刷新父页面

--------------------编程问答--------------------
引用 5 楼 net_lover 的回复:
方法很多,下面是一个完整的例子。你只需拷贝转贴进行进行测试即可
test.aspx

<%@ Page Language="C#" %>

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



ls.options.length = 0; 这个缺少对象

问下 add(t, v)是在哪被调用的
--------------------编程问答-------------------- ls.options.length = 0; 这个缺少对象

你的listbox的客户端id不对

ls = document.getElementById("<%=ListBox1.ClientID%>");


add(t, v)是在哪被调用的
在b.aspx里面啊
window.dialogArguments.add(st.join(","), sv.join(","))

这是第一种方法里面的使用方法 --------------------编程问答-------------------- 还是红钻厉害 --------------------编程问答--------------------
引用 13 楼 net_lover 的回复:
ls.options.length = 0; 这个缺少对象

你的listbox的客户端id不对

ls = document.getElementById("<%=ListBox1.ClientID%>");


add(t, v)是在哪被调用的
在b.aspx里面啊
window.dialogArguments.add(st.join(","), sv.join(","))
……

listbox的id我改了
调试的时候显示ls.options.length = 0; 运行错误 缺少对象
--------------------编程问答-------------------- 现在
主页面:
    <script type="text/javascript" language="javascript">

         function add() {
             a = window.showModalDialog("SelectStas.aspx?temp=" + Math.random(), "", "dialogHeight:300px,center:yes,resizable:yes,status:no")
             t = a.split("|")[0];
             v = a.split("|")[1];
             ls = document.getElementById("GetData");
             st = t.split(",")
             sv = v.split(",")
             ls.options.length = 0;
             for (i = 0; i < st.length; i++) {
                 ls.options[ls.options.length] = new Option(st[i], sv[i]);
             }
         }

     
  <asp:listbox id="GetData" runat="server" Rows="10" SelectionMode="Multiple" Width="200"></asp:listbox>
   <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="add()();return false;"  />

         
    </script>   


子页面:
  function SendTo() {
         ls = document.getElementById("lbStaffRight");
  st = []
  sv = [];
  for (i = 0; i < ls.options.length; i++) {
  st.push(ls.options[i].text);
  sv.push(ls.options[i].value);
  }
  window.returnValue = st.join(",") + "|" + sv.join(",");
  window.close();
  }

  <asp:Button ID="Button1" runat="server" Text="传递" OnClientClick="SendTo();return false" />
    

就是显示那个缺少对象 --------------------编程问答-------------------- 你的子页面里面有
<asp:ListBox Id="lbStaffRight" 吗? --------------------编程问答--------------------
引用 17 楼 net_lover 的回复:
你的子页面里面有
<asp:ListBox Id="lbStaffRight" 吗?

有啊 我用alert可以查看到返回值
就是getdata这个listbox不显示值
--------------------编程问答-------------------- 我的例子都给你写的很清楚了。

难道计算机会撒谎吗?要是真的有还能报告缺少对象??

另外你的
 <asp:listbox id="GetData" runat="server" Rows="10" SelectionMode="Multiple" Width="200"></asp:listbox>
   <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="add()();return false;"  />

         
    </script>  

你的代码放在</script> 里面?



 OnClientClick="add()();return false;"

有这样调用函数的吗?


拷贝我的例子直接进行测试,就知道是否有错误了,
如果我的例子有错,那是我的问题,如果我的例子没错,你的代码出错,那你应该去对照你自己的代码哪里出问题了。 --------------------编程问答-------------------- add()刚才改了。好像是javascript调用的时候id会变了。刚才改了下 现在在调
--------------------编程问答-------------------- 传到父页面listbox中的值是怎么取的啊
为什么this.listbox1.items.count取不到值啊 --------------------编程问答--------------------
引用 19 楼 net_lover 的回复:
我的例子都给你写的很清楚了。

难道计算机会撒谎吗?要是真的有还能报告缺少对象??

另外你的
 <asp:listbox id="GetData" runat="server" Rows="10" SelectionMode="Multiple" Width="200"></asp:listbox>
  <asp:Button ID="Button1" runat="server" ……

传到父页面listbox中的值是怎么取的啊
为什么this.listbox1.items.count取不到值啊 --------------------编程问答-------------------- 为什么this.listbox1.items.count取不到值啊?
js和ajax添加的选项,是不能使用this.listbox1.items.count进行获取的。
只能在客户端进行获取。

包括listbox1.SelectedValue等都是不能使用的,
你需要使用
Request.Form["listbox1"]得到选中的

要得到this.listbox1.items.count,你需要在客户端计算传到服务器端 --------------------编程问答--------------------
引用 23 楼 net_lover 的回复:
为什么this.listbox1.items.count取不到值啊?
js和ajax添加的选项,是不能使用this.listbox1.items.count进行获取的。
只能在客户端进行获取。

包括listbox1.SelectedValue等都是不能使用的,
你需要使用
Request.Form["listbox1"]得到选中的

要得到this.listbox1.items.……

那如果我想使用子页面上listbox的值要怎么用啊?
比如主页面的后台要使用子页面的listbox。value
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,