• Asp.Net常用代码


    1、jquery搜索框效果

    $(document).ready(function()    {      
    //搜索框   
        $('#keyword').focus(function() {   
            if($(this).val() == '请输入关键词!') {   
                $(this).val("");   
            }   
        });   
        $('#keyword').blur(function(){   
            if($(this).val() == "") {   
                $(this).val('请输入关键词!');   
            }   
        });   
    });  

      2、Asp.Net给绑定后的DropDownList加上自定义内容

      protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cityname = this.ddlProvince.SelectedItem.Value;
            DataSet ds = Tryine.DBUtility.DbHelperSQL.ExecuteDataSet(CommandType.Text, "select * from Cy_UserInfo where u_city='" + cityname + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.ddlShopID.DataSource = ds;
                this.ddlShopID.DataTextField = "u_companyName";
                this.ddlShopID.DataValueField = "userid";
                this.ddlShopID.DataBind();
                this.ddlShopID.Items.Insert(0, new ListItem("请选择商会", "0"));
            }
        }
    

      3、ajax和ashx提交数据

          $.ajax({
                    type: "POST",    //页面请求的类型,通常使用POST,那么处理页需要使用Request.Form["参数名称"]来获取页面传递的参数
                    url: "http://www.cnblogs.com/WebService/CgAdd.ashx",   //处理页的相对地址
                    data: { qy: qyname, cg: cgName,cp:cgphone, zx: zxName },
                    contentType: "application/x-www-form-urlencoded; charset=gb2312",
                    success: function (msg) {    //这是处理后执行的函数,msg是处理页返回的数据
                        alert(msg);
                        //提交成功后清空文本框
                        $("#QyName").val("");
                        $("#CgName").val("");
                        $("#Cgphone").val("");
                        $("#ZxName").val("");
                    }
                });
    

      4、Repeater删除数据

          在repeater的某一列放入一个按钮,把按钮的e.CommandName设置为“del”,e.CommandArgument设置你要删除的主键,例如

     <td>
       <asp:LinkButton ID="lbtnDel" runat="server" CommandArgument='<%#Eval("id")%>' CommandName="del">删除</asp:LinkButton>
     </td>
    

      然后再rrepeater的ItemCommand事件里写如下代码

      protected void rptEducationalList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                  //删除的主键=e.CommandArgument.tostring();
                  //删除的方法;
            }
        }
    

      5、遍历dataset

               DataSet ds = xxx.GetList("");
    
               if(ds == null || ds.Tables.Count <=0 || ds.Tables[0].Rows.Count <= 0) 
               {
                 return;
               }
               //循环遍历ds
               foreach(DataRow row in ds.Tables[0].Rows)
               {
                   this.xxx.Text = row["列名"].ToString();
                   //........
               }
    

      6、高光显示关键字

            /// <summary>
            /// 高光显示关键字
            /// </summary>
            /// <param name="PageStr">内容</param>
            /// <param name="keys">关键字</param>
            /// <returns></returns>
            public string p__HighLight(string PageStr, string keys)
            {
                string[] key = keys.Split(new string[] { " " }, StringSplitOptions.None);
                for (int i = 0; i < key.Length; i++)
                {
                    PageStr = PageStr.Replace(key[i].Trim(), "<font color=#C60A00>" + key[i].Trim() + "</font>");
                }
                return PageStr;
            }
            /// <summary>
            /// 替换关键字为红色
            /// </summary>
            /// <param name="pain">原始内容</param>
            /// <param name="keyword">关键字,支持多关键字</param>
            public string HighLightKeyWord(string pain, string keys)
            {
                string _pain = pain;
                string[] key = keys.Split(new string[] { " " }, StringSplitOptions.None);
                if (key.Length < 1)
                    return _pain;
                for (int i = 0; i < key.Length; i++)
                {
                    System.Text.RegularExpressions.MatchCollection m = System.Text.RegularExpressions.Regex.Matches(_pain, key[i].Trim(), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    //忽略大小写搜索字符串中的关键字
                    for (int j = 0; j < m.Count; j++)//循环在匹配的子串前后插东东
                    {
                        //j×31为插入html标签使pain字符串增加的长度:
                        _pain = _pain.Insert((m[j].Index + key[i].Trim().Length + j * 31), "</span>");//关键字后插入html标签
                        _pain = _pain.Insert((m[j].Index + j * 31), "<span style=color:red>");//关键字前插入html标签
                    }
                }
                return _pain;
            }
    

      7、获得Model修改数据

      protected void btnCancel_Click(object sender, EventArgs e)
        {
            string recordNO = Request.QueryString["recordNO"];
            rRecord = bllRR.GetModel(recordNO);//获得一个model
            rRecord.Status = "已取消";
            if (bllRR.Update(rRecord))
            {
                AddLog.Add("车辆维修记录", "取消车辆维修:" + recordNO, Convert.ToInt32(Session["organid"].ToString()));
                MsgUtil.CreateInstance().ScriptAlert(this, "取消维修成功!", "/Organ/index.aspx", Icons.success);            
            }
            else
            {
                MsgUtil.CreateInstance().ScriptAlert(this, "取消维修失败!", Icons.alert);
            }
        }
    

          8、发送邮件

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Mail;
    namespace Tryine.Common
    {
        public class EmailClass
        {
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="to">收件人邮件地址</param>
            /// <param name="from">发件人邮件地址</param>
            /// <param name="subject">邮件主题</param>
            /// <param name="body">邮件内容</param>
            /// <param name="username">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param>
            /// <param name="password">登录smtp主机时用到的用户密码</param>
            /// <param name="smtpHost">发送邮件用到的smtp主机</param>
            public void Send(string tos, string froms, string subject, string body, string userName, string password, string smtpHost)
            {
                MailAddress from = new MailAddress(froms);
                MailAddress to = new MailAddress(tos);
                MailMessage message = new MailMessage(from, to);
                
                message.Subject = subject;//设置邮件主题
                message.IsBodyHtml = true;//设置邮件正文为html格式
                message.Body = body;//设置邮件内容
                SmtpClient client = new SmtpClient(smtpHost);
                //设置发送邮件身份验证方式
                //注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com
                client.Credentials = new NetworkCredential(userName, password);
                client.Send(message);
            }
        }
    }
    

     9、搜索框效果

     /* 搜索文本框效果 */
        $(function () {
            $("#searchkey").focus(function () {
                $(this).addClass("focus");
                if ($(this).val() == this.defaultValue) {
                    $(this).val("");
                }
            }).blur(function () {
                $(this).removeClass("focus");
                if ($(this).val() == '') {
                    $(this).val(this.defaultValue);
                }
            })
        })
    

    10、指定下拉列表的选定项

    string pid = ds.Tables[0].Rows[0]["parentID"].ToString();
    if (pid != "0")
    {
        this.ddl_P.Items.FindByValue(pid).Selected = true;
    }
    

    11、

  • 相关阅读:
    gzip是一种数据格式,deflate是一种压缩算法
    js 实现图片上传 续
    iframe 元素会创建包含另外一个文档的内联框架(即行内框架)
    HTTPS简介----
    回归测试
    HTTP 返回码 400
    js 实现 一张图片的上传
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/Jiangliang/p/3042117.html
Copyright © 2020-2023  润新知