• ASP.NET中实现无刷新级联


    最近在帮学校做一个设备报修系统,由于水平有限,以前做项目的时候很少用到ajax方面的知识,为了实现更好的效果,我查阅了相关资料,参考网上教程写了我下面的程序,不足之处,请多多指正。

    开发环境配置为:Visual Studio 2008 Sql  Server 2008

    源码下载地址:无刷新级联

    1、建立测试数据库

    用到两个表,一个所在部门表 Department,一个使用部门表 UseDepart

    表:Department

    image

    表:UseDepart

    image

    2、新建一个MyClass类

      public class MyClass
      {
          public DataSet GetList(string table, string where)
          {
              string connStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
              using (SqlConnection conn = new SqlConnection(connStr))
              {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select * from ").Append(table);
                  if (!string.IsNullOrEmpty(where))
                  {
                      strSql.Append(" where ").Append(where);
                  }
                  using (SqlCommand cmd = new SqlCommand(strSql.ToString(), conn))
                  {
                      conn.Open();
                      DataSet ds = new DataSet();
                      SqlDataAdapter da = new SqlDataAdapter();
                      da.SelectCommand = cmd;
                      da.Fill(ds, "ds");
                      return ds;
                  }
              }
          }
      }

    3、Default.aspx页面代码JavaScript代码

        <script type="text/javascript">
            $(function() {
                $("#<%=ddlDepartment.ClientID %>").change(
                function() {
                    $("#ddlUseDepart").load("LoadUseDepart.aspx?InDepartId=" + $("#" + "<%= ddlDepartment.ClientID %>" + " option:selected").val());
                }
                );
            });
        </script>

    4、LoadUseDepart.aspx后台代码

            protected void Page_Load(object sender, EventArgs e)
            {
                int id;
                if (int.TryParse(Request.Params["InDepartId"].ToString(), out id))
                {
                    Response.Write(GetUseDepart(id));
                    Response.End();
                }
            }
    
            private string GetUseDepart(int id)
            {
                StringBuilder sb = new StringBuilder();
                DataSet ds = new MyClass().GetList("UseDepart", "DepartId=" + id);
                if (ds.Tables.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        sb.Append("<option value='" + ds.Tables[0].Rows[i]["Id"].ToString() + "'>" + ds.Tables[0].Rows[i]["Name"].ToString() + "</option>");
                    }
                }
                return sb.ToString();
            }
  • 相关阅读:
    2.7连接数据库中遇见的相应问题1
    linux bash中too many arguments问题的解决方法
    linux系统补丁更新 yum命令
    安装node,linux升级gcc
    python-导出Jenkins任务
    升级openssl和openssh版本
    linux修改文件所属的用户组以及用户
    linux的Umask 为022 和027 都是什么意思?
    keepalived
    自己编写k8s
  • 原文地址:https://www.cnblogs.com/nianming/p/2078538.html
Copyright © 2020-2023  润新知