//光棒效果
protected void GridView1_RowDataBound(objectsender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#87CEEB'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
HtmlInputCheckBoxch = (HtmlInputCheckBox)e.Row.FindControl("cbTui");
intxx = Convert.ToInt32(ch.Value);
if(xx == 1)
{
ch.Checked = true;
}
else
{
ch.Checked = false;
}
HtmlInputCheckBoxc = (HtmlInputCheckBox)e.Row.FindControl("cbHot");
intx = Convert.ToInt32(c.Value);
if(x == 1)
{
c.Checked = true;
}
else
{
c.Checked = false;
}
}
}
光棒显示,用行数据绑定事件。
复选按钮的绑定
也在这个事件里,注意要判断一下,行类型要是数据行的时候绑定。
。先在页面那里绑定好value,然后根据value值判断是否选中。
页面要注意要在加载事件里用isPostBack,如:
If(!isPostBack)
{
……内容
}
否则会取不到内容控件已经更改的数据,而仍然是旧的数据。这一点我是屡屡犯错,屡屡不改!
Asp的上传图片(单张上传)
//上床方法
public void Upload()
{
HttpPostedFileupImg=this.upImg.PostedFile;
ClientScriptManagercs = Page.ClientScript;
stringpicName = "";//生成时间格式的文件名
stringfilename = "";//不?含?路¡¤径?的Ì?文?件t名?
//upImg的格式:C:\cake\sdfsdf.jpg
filename =upImg.FileName.Substring(upImg.FileName.LastIndexOf("\\")+ 1, upImg.FileName.Length - upImg.FileName.LastIndexOf("\\") - 1);
stringextension = System.IO.Path.GetExtension(filename).ToLower();
//格式限制处理
if(extension == ".jpg" || extension== ".jpeg" || extension == ".gif" || extension == ".bmp")
{
Randomra = new Random();
DateTimed1 = DateTime.Now;
picName = d1.ToString("yyyyMMddHHmmss") + ra.Next(100,999).ToString() + extension;//生成时间格式的文件名
stringsavePath = Server.MapPath("~/UpPic/"+ picName);//保存的路径
upImg.SaveAs(savePath);
Session["pic"] = picName;
this.add.Style.Clear();
this.add.Style["display"] = "block";
cs.RegisterStartupScript(this.GetType(), "ss","alert('上传成功')", true);
}
else
{
cs.RegisterStartupScript(this.GetType(), "ss","alert('上传失败')", true);
}
}
//先把放在文件里的图片删除了,再做数据的删除操作,删除数据库的记录。
File.Delete(Server.MapPath("~/UpPic/" + f.PicUrl));
.Nets生成文件夹,以及文件的操作
//创建文件夹
public static bool CreateDir(stringpath,string fileName)
{
try
{
Directory.CreateDirectory(path+"\\"+fileName);
returntrue;
}
catch(Exception)
{
returnfalse;
}
}
/// <summary>
///
/// </summary>
/// <param name="dir">完整路径路径</param>
public static List<ModelFile> FindFile(stringdir)
{
//在指定目录及子目录下寻找文件
DirectoryInfo Dir=new DirectoryInfo(dir);
List<ModelFile> li = newList<ModelFile>();
foreach(DirectoryInfo d inDir.GetDirectories()) //寻找子目录
{
ModelFilem = new ModelFile();
m.FileName = d.Name;
m.CreateTime =d.CreationTime.ToString();//文件的创建时间
foreach(var item inDir.GetDirectories(d.Name))//以下操作只是为了计算该文件夹下的文件数量
{
inti = 0;
foreach(var it initem.GetFiles())
{
i++;
}
m.Num = i;
}
li.Add(m);
}
returnli;
}
//返¤¦Ì回?某3一°?文?件t夹D下?的Ì?html文?件t
//读取出子文件
public static List<ModelHtmlFile> FindSonFile(string dir,stringfileName)
{
//在¨²指?定¡§目?录?及¡ã子Á¨®目?录?下?查¨¦找¨°文?件t
DirectoryInfoDir = new DirectoryInfo(dir);
List<ModelHtmlFile> li = newList<ModelHtmlFile>();
vardd=Dir.GetDirectories(fileName);//指定文件夹
foreach(DirectoryInfo d indd) //查找子目录
{
foreach(var item ind.GetFiles())
{
if(item.Extension!=".jpg"){//文件后缀的判断
ModelHtmlFileht = new ModelHtmlFile();
ht.FileName = item.Name;
ht.CreateTime =item.CreationTime.ToString();
ht.Path = dir + fileName;
ht.Type = "html";
li.Add(ht);
}
}
}
returnli;
}
IO流的操作
/// <summary>
/// 文件的读取
/// </summary>
/// <paramname="url">全¨?部?的Ì?路¡¤径?</param>
/// <returns></returns>
public static stringReaderHtml(string url)
{
FileStreamfs = null;
StreamReadersr = null;
stringstr = "";
try
{
//构造文件流
fs = newFileStream(url,FileMode.Open);
sr = newStreamReader(fs);
//从头读到尾
str = sr.ReadToEnd();
//关闭读写器
sr.Close();
//关闭文件流
fs.Close();
}
catch (Exception)
{
sr.Close();
fs.Close();
returnstr;
}
returnstr;
}
//写入
public static boolWirteHtml(string url,stringstr)
{
FileStreamfs = null;
StreamWritersw = null;
try
{
//构造文件流
fs = newFileStream(url, FileMode.Open);
//构造写入器
sw = newStreamWriter(fs, Encoding.UTF8);
sw.Write(str);
// 关闭读写器
sw.Close();
//关闭文件流
fs.Close();
}
catch (Exception)
{
sw.Close();
fs.Close();
returnfalse;
}
return true;
}
用My97DatePicker:
先将js文件WdatePicker.js引入
<asp:TextBox ID="txtTime" runat="server" CssClass="Wdate"
onClick="WdatePicker()"></asp:TextBox>
这次项目的做的批量删除,是把id用逗号拼接在stirng里面,然后,再用split分割出来取出来,放在一个stirng数组里边。然后就是遍历,一个一个分别删除
验证码(似乎MVC的验证码)
1普通的aspx页面的验证码(GetValidateCode.aspx)
需要引入一些命名空间:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
public partial class GetValidateCode: System.Web.UI.Page
{
protectedvoid Page_Load(objectsender, EventArgs e)
{
stringvalidatecode = GenerateCheckCode();
Session["validatecode"]= validatecode;//放到session里面,判断是也是通过session取出来判断
byte[]bytes = CreateValidateGraphic(validatecode);
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(bytes);
}
/// <summary>
/// 获取随机验证码
/// </summary>
/// <returns></returns>
public string GenerateCheckCode()
{
intnumber;
charcode;
stringcheckCode = String.Empty;
System.Randomrandom = new Random();
for(int i = 0; i < 5; i++)
{
number = random.Next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
returncheckCode;
}
/// <summary>
///创建验证码图片
public byte[] CreateValidateGraphic(stringvalidateCode)
{
Bitmapimage = new Bitmap((int)Math.Ceiling(validateCode.Length* 12.0), 22);
Graphicsg = Graphics.FromImage(image);
try
{
//随机生成器
Random random = new Random();
//清空背景色,背景设置为白色
g.Clear(Color.White);
//设置图片的干扰线
for(int i = 0; i < 25; i++)
{
intx1 = random.Next(image.Width);
intx2 = random.Next(image.Width);
inty1 = random.Next(image.Height);
inty2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Fontfont = new Font("Arial", 12, (FontStyle.Bold| FontStyle.Italic));
LinearGradientBrushbrush = new LinearGradientBrush(new Rectangle(0,0, image.Width, image.Height),
Color.Blue,Color.DarkRed, 1.2f, true);
g.DrawString(validateCode,font, brush, 3, 2);
//画图片的前景干扰线
for(int i = 0; i < 100; i++)
{
intx = random.Next(image.Width);
inty = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height- 1);
//保存图片数据
MemoryStreamstream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
returnstream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
这是一个aspx页面,我写在里面,然后输出,最后再由我需要使用的那个页面指向这个页面。
<img onclick="updateCode()"id="validateImg" src="GetValidateCode.aspx" />
updateCode()函数:让它每点击一次改变一次
<script type="text/javascript">
functionupdateCode() {
document.getElementById("validateImg").src = "GetValidateCode.aspx?time" + new Date().getMilliseconds();
}
</script>