用户希望从一个列表中选择若干个数据,比如 注册的时候选择一个所在城市。
当数据量很大的时候,用下拉列表框就有些不方便。
这里提供了两种选择:新打开一个窗体。或者使用asp2.0实现的XMLHTTP方式。
首先,原始的有一种做法,就是在Page_load里面已经全部取出了数据,并且绑定到一个选择控件上,这是一种方式。这里讨论的两种方法都不是这样的顺序,而是在用户需要选择的时候,再次从数据库获取数据,并且保证原来的页面不刷新,保持原来的状态。
1:新打开一个页面 window.showModalDialog
这种方法是在用户决定从列表中选择的时候,界面弹出一个新的窗体,新窗体中列出数据,用户选择完成之后返回给原页面结果。
难点就是使用window.showModalDialogd来传递参数,下边是主页面使用的几个js
-----------------------code----------------------
<script language="javascript">
function GetParamFromNewPage(url,param,height,width,target_show,target_bind)
{
//打开一个新的页面,提供选择。
//url是新页面的名称,param是参数名称,该参数也同时通过url传递到页面上。
//target_show是显示选择的值,target_bind保存的是Id,不显示
var feature = "dialogWidth:"+width+"px;dialogHeight:"+height+"px;scroll:yes;status:no;help:no;center:1";
var returnTarget = window.showModalDialog(url+"?"+param, param, feature);
if(returnTarget != undefined && returnTarget.length > 1)
{
BindData(returnTarget,target_show,target_bind);
}
return false;
}
function BindData(source,show,hide)
{
//返回值是这样的:text1;value1;text2;value2
//所以分割,分别显示
var bind_array =source.split(";");
for (loop=0; loop < bind_array.length;loop++)
{
if(loop%2==0)
document.getElementById(show).value+=bind_array[loop]+";";
else
document.getElementById(hide).value+=bind_array[loop]+";";
}
}
--------------------------------------end---------------------------
这是引用这些js的控件
<INPUT onclick="GetParamFromNewPage('WebForm6.aspx','p=xx',300,300,'TextBox1','TextBox2')" type="button" value="open">
第二页接受参数,显示,取得选择值,返回结果
这是几个主要的js
---------------------code-------------------------------
<script language="javascript">
function exit(source)
{
//上页传过来的参数可以在这里获取(如下),也可以在Page_Load里面如此获取:Request.Params["p"];
//var param=window.dialogArguments;
window.returnValue =GetResult(source);
window.close();
}
function GetResult(source)
{
//选择完成之后返回,注意去掉最后一个 ';'
//格式:text1;value1;text2;value2
var len=document.getElementById(source).length;
var result='';
for(i=0;i<len;i++)
{
if(document.getElementById(source).options[i].selected)
{
result+=document.getElementById(source).options[i].text+";"+document.getElementById(source).options[i].value+";";
}
}
return result.substring(0,result.length-1);
}
</script>
------------------------------------------end-------------------------
这是引用js的控件,选择完成之后的
<INPUT type="button" onclick="exit('ListBox1')" value="ok">
这种方法下,需要两个页面
2:XMLHTTP方式。使用asp.net的新特性,异步回调
这一种方式下,只需要一个页面,只有用户需要的时候,程序才向服务器发送请求,并且看起来,页面并没有刷新,也就保持了原来的状态。
主要使用的js
----------------------------------------code------------------------
<script language="JavaScript">
function Hide(source,btn,target)
{
//source是列表框,btn是ok按钮,target是选择完成之后显示结果的文本框
//选择完成之后,隐藏掉这些按钮和显示框,并且把选择的数据提交给target显示
var len=document.getElementById(source).length;
for(i=0;i<len;i++)
{
if(document.getElementById(source).options[i].selected)
{
if(document.getElementById(target).value=="")
document.getElementById(target).value=document.getElementById(source).options[i].text;
else
document.getElementById(target).value=document.getElementById(target).value+";"+document.getElementById(source).options[i].text;
}
}
document.getElementById(source).style.display="none";
document.getElementById(btn).style.display="none";
}
function Show(param,source,btn)
{
//开始选择,异步调用到服务器,获取数据,然后显示在source中
//param是回调服务器代码的参数,source是显示结果的列表框,btn是ok按钮
//回调,并给出再次回调的函数
WebForm_DoCallback('__Page',param,onGotMessage,null,null,false);
//让数据源和ok按钮显示
document.getElementById(source).style.display="block";
document.getElementById(btn).style.display="block";
}
function onGotMessage (result, context)
{
//服务器代码完成之后的回调函数,用以真正显示数据
//result格式:text1;value1;text2;value2
var binder='ListBox1';//可惜,不知道怎么传参数过来
var bind_array =result.split(";");
for (loop=0; loop < bind_array.length;loop++)
{
var currentIndex=loop/2;
//无则加一个选项
if(document.getElementById(binder).length<=currentIndex)
document.getElementById(binder).add( new Option("default",'v_default'));
if(loop%2==0)
document.getElementById(binder).options[currentIndex].text=bind_array[loop];
else
document.getElementById(binder).options[currentIndex].value+=bind_array[loop];
}
}
</script>
------------------------------------end------------------------
使用这是js的控件
<INPUT onclick="Show('workstep_name=xxx','ListBox1','btn_ok')" type="button" value="pickup">
<INPUT id="btn_ok" onclick="Hide('ListBox1','btn_ok','TextBox1')" type="button" value="ok">
服务器段代码:一个是需要继承ICallbackEventHandler,还需要获取注册回调函数,然后就是回调函数的实现。
public partial class GetDataFromBack : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
//获取回调函数的注册
string cbref = ClientScript.GetCallbackEventReference(this,"workstep_name=xx", "onGotMessage", "null");
}
//异步调用的服务器代码
string ICallbackEventHandler.RaiseCallbackEvent(string arg)
{
//get data based on the arg from client
//change data to string , then send to client to show
string result = "aa;v_aa;bb;v_bb;cc;v_cc;dd;v_dd;";
return result;
}