jquery ajax 后台绑定select

来源:互联网 发布:上海闵行区网络新村 编辑:程序博客网 时间:2024/05/17 18:18

      var mask=new WindowMask(); //万能全屏遮盖使用 可以看我的http://blog.csdn.net/yefighter/article/details/8479535

       mask.show("数据处理中");
        enviSelect.html('');
        enviSelect.append("<option value='-1' selected='selected'>请选择</option>");

 

       //可以优先使用 $.getJSON这个方法
        $.post("FrmTestCaseCopyEnviroment.aspx", { isAjaxRequest: "2"},
                function(data) {

               //如果优先使用了 $.getJSON

                那么就不需要这个了
                   var jsonObj = $.parseJSON(data);
                //也可以用jquery的each函数 

                  for (i=0;i<jsonObj.length;i++) {
                      enviSelect.append("<option value=\"" + jsonObj[i].value + "\" >" + jsonObj[i].text + "</option>") ;
                 }
                 mask.hide();
          });

.net 后台

 Response.Clear();

DataTable dtCurrEnviroment;

//从数据库 也可以是缓存中获取数据

Hashtable htCond = new Hashtable();
htCond["TEST_OBJECT_ID"] = this.SessionInfo.CurrentTestObjectId.ToString();
 htCond["ORG_ID"] = this.SessionInfo.CurrentOrganizationId.ToString();
dtCurrEnviroment = mIOtTestEnvironmentService.SeleteTestEnvironment(htCond);

//拼接json数据 方法在下面

string OutPut = DataTableJson(dtCurrEnviroment);

//输出json数据
Response.Write(OutPut);

Response.End();

 

public string DataTableJson(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("[");
        foreach (DataRow dr in dt.Rows)
        {
            jsonBuilder.Append("{");
            jsonBuilder.Append("\"");
            jsonBuilder.Append("text");
            jsonBuilder.Append("\":\"");
            jsonBuilder.Append(dr["TEST_ENVIRONMENT_NO"] + " " + dr["TEST_ENVIRONMENT_NAME"]);
            jsonBuilder.Append("\",");

            jsonBuilder.Append("\"");
            jsonBuilder.Append("value");
            jsonBuilder.Append("\":\"");
            jsonBuilder.Append(dr["SERIAL_ID"]);
            jsonBuilder.Append("\"");
            jsonBuilder.Append("},");
        }
        if (jsonBuilder.Length != 0)
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        return jsonBuilder.ToString();
    }

 

原创粉丝点击