ICallbackEventHandler接口实现多级联动

来源:互联网 发布:qq自动留言软件 编辑:程序博客网 时间:2024/06/09 23:05

从一位老兄的Blog上看到了这个,可是代码却不太全(至少我这里看不全)。于是想办法补充了一点:

1、客户端脚本:

 function CallServer()
 {
  var product = "测试";
  <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
 }
 
 function ReceiveServerData(rValue)
 {
  alert(rValue);
  window.location.reload();
  var tt=document.getElementById("TextBox1");
  alert(tt.DataSource);
 }
 
 function selectChange(e)
 {
  for(var i=0;i<e.options.length;i++)
  {
   if(e.options[i].selected==true)
   {
    selectSon(e.options[i].value); break;
   }
  }
 }
 
 function selectSon(e)
 {
  <%= ClientScript.GetCallbackEventReference(this, "e", "ReceiveServerData",null)%>;
 }
 
 function ReceiveServerData(rValue)
 {
  var son=form1.elements["SelSon"];
  var s=rValue.split("|");
  son.length=s.length;
  for(var i=0;i<s.length-1;i++)
  {
   son.options[i+1]=new Option(s[i],s[i]);
  }
 }
 /////////////////////
 function selectChangeSon(e)
 {
  for(var i=0;i<e.options.length;i++)
  {
   if(e.options[i].selected==true)
   {
    select(e.options[i].value); break;
   }
  }
 }
 
 function select(e)
 {
  <%= ClientScript.GetCallbackEventReference(this, "e", "hello",null)%>;
 }
 
 function hello(rValue)
 {
  var child=form1.elements["Sel"];
  var s=rValue.split("|");
  child.length=s.length;
  for(var i=0;i<s.length-1;i++)
  {
   child.options[i+1]=new Option(s[i],s[i]);
  }
 }
</script>

2、aspx页面代码

  <select id="SelParent" runat="server" onchange="selectChange(this)">
   <option selected="selected" value="0">请选择 </option>
  </select>
  <br />
  <select id="SelSon" runat="server" onchange="selectChangeSon(this)">
   <option selected="selected" value="0">请选择 </option>
  </select>
  <br />
  <select id="Sel">
   <option selected="selected" value="0">请选择</option>
  </select>

3、服务器端代码

    OracleConnection conn = new OracleConnection("Data Source=?;UID=?;PWD=?");

    protected void Page_Load(object sender, EventArgs e)

    {

        ParentBind();

    }

    private string resualt;

    private void ParentBind()

    {

        string str = "select distinct(CID) from multimenu order by cid";

        DataSet ds = ExecuteSql4Ds(str);

        SelParent.DataSource = ds;

        SelParent.DataTextField = "cid";

        SelParent.DataBind();

    }

    private string SonBind(string e)//绑定第二级

    {

        string str = "select distinct(CCID) from multimenu where CID=" + e;

        DataSet ds = ExecuteSql4Ds(str);

        string s = "";

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            s += ds.Tables[0].Rows[i][0] + "|";

        return s;

    }

 

    private string ChildBind(string e)//绑定第三级

    {

        string str = "select distinct(CCCID) from multimenu where CCID=" + e;

        DataSet ds = ExecuteSql4Ds(str);

        string s = "";

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            s += ds.Tables[0].Rows[i][0] + "|";

        return s;

    }

 

    private DataSet ExecuteSql4Ds(string str)

    {

        OracleCommand cmd = new OracleCommand(str, conn);

        OracleDataAdapter da = new OracleDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

 

        return ds;

    }

    void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)

    {

        if (eventArgument.Length == 1)//绑定第二级,我的数据库里分别是用1,11,111来代表三级,所以长度为一的就是一级,大家可以根据自己的需要把修改

        {

            string eventArgument1 = SonBind(eventArgument);

            resualt = eventArgument1;

        }

        else

        {

            string eventArgument2 = ChildBind(eventArgument);

            resualt = eventArgument2;

        }

    }

    string ICallbackEventHandler.GetCallbackResult()

    {

        return resualt;

    }

 

 4、数据库脚本

create table MULTIMENU
(
  ID    NUMBER(2) not null,
  CID   NUMBER(2) not null,
  DSCT  VARCHAR2(30) not null,
  CCID  NUMBER(2),
  CCCID NUMBER(3)
)

原创粉丝点击