数据库有2个表Contentsort和ContentType 结构如下

 

字段名

字段类型

sortid

int

typeid

int

sortname

Varchar(50)

remark

Varchar(100)

 

字段名

字段类型

typeid

int

typename

Varchar(50)

remark

Varchar(100)









其中typeid是ContentSort表的fk.

前台代码如下:

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

<!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>1</title>
     <SCRIPT LANGUAGE="JavaScript">
    <!--
    //以XML求取数据
    function XmlPost(obj)
    {
      var svalue = obj.value;
      //alert(svalue);
      if(svalue=="请选择")
      {
       document.all("Sortdrop").length=0;
      }
      else
      {
       var webFileUrl = "AddContent.aspx?TypeID=" + svalue;
       var result = "";
       var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
       xmlHttp.open("POST", webFileUrl, false);//使用XMLPOST方式
       xmlHttp.send("");//发送
       result = xmlHttp.responseText;//得到返回的结果
       
       if(result != "")
       {
         document.all("Sortdrop").length=0;//清空原来存在的
         var piArray = result.split(",");
         for(var i=0;i<piArray.length;i++)
         {
           var ary1 = piArray[i].toString().split("|");
        
           document.all("Sortdrop").options.add(new Option(ary1[0].toString(),ary1[1].toString()));
         }
       }
       else
       {
         alert(result);
       }
      }
    }
    //-->
   </SCRIPT>

<table class="table"  cellspacing="1" cellpadding="0" border="0" width="80%">
   <tr>
  <td height="25" width="10%" class="td_left">类别</td>
  <td class="td_right" align=left >
            <asp:DropDownList ID="Typedrop1" runat="server" Width="80px" >
            </asp:DropDownList></td>
  <td height="25" width="10%" class="td_left">分类</td>
  <td class="td_right" align=left >
           <asp:DropDownList ID="Sortdrop" runat="server" Width="80px">
            </asp:DropDownList></td>
  </tr>
</table>

后台cs

 protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
            RefurData();         
        }
    }

    public void RefurData()
    {
        string TypeID = Request.QueryString["TypeID"];//第一次请求结果为null
        if (TypeID + "Flag" != "Flag")//如果加Flag不等于Flag,表示Typedrop1改变过,触发过onchange事件
        {
            SortInfoBind(TypeID);
        }

        if (!this.IsPostBack)
        {
            gettypelist();
        }
    }

 

    private void gettypelist()
    {
        DataSet ds = db.Select(PREData.ContentType).ToDataSet();

        Typedrop1.DataSource = ds;
        Typedrop1.DataTextField = "TypeName";
        Typedrop1.DataValueField = "typeID";
        Typedrop1.DataBind();
        Typedrop1.Items.Insert(0, "请选择");
        Typedrop1.Items[0].Selected = true;

        Typedrop1.Attributes.Add("onchange", "XmlPost(this);");

    }

    //返回ddlInfo下拉框需要的值给xmlhttp
    private void SortInfoBind(string TID)
    {
        string Mystr = "";
        int TypeID = int.Parse(TID);
        DataSet ds= db.Select(PREData.ContentSort)
                   .Where(PREData.ContentSort.typeid == TID)
                   .ToDataSet(); //此处用了数据访问层的代码,如要运行,必须自己写方法
        DataTable MyTab = ds.Tables[0];

        if (MyTab.Rows.Count != 0)//判断是否有记录
        {
            for (int i = 0; i < MyTab.Rows.Count; i++)//循环DataTable里的记录
            {
                Mystr += "," + MyTab.Rows[i]["sortname"].ToString() + "|" + MyTab.Rows[i]["sortid"].ToString();
                //DataTable每一行都有编号和记录,使用,和|分隔,在XMLPOST中用到
            }
            Mystr = Mystr.Substring(1);
        }
        Response.Write(Mystr);//向HTTP输出内容流写入一个字符数组
        Response.End();//停止该页的执行
    }

posted on 2007-12-27 10:51  雄哥  阅读(465)  评论(0)    收藏  举报