1、 table 每行 鼠标悬停和移出的样式变化: <tr class="line-odd" onmouseover="currentcolor = this.style.backgroundColor;this.style.backgroundColor='#F7FFF7';this.style.cursor='hand';" onmouseout="this.style.backgroundColor = currentcolor;"> <td>二号楼</td> <td></td> </tr> 2、由1,我们可以对gridview行 鼠标悬停和移出的样式变化: protected void gvBuildList_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { try { e.Row.Attributes.Add("onmouseover", "currentcolor = this.style.backgroundColor;this.style.backgroundColor='#F7FFF7';this.style.cursor='hand';"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = currentcolor;"); } catch { } } } 3 xml文件,删除节点的方法: DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("XML/BuildList.xml")); bool isHave = false; foreach (DataRow drTemp in ds.Tables[0].Rows) { if (drTemp["BuildText"].ToString().Equals(this.gvBuildList.Rows[e.RowIndex].Cells[0].Text)) { drTemp.Delete(); break; } } ds.WriteXml(Server.MapPath("XML/BuildList.xml")); 4 文本框中只能输入数字的js方法: function CheckTotalPrice(text) { text.value=text.value.replace(/[^\.\d]/g,''); if(text.value.split('.').length>2) { text.value=text.value.split('.')[0]+'.'+text.value.split('.')[1]; } else if(text.value.split('.').length==2) { text.value=text.value.split('.')[0] + '.'+text.value.split('.')[1].substr(0,2); } } <asp:TextBox ID="txtTotalPriceTo" runat="server" CssClass="text-line" onKeyUp="CheckTotalPrice(this)"></asp:TextBox> 5 清除表单里所有TextBox里的值(该表单里有其它元素) <script language="C#" runat="server"> protected void ClearText(Object sender,EventArgs e) { ClearTextBox(this); } protected void ClearTextBox(Control c) { if(c is TextBox) { ((TextBox)c).Text=""; } foreach(Control cc in c.Controls) { ClearTextBox(cc); } } </script> <asp:Button ID="btn" runat="server" Width="86px" CssClass="bttn" Text="重新填写" OnClick="ClearText" /> 6 连接打印机,打印功能:实例 固定资产系统,asset/AssetBillQuery.aspx 7 gridview 里,模板列里添加按钮,按钮出发后台事件的方法: 前台:按钮添加属性 CommandName="DeleteBill",值自己随意填写。 <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False"OnRowCommand="gvList_RowCommand"> <asp:TemplateField HeaderText="永久删除"> <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" CommandName="DeleteBill" ImageUrl="~/images/system/delete.gif" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"batchNO") %>' OnClientClick="javascript:return confirm('确认要删除固定资产账单信息吗?删除后将连同资产明细一并彻底删除');" /> </ItemTemplate> </asp:TemplateField> </asp:GridView> 后台: protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "DeleteBill": this.DeleteBill(e.CommandArgument.ToString()); break; default: break; } } 8 gridview 里,模板列里添加按钮,进行增删改操作的方法,可以用7的方法,也可以这样: <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" OnRowEditing="gvList_RowEditing" OnRowDeleting="gvList_RowDeleting" OnRowUpdating="gvList_RowUpdating" OnRowCancelingEdit="gvList_RowCancelingEdit"> <asp:TemplateField HeaderText="导出账单"> <ItemTemplate> <asp:ImageButton ID="ibtnPrint" runat="server" CommandName="Cancel" ImageUrl="~/images/system/export.gif" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="账单明细"> <ItemTemplate> <asp:ImageButton ID="ibtnAsset" runat="server" CommandName="Delete" ImageUrl="~/images/system/export.gif" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="打印标签"> <ItemTemplate> <asp:ImageButton ID="ibtnPrintLable" runat="server" CommandName="Update" ImageUrl="~/images/system/BtnPrint.gif" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText=" 修 改 "> <ItemTemplate> <asp:ImageButton ID="ibtEdit" runat="server" CommandName="Edit" ImageUrl="~/images/system/edit.gif" /> </ItemTemplate> </asp:TemplateField> </asp:GridView > protected void gvList_RowEditing(object sender, GridViewEditEventArgs e) { AssetQueryBO bo = new AssetQueryBO(); AssetRegisterBill bill = bo.GetRegisterBill(this.gvList.Rows[e.NewEditIndex].Cells[0].Text); } protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e) { OpenWebForm("./BillAssetList.aspx?BATCHNO=" + this.gvList.Rows[e.RowIndex].Cells[0].Text); } protected void gvList_RowUpdating(object sender, GridViewUpdateEventArgs e) { } protected void gvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { Response.Redirect("./AssetsBillExport.aspx?billNO=" + this.gvList.Rows[e.RowIndex].Cells[0].Text); } 还有一种方法:在OnItemDataBound(...)事件里为每行的某已列值添加属性 e.Item.Cells[COLUMNINDEX].Attributes.Add("onclick", "show('td_" + strtaskId + "')"); 10 弹出模态对话框,页面上有回传操作,操作完后点击关闭按钮,发现关闭不了,解决方法为: 在<head></head>中加入<base targe="_self"/> 11 asp.net 后台向前台注册js 段的方法 Page.ClientScript.RegisterStartupScript(GetType(), "111", strTemp); 12 判断一个数值是否为NULL,的方法 if(dt.Row[0]["IsRead"]==System.DBNull.Value) { //如果是NUll } 12 gridview 绑定 在一TemplateFiled上绑定一个值,如果该值为1,则在页面上显示“是”,如果为“0”,则在页面上显示为“否”的方法: <asp:TemplateField HeaderText="是否主用户"> <ItemStyle HorizontalAlign="center" Width="10%" ForeColor="Black"></ItemStyle> <ItemTemplate> <%# Eval("IsMain").ToString()=="1"?"是":"否"%> </ItemTemplate> </asp:TemplateField> 13 使用jquery里trim方法 <script src="../Scripts/jquery-1.2.6.js" type="text/javascript"></script> //发送时的检查 function checkFormSend() { var GroupCode = $("#" + "<%=txtGroupCode.ClientID %>").val(); var GroupName = $("#" + "<%=txtGroupName.ClientID %>").val(); if (GroupCode == "" || $.trim(GroupCode)=="") { alert("请输入分组编码!"); $("#" + "<%=txtGroupCode.ClientID %>").focus(); return false; } if (GroupName == "" || $.trim(GroupName) == "") { alert("请输入分组名称!"); $("#" + "<%=txtGroupName.ClientID %>").focus(); return false; } return true; } 14 js比较日期大小 <script type="text/javascript"> function check() { var starttime = document.getElementById("TextBox1").value; var endtime = document.getElementById("TextBox2").value; checkDate(starttime, endtime, '-'); } function checkDate(starttime, endtime, splitStr) { var aStart = starttime.split(splitStr); var aEnd = endtime.split(splitStr); var startDate = aStart[0] + "/" + aStart[1] + "/" + aStart[2]; var endDate = aEnd[0] + "/" + aEnd[1] + "/" + aEnd[2]; if (startDate > endDate) { alert("开始时间不能大于结束!"); } else { alert("ddd"); } } 15 读取xml格式的字符串: XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(requestData); XmlNodeList xmlNodeList = xDoc.SelectNodes("/usermsg"); 16 下载文件,用a标签 ,如果文件后缀为.reg.则要在IIS里添加MIME,.reg reg 17 gridview添加样式 protected void dgList_RowDataBound(object sender, GridViewRowEventArgs e) { dgList.AlternatingRowStyle.BackColor =Color.Red; dgList.BackColor = Color.Green; if (e.Row.RowType == DataControlRowType.DataRow) { string id = dgList.DataKeys[e.Row.RowIndex].Value.ToString(); e.Row.Attributes.Add("id", id); //绑定的时候,修改某一列的值 DataRowView row = (DataRowView)e.Row.DataItem; Label retLbl = e.Row.FindControl("AppState") as Label; string ott = row["OperationType"].ToString(); if (row["OperationType"].ToString() == "addOrg") { retLbl.Text = "新增组织机构"; } } } /// <summary> /// DataGrid样式设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { DataGrid dg = ((DataGrid)sender) ; dg.CssClass = "CSSDataGrid"; dg.HeaderStyle.CssClass = "HeaderStyle"; dg.FooterStyle.CssClass = "FooterStyle"; dg.ItemStyle.CssClass = "ItemStyle"; dg.SelectedItemStyle.CssClass = "SelectedItemStyle"; dg.AlternatingItemStyle.CssClass = "AlternatingItemStyle"; dg.EditItemStyle.CssClass = "EditItemStyle"; dg.PagerStyle.CssClass = "PagerStyle"; dg.GridLines = GridLines.Both ; dg.BorderColor = ColorTranslator.FromHtml("#89C2D6"); } 18 前台绑定值,将时间类型改变: Text='<%# Convert.ToDateTime(Eval("EndDate")).ToString("yyyy-MM-dd") %>' 19 前台调用后台的方法: <%#GetHead()%> 后台代码: public string GetHead() { string re = "<input type=\"checkbox\" name=\"chkSelectAll\" id=\"chkSelectAll\" onclick=\"selectAll(this);\" />全选"; if (currentNode.wn_isMulti)//多选 { re = "<input type=\"checkbox\" name=\"chkSelectAll\" id=\"chkSelectAll\" onclick=\"selectAll(this);\" />全选"; } else { re = "<input type=\"checkbox\" name=\"chkSelectAll\" id=\"chkSelectAll\" onclick=\"selectAll(this);\" style=\"display:none\"/>选择"; } return re; } 20 js 每隔一段时间后执行一个方法 <script type="text/jscript"> function close() { parent.location.reload(); } //停止1秒钟后关闭该页面,并且刷新主页面 function window.onload() { setInterval("close()", 1200); } </script> 21 sqlserver2008 还原数据库,提示正在使用,无法独占使用。 解决方法:先执行第一条sql,执行完再执行第二条sql。就可以了。 alter database AgilePoint set offline with rollback immediate alter database AgilePoint set online 22 刷新父页面 parent.location.replace(parent.location.href + ''); 23 SqlServer 2008中,sql语句查询,查询条件里有中文,则查询不到记录。 修改方法:右击数据库-----属性---- 选项-----排序规则:改成“Chinese_PRC_90_CI_AS”,别的默认即可。点击“确定”。