【问题标题】:populate multiple dropdown lists from ajax call从 ajax 调用填充多个下拉列表
【发布时间】:2012-07-12 17:25:11
【问题描述】:

我有一个包含 +- 50 行的 html 表(从数据库生成),每一行都有三个唯一的下拉列表(每个下拉选项都来自数据库中的不同表)

如何使用 ajax 填充它们。我知道如何填充和构建一个,但您能否就多的概念提出建议。

我是否使用一个 ajax 调用帖子到 handler.ashx,它使用一个 sql 语句检索所有三个表数据,然后将其放入一个 json 对象 - 然后 jquery 从那里构建它?

还是我使用三个单独的 ajax 调用和三个 sql 语句?但我认为这是不可能的,因为在循环中,ajax 不会等待数据被获取?

您能否就最佳解决方案或教程或想法/概念提出建议?

【问题讨论】:

  • 在填充 html 表(即在同一个数据库调用中)的同时,您是否无法填充下拉列表?

标签: c# javascript jquery sql vb.net


【解决方案1】:

我同意 markpsmith,您应该可能尝试在服务器端填充下拉列表,但如果您真的想在客户端填充它们,这里有一个示例说明如何用 AJAX 完成。请记住,该示例并未注册事件以进行验证。这是您必须解决的完全不同的问题...

页面代码

<%-- 
c2012 Shawn Eary cc-wiki - Load DropDowns via AJAX Demo 

Shows how to use client script to load ASP AJAX DropDownLists using
AJAX and jQuery.  This ASP.NET file does not use any server side
code other than the code that is contained in the GetDropDownData
webservice.  All population is done on the client side. 

References (Places I Got Help From) - 
[1] - Unknown. 
Options to List Box in Client Side JavaScript." 
plus2net. 2010. 
http://www.plus2net.com/javascript_tutorial/list-adding.php 
(accessed AUG 31, 2012).

[2] - Ward, David. 
"Using jQuery to Directly Call ASP.NET AJAX Page Methods." 
Encosia. MAR 10, 2012. 
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ 
(accessed AUG 31, 2012).
--%>
<%@ Page Language="C#" AutoEventWireup="true" %>

<!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>
   c2012 Shawn Eary cc-wiki - Load DropDowns via AJAX Demo
   </title>
   <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js" type="text/javascript"></script> 
</head>
<body>  
   <form id="form1" runat="server">    
      <%-- A table with three columns and several rows of non
           populated DropDownLists --%>
      <table>
         <tr>
            <td><asp:DropDownList CssClass="Drop1" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop2" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop3" runat="server"></asp:DropDownList></td>
         </tr>                  
         <tr>
            <td><asp:DropDownList CssClass="Drop1" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop2" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop3" runat="server"></asp:DropDownList></td>
         </tr>                  
         <tr>
            <td><asp:DropDownList CssClass="Drop1" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop2" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop3" runat="server"></asp:DropDownList></td>
         </tr>                  
         <tr>
            <td><asp:DropDownList CssClass="Drop1" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop2" runat="server"></asp:DropDownList></td>
            <td><asp:DropDownList CssClass="Drop3" runat="server"></asp:DropDownList></td>
         </tr>                  
      </table>    
   </form>


   <script type="text/ecmascript">
      function setColumnDropDownData(dataSource, columnNum) {
         for (var i = 0; i < dataSource.length; i++) {
            var curItem = dataSource[i];
            var text = curItem.text;
            var value = curItem.value;
            var theDropDowns = $('.Drop' + columnNum);
            for (var j = 0; j < theDropDowns.length; j++) {
               var curDropDown = theDropDowns[j]; 

               // Populate the DropDown as in [1]
               var someOption = document.createElement("OPTION");
               someOption.text = text; 
               someOption.value = value;
               curDropDown.options.add(someOption);               
            }            
         }
      }

      <%-- Mr. Ward shows how to call a WebMethod without using 
           AJAX ScriptManager in [2] --%>
      $.ajax({
         type: "POST",
         url: "testServices.asmx/GetDropDownData",
         data: "{tableName: 'color'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (msg) {            
            setColumnDropDownData(msg.d, 1);
         }
      });

      $.ajax({
         type: "POST",
         url: "testServices.asmx/GetDropDownData",
         data: "{tableName: 'shape'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (msg) {
            setColumnDropDownData(msg.d, 2);
         }
      });

      $.ajax({
         type: "POST",
         url: "testServices.asmx/GetDropDownData",
         data: "{tableName: 'size'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (msg) {
            setColumnDropDownData(msg.d, 3);
         }
      });      
   </script>
</body>
</html>

服务代码

// c2012 Shawn Eary cc-wiki - Load DropDowns via AJAX Demo 
// 
// Shows how to use a WebMethod to selectively push possible 
// DropDownList population data back to the client 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Container for the data items that will be pushed back to the 
/// client via JSON
/// </summary>
public class selectOption
{
   public selectOption(int iValue, string iText)
   {
      value = iValue; 
      text = iText; 
   }
   public int value; 
   public string text; 
}


/// <summary>
/// Summary description for testServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class testServices : System.Web.Services.WebService {

   // Send Data back to the client based upon the table of 
   // information she/he wants.  In real life, each of the different
   // if branches could be replaced with calls to the SQL database
   // to get the data but putting these calls here doesn't 
   // significantly enhance the purpose of this example
   [WebMethod]
   public List<selectOption> GetDropDownData(string tableName) {                  
      List<selectOption> returnVal = new List<selectOption>();

      if (tableName == "color") { 
         // Normally you would use Entity Framework to get information
         // from your T-SQL database here, but it isn't necessary
         // for me to show the database calls for this illustration
         returnVal.Add(new selectOption(1, "blue"));
         returnVal.Add(new selectOption(2, "yellow"));
         returnVal.Add(new selectOption(3, "green"));
      }
      else if (tableName == "shape")
      {
         // Normally you would use Entity Framework to get information
         // from your T-SQL database here, but it isn't necessary
         // for me to show the database calls for this illustration
         returnVal.Add(new selectOption(1, "square"));
         returnVal.Add(new selectOption(2, "triangle"));
         returnVal.Add(new selectOption(3, "oval"));
         returnVal.Add(new selectOption(4, "circle"));
      }
      else if (tableName == "size")
      {
         // Normally you would use Entity Framework to get information
         // from your T-SQL database here, but it isn't necessary
         // for me to show the database calls for this illustration
         returnVal.Add(new selectOption(1, "big"));
         returnVal.Add(new selectOption(2, "small"));         
      }

      return returnVal;
   }    
}

结果截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多