1.jQuery DatatableToJson
http://dotnet.aspx.cc/file/jQuery-Receive-ashx-DataSet-JSON.aspx 稍微修改一下
public static string DataTable2Json(DataTable dt)
{
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.Append("{");
jsonBuilder.Append(dt.TableName); //这里TableName设为了provider
jsonBuilder.Append(":[");
for (int i = 0; i < dt.Rows.Count; i++)
{
jsonBuilder.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
jsonBuilder.Append(dt.Columns[j].ColumnName);
jsonBuilder.Append(":'");
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append("',");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
}
jsonBuilder最终格式为 {provider:[{ID:'1',GYSMC:'12',GSXZ:'12',LXDH:'12',LXR:'12',GSJJ:'12'},{ID:'2',GYSMC:'12',GSXZ:'13',LXDH:'13',LXR:'13',GSJJ:'3'}]}
前台JsonToTable
$.getJSON("searchProvider.ashx", {providername:providerName}, function(data) {
$.each(data.provider, function(i, data) {
var tr = $("<tr>");
$("<td style='class:providerID'>").text(data.ID).appendTo(tr);
$("<td>").text(data.GYSMC).appendTo(tr);
$("<td>").text(data.GSXZ).appendTo(tr);
$("<td>").text(data.LXDH).appendTo(tr);
$("<td>").text(data.LXR).appendTo(tr);
$("<td>").text(data.GSJJ).appendTo(tr);
tr.appendTo($("#tableProvider"));
})
})
<div id="divSearchProvider">
<table id="tableProvider" class="tableUI">
<tr><th>公司编码</th><th>公司名称</th><th>公司性质</th><th>电话</th><th>联系人</th><th>公司简介</th></tr>
</table>
</div>
LINQ实现
string jsonxscj="{scores:[";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/JSON";
string sid = context.Request.QueryString["thesid"];
using (GAOEDataContext db = new GAOEDataContext())
{
var scores = from s in db.Score join p in db.Paper on s.PaperId equals p.ID where s.Sid.ToString().Equals(sid) select new { papername = p.PaperName, thescore = s.TheScore };
foreach (var item in scores)
{
jsonxscj += "{pn:'"+item.papername+"',sc:'"+item.thescore.ToString()+"'},";
}
jsonxscj=jsonxscj.Substring(0, jsonxscj.LastIndexOf(","));
jsonxscj += "]}";
}
string ss = jsonxscj;
context.Response.Write(jsonxscj);
}
{scores:[{pn:'C#面试题(一)',sc:'70'},{pn:'C#面试题(二)',sc:'36'},{pn:'C#面试题(三)',sc:'35'}]}
如果没有成功,可考虑一下jQuery版本的问题,jQuery各个版本之间的不兼容真是让人恼火。
2.在有许多层iframe的情况下的js刷新某一页
alert(window.parent.location)
alert(window.parent.frames["MainIframe"].location);
alert(window.parent.frames["MainIframe"].frames["content"].location);
可以一个一个查看其地址,直到找到想要刷新的页,然后
window.parent.frames["MainIframe"].frames["content"].location.reload();
3. 从一个iframe中获取另一个iframe中的某元素值&调用js函数
window.parent.frames["provider_main"].document.getElementById("hid_providerid").value
provider_main是另一个iframe的id 只要找到相应的iframe就可以了
同理从一个iframe调用另一个iframe的js函数 也是一样
window.parent.frames["provider_main"].myPrint();
4.使用jQuery的get方法的中文乱码问题解决
①前台encodeURI编码 {1:encodeURI(1),2:encodeURI(2)} 后台解码context.Server.UrlDecode(context.Request.QueryString["1"]);
不行的话可以如下
②前台escape编码 {1:escape(1),2:escape(2)} 后台解码 context.Server.UrlDecode(context.Request.QueryString["1"]);
用post方法访问oracle数据库时有可能报错 : 并非所有变量都关联 当然用get方法如果有些数值为null也会报同样错误
5.JS打印
js自带函数打印,这里打印的是哪个页面取决于打印按钮在哪页,而不管打印函数在哪页
<a href="javascript:window.print()">打印</a>
<input type="button" value="打印" onclick="window.print()"/>
局部打印 http://www.cnblogs.com/samlin/archive/2008/04/13/1151265.html
其他 http://blog.csdn.net/coffeeroom/article/details/6609119
6.记事本 .LOG
在记事本的第一行写上 .LOG ,这样今后打开记事本文件就知道上次最后的打开时间了。
7.jQuery ajax 报"缺少对象"的错误的一个可能原因
jQuery ajax 数据用json方式data:{1:1,2:2}的话 如果数据过多可能会报"缺少对象"的错误[而且json方式传值需
要涉及到escape()中文加解码的问题(如上4)]
推荐用写在url后面的方式?1=1&2=2
8.GridView可编辑表格
<asp:GridView ID="grwBGSP" runat="server" CssClass="tableUI" AutoGenerateColumns="false" onrowdatabound="grwBGSP_RowDataBound"> <Columns> <asp:BoundField HeaderText="商品编码" FooterText="SPBM" DataField="SPBM" /> <asp:BoundField HeaderText="商品名称" FooterText="SPMC" DataField="SPMC" /> <asp:BoundField HeaderText="商品型号" FooterText="SPXH" DataField="SPXH" /> <asp:BoundField HeaderText="供应商" FooterText="GYS" DataField="GYS" /> <asp:BoundField HeaderText="单位" FooterText="DW" DataField="DW" /> <asp:BoundField HeaderText="采购数量" FooterText="CGSL" DataField="CGSL" /> <asp:BoundField HeaderText="本次价格" FooterText="BCJG" DataField="BCJG" /> <asp:BoundField HeaderText="备注" FooterText="BZH" DataField="BZH" /> </Columns> </asp:GridView>
protected void grwBGSP_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = (DataTable)grwBGSP.DataSource; string spbm = dt.Rows[e.Row.RowIndex]["spbm"].ToString(); for (int i = 1; i < e.Row.Cells.Count; i++) { //i从1开始,在js里的响应就是第1列不可编辑 e.Row.Cells[i].ToolTip = grwBGSP.Columns[i].FooterText; e.Row.Cells[i].Attributes.Add("spbm",spbm); } } }
var oldtxt = null;
var newtxt = null; $(function() { var qdid=document.getElementById("hid_qdid").value; var jdid=document.getElementById("hid_jdid").value; $("#grwBGSP tr td").dblclick(function() { var objTD = $(this); oldtxt = objTD.text(); var spbm = this.spbm; var colName = this.title; if (spbm == undefined || colName == undefined || spbm.length == 0 || colName.length == 0) { return; } var input = $("<input type='text' value='" + oldtxt + "' style='width:80px;'>"); objTD.html(input); input.click(function() { return false; }); input.trigger("focus").trigger("select"); input.blur(function() { var inputCtl = $(this).val(); // var json = "{action:'bianji',jdid:'"+jdid+"',qdid:'"+qdid+"',spbm:'" + spbm + "',colName:'" + colName + "',colValue:'" + inputCtl.val() + "'}"; $.ajax({ type: "get", url: "../../PurchaseChange/PurchaseChangeHandle.ashx", data:{action:"bianji",jdid:jdid,qdid:qdid,spbm:escape(spbm),colName:escape(colName),colValue:escape(inputCtl)}, success: function(msg) { if (msg== "保存成功") { newtxt = inputCtl; objTD.html(newtxt); } else { alert("编辑失败!"); } } }); }); }); });