日期:2014-05-16  浏览次数:20785 次

ASP.NET中常用方法总结

一:JQuery实现在GridView中 点击一行的任意位置  选中这一行中的CheckBox

$(function(){
        $("#GridView1").find("tr").click(function(){
            $(this).find("input[type='checkbox']").attr("checked",!$(this).find("input[type='checkbox']").attr("checked"));
             
        });
         
    })

二:在ASP.NET后台实现 GridView 光棒效果


protected void gvProjectList_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
	//这是鼠标移到某行时改变某行的背景 
	e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#eaeaea';");
	//鼠标移走时恢复
	e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;"); 
} 


三:在GridView中点击删除LinkButton时  提示是否删除

前台实现:

 <ItemTemplate>
          <asp:LinkButton ID="LinkDelete" runat="server" Visible ="false" OnClientClick="if(!confirm('确定要删除这条纪录吗?')) return false;"
                       CommandName="lb_delete" Text="删除" CommandArgument='<%#Bind("aa") %>' />
  </ItemTemplate>

后台实现:

//数据绑定时对删除按钮添加提示
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
	//只有数据行才有绑定数据
	if (e.Row.RowType == DataControlRowType.DataRow)
        {
	//由于是链接按钮所以声明一个链接按钮,根据实际情况变动
            LinkButton lnkBtFalg = e.Row.Cells[0].Controls[0] as LinkButton;
            lnkBtFalg.Attributes.Add("onclick", "javascrip:return confirm('您真要的删除吗!')");
        }
    }

四:在服务器端写js脚本  弹出对话提示框

public static void ShowMessage(System.Web.UI.Page thePage,string Text)
        {
            Text = Text.Replace("\"", "").Replace("'", "").Replace("\r", "").Replace("\n", "");
            thePage.ClientScript.RegisterStartupScript(thePage.GetType(), Guid.NewGuid().ToString(),
              "alert('"+Text+"');", true);}

五:Md5加密算法

 public static string Md5Hash(string text)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
            MD5 md5 = MD5.Create();
            byte[] resultBytes=md5.ComputeHash(bytes);//md5

            string md5Result = "";

            for (int i = 0; i < resultBytes.Length; i++)
            {
                md5Result += resultBytes[i].ToString("X2");
            }


            return md5Result;
        }