您的位置:首页 > 编程语言

各种常用代码收集

2013-07-04 09:12 375 查看
gridview行中的按钮添加提示信息

////给删除按钮加提示

//if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)

//{

// try

// {

// ((LinkButton)e.Row.FindControl("delete")).Attributes.Add("onclick", "javascript:return delAlert()");

// }

// catch

// {

// }

//}

public static string IsEmail(string str)
{
string res = string.Empty;
string expression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
if (Regex.IsMatch(str, expression, RegexOptions.Compiled))
{
res = "输入的电子邮件格式不正确!如(yongjie@4ugood.net)。";
}
return res;
}


/// <summary>

/// 判断一个字符串是否为合法double类型

/// </summary>

/// <param name="s">字符串</param>

/// <returns></returns>

public bool Isdouble(string s)

{

try {

Double d;

bool flag=double.TryParse(s,out d);

return flag;

}

catch {

return false;

}

}

/// <summary>

/// 判断一个字符串是否为合法整数(不限制长度)

/// </summary>

/// <param name="s">字符串</param>

/// <returns></returns>

public bool IsInteger(string s)

{

string pattern = @"^\d*$";

return Regex.IsMatch(s, pattern);

}

日期转换:

System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);

ss.StartTime = DateTime.ParseExact(this.StartTime.Text.Trim(), "yyyy-MM-dd", format);

SQL分页语句:

select top pagesize * from ApplicationProject where Pid not in

(select top (pagesize * (pageindex-1)) Pid from ApplicationProject where EcityReview is not null order by Pdata desc)

and EcityReview is not null order by Pdata desc

select top 20 * from ApplicationProject where Pid not in

(select top (20 * (1-1)) Pid from ApplicationProject where EcityReview is not null order by Pdata desc)

and EcityReview is not null order by Pdata desc

DropdownList选定默认值

this.PStatus.Items.FindByValue(ap.PStatus.ToString()).Selected = true;

gridview模板列对日期进行格式化

<asp:TemplateField HeaderText="申报年份">

<ItemTemplate>

<%#Eval("Pdata", "{0:yyyy}").ToString()%>

</ItemTemplate>

</asp:TemplateField>

gridview模板列绑定linkbutton以及linkbutton的触发事件

<asp:TemplateField HeaderText="申报题目">

<ItemTemplate>

<asp:LinkButton ID="LinkButton1" ToolTip='<%#Eval("PProjectTheme")%>' runat="server" Text='<%#Eval("PProjectTheme")%>' OnClick="LinkButton1_Click"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

/// <summary>

/// 进入编辑文章

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void LinkButton1_Click(object sender, EventArgs e)

{

//

int index = ((GridViewRow)(((LinkButton)sender).Parent.Parent)).RowIndex;

int jid = int.Parse(this.gvList.DataKeys[index].Value.ToString());

Response.Redirect("ApplicationProjectJournalShow.aspx?jid=" + jid);

}

gridview中合并底部以及鼠标经过时变色

protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)

{

//底部栏的跨列

if (e.Row.RowType == DataControlRowType.Footer)

{

int col = 3;

e.Row.Cells[0].ColumnSpan = col;

while (--col > 0)

{

e.Row.Cells[col].Visible = false;

}

}

//如果是绑定数据行

if (e.Row.RowType == DataControlRowType.DataRow)

{

//鼠标经过时,行背景色变

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

//鼠标移出时,行背景色变

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

}

}

gridview中模板列对绑定数据调用方法

<asp:TemplateField HeaderText="状态">

<ItemTemplate>

<%#getStatus(Convert.ToInt32(Eval("PStatus")))%>

</ItemTemplate>

</asp:TemplateField>

/// <summary>

/// 获取不重复随机数的字符串数组

/// </summary>

/// <param name="num"></param>

/// <param name="max"></param>

/// <returns></returns>

protected List<String> getRandomNum(int num,int max)

{

Random rd = new Random();

List<String> numArr = new List<String>();

while (numArr.Count < num)

{

int rdnum = rd.Next(1, max);

if (!numArr.Contains(rdnum.ToString()))

{

numArr.Add(rdnum.ToString());

}

}

return numArr;

}

删除浏览器缓存(命令行代码)

del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"

不同数据库中表数据拷贝

insert into ReviewType(Rname,RxpertClassificationId,Rlevel,Rgrade,ReviewTypeID,Rstandard,Rreference)

select Rname,RxpertClassificationId,Rlevel,Rgrade,ReviewTypeID,Rstandard,Rreference from rjwDB..ReviewType

判断及创建文件夹

if(!Directory.Exists(Server.MapPath("~/UpLoad/ExampleAwardReturns/"+System.DateTime.Now.Year.ToString())))

{

Directory.CreateDirectory(Server.MapPath("~/UpLoad/ExampleAwardReturns/"+System.DateTime.Now.Year.ToString()));

}

修改文件及文件夹名字
//方法一

if (System.IO.File.Exists(srcFileName))

{

System.IO.File.Move(srcFileName, destFileName);

}

if (System.IO.Directory.Exists(srcFolderPath))

{

System.IO.Directory.Move(srcFolderPath, destFolderPath);

}

//方法二

if (System.IO.File.Exists(srcFileName))

{

System.IO.FileInfo file =new System.IO.FileInfo(srcFileName);

file.MoveTo(destFileName);

}

if (System.IO.Directory.Exists(srcFolderPath))

{

System.IO.DirectoryInfo folder =

new System.IO.DirectoryInfo(srcFolderPath);

folder.MoveTo(destFolderPath);

}

移动文件

FileInfo f =
new
FileInfo(
"c:\\t.txt"
);


FileInfo fi = new FileInfo(@"D:\upfile\creat.txt")

fi.CopyTo(@"e:\creat.txt");

fi.Delete();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: