2018-04-09
2018-04-09 本文已影响23人
vlsee
分析原函数
public void getWellGridView1Row()
{
//存储各个对象的信息
ArrayList WellNews = new ArrayList();
//循环获取单个油井信息
//从数据库中读取Infor_Map()所需参数
foreach (GridViewRow row in GridView1.Rows)
{
string WellNum = row.Cells[1].Text.Trim().ToString();
string Section = row.Cells[2].Text.Trim().ToString();
string BuildData = row.Cells[3].Text.Trim().ToString();
string Principal = row.Cells[4].Text.Trim().ToString();
string Position = row.Cells[5].Text.Trim().ToString();
string aWellNews = WellNum + ";" + Position + ";" + Section + ";" + Principal + ";" + BuildData;
WellNews.Add(aWellNews);
}
Array WellNewsArray = WellNews.ToArray();
ArrayList aWellNum = new ArrayList();
ArrayList aPosition = new ArrayList();
ArrayList aSection = new ArrayList();
ArrayList aPrincipal = new ArrayList();
ArrayList aBuildData = new ArrayList();
foreach (string aWellNewsArray in WellNewsArray)
{
string[] aWellNewsString = aWellNewsArray.Split(';');
aWellNum.Add(aWellNewsString[0]);
aPosition.Add(aWellNewsString[1]);
aSection.Add(aWellNewsString[2]);
aPrincipal.Add(aWellNewsString[3]);
aBuildData.Add(aWellNewsString[4]);
}
string aWellNums = string.Join(";", (string[])aWellNum.ToArray(typeof(string)));
string aPositions = string.Join(";", (string[])aPosition.ToArray(typeof(string)));
string aSections = string.Join(";", (string[])aSection.ToArray(typeof(string)));
string aPrincipals = string.Join(";", (string[])aPrincipal.ToArray(typeof(string)));
string aBuildDatas = string.Join(";", (string[])aBuildData.ToArray(typeof(string)));
//将调用Infor_Map()的js代码添加到前端页面中
StringBuilder Sb1 = new StringBuilder();
Sb1.Append("<script language='javascript'>");
Sb1.Append("InFor_Map('" + aWellNums + "','" + aPositions + "','" + aSections + "','" + aPrincipals + "','" + aBuildDatas + "');");
Sb1.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", Sb1.ToString());
}
后端代码中有前端函数的执行,所以我觉得如果如果你要合并前端函数,后端代码也需要修改。
类似getWellGridView1Row()的函数其大致逻辑为
- 取得数据
- 处理数据
- 将处理结果作为前端函数的参数传出
将getWellGridView1Row()中取得的数据传到前台,以便js接收
后台代码
//keyValue-后台中以字典方式存储参数数据的全局变量
public System.Collections.Generic.Dictionary<String, String> keyValue = new Dictionary<string, string>();
//dataJson-存储json格式的keyValue,前台js取得该变量便也就取得了所需参数
public string dataJson;
protected void Page_Load(object sender, EventArgs e)
{//默认页面处理函数,理解为后台程序入口
getWellGridView1Row();//执行类似函数,将所需参数数据存入keyValue
//以下两行为keyValue的json化
JavaScriptSerializer serializer = new JavaScriptSerializer();
dataJson = serializer.Serialize(keyValue);
Response.Write(dataJson);//可删除,将dataJson打印到页面顶端
}
public void getWellGridView1Row()
{
//模拟从数据库中取得数据并处理为可用参数
String aWellNums = "aWellNums";
String aPositions = "aPositions";
String aSections = "aSections";
//将所需参数加入到keyValue
keyValue.Add("aWellNums", aWellNums);
keyValue.Add("aPositions", aPositions);
keyValue.Add("aSections", aSections);
}
主要逻辑为

前端代码
<script>
var dataJson = <%=dataJson %>;//接收dataJson
alert(dataJson.aWellNums);//可以直接这样很方便地取得参数值
//后台的dataJson是一个json格式的sring,而前台接收到的dataJson并非一个json格式的string,
//而是一个js的object,实在想不通所以下面的代码是不需要的只是为了搞清楚这个问题,不过还是没搞清楚
var test = { "width": "100", "height": "200" };
console.log(test)
test = '{ "width": "100", "height": "200" }';
console.log(test);
console.log(JSON.parse(test));
</script>