小知识(八)
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" 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" Css 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("编辑失败!"); } } }); }); });});