asp.net C# 技术小点
1、asp.net里GridView的字段,怎么居中显示?
解决:
VS设计器里面属性里面有个RowStyle,下面HorizontalAlign设置成center
2、MD5加密?
解决:
private static string Password(string pass)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(pass,"MD5");
}
3、FreeTextBox控件使用是报错误提示隐藏的脚本?
解决:
ValidateRequest="false"
4、鼠标单击事件时显示成小手?
解决:
<img name="aa" onclick="test()" style="cursor:pointer">
5、FileUpload控件如何使用?
解决:
string strFileFullName = System.IO.Path.GetFileName(this.FileUpload1.PostedFile.FileName);
if (strFileFullName.Length > 0)
{
if (this.FileUpload1.HasFile)
{
string path = Server.MapPath("~/images/Toiletry/" + strFileFullName);
this.FileUpload1.SaveAs(path);
}
else
{
ScriptManager.RegisterStartupScript(this.ImageButton1, this.GetType(), "", "<script>alert('找不到此图片!');</script>", false);
return;
}
}
6、FreeTextBox如何设置成中文提示?
解决:
控件中有个属性 Language 设置成 zh-CN
7、GridView控件中设置删除控件单击弹出提示框?
解决:
<script>return window.confirm('确认要删除选中的信息!');</script>
8、弹出对话框单击确定后跳转网页?
解决:
<script>window.location.href='UserLogin.aspx';</script>
9、GridView控件中如何获取选中列的索引?
解决:
得出的值是表示那行被选中的索引值
GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
Label lable=this.GridView6.Rows[Convert.ToInt32(drv.RowIndex)].FindControl
("Label2") as Label;
10、AspNetPager控件+DataList控件怎么使用?
解决:
Load事件中
this.控件名.RecordCount = ArticleManager.GetAll().Count;
this.ShujuDataBings();
方法中
private void ShujuDataBings()
{
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ArticleManager.GetAll();
ps.AllowPaging = true;
ps.PageSize = this.控件名.PageSize;
ps.CurrentPageIndex = this. 控件名.CurrentPageIndex - 1;
this. 控件名.DataSource = ps;
DataList1.DataBind();
}
翻页事件PageChanged
重新绑定方法
11、数据库时间查询?
解决:
select * from articles where convert(varchar(10),pubdate,120) like '
12、使用代码截取新闻内容或者文章内容,使其显示规定数量的文字?
解决:
代码
public string GetString(object s)
{
string str = s.ToString();//如果获取的字符长度大于100的话截取100,否则不截取
return str.Length > 100 ? str.Substring(0, 100) + "......" : str;
}
使用
Text='<%# GetString(Eval("Content")) %>'
13、Web文件配置错误信息设置?
解决:
<customErrors defaultRedirect="error/error.htm" mode="On">
<error statusCode="404" redirect="error/error.htm"/>
<error statusCode="501" redirect="error/error.htm"/>
</customErrors>
14、配置文件里如何配置数据库连接字符串?
解决:
<connectionStrings>
<!--数据库连接串-->
<add name="DBTYPE" connectionString="Data Source=.;Initial Catalog=数据库名字;User ID=sa;pwd=123"/>
</connectionStrings>
15、从某个时间开始到某个时间结束的查询语句?
解决:
select sum(marknumber) as number from rating where customerid =1 and convert(varchar(10),addtime,120) between '
16、页面自动生成验证码,调用使用方法?
解决:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{ System.Random rand = new Random();
int len = rand.Next(4, 4);
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
System.Text.StringBuilder myStr = new System.Text.StringBuilder();
for (int iCount = 0; iCount < len; iCount++) { myStr.Append(chars[rand.Next(chars.Length)]); }
string text = myStr.ToString();
this.Session["checkcode"] = text.ToLower();
System.Drawing.Size ImageSize = System.Drawing.Size.Empty;
System.Drawing.Font myFont = new System.Drawing.Font("MS Sans Serif", 15);
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(5, 5))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
{
System.Drawing.SizeF size = g.MeasureString(text, myFont, 10000);
ImageSize.Width = (int)size.Width + 6;
ImageSize.Height = (int)size.Height + 0;
}
}
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ImageSize.Width, ImageSize.Height))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
{
g.Clear(System.Drawing.Color.White);
using (System.Drawing.StringFormat f = new System.Drawing.StringFormat())
{
f.Alignment = System.Drawing.StringAlignment.Near;
f.LineAlignment = System.Drawing.StringAlignment.Center;
f.FormatFlags = System.Drawing.StringFormatFlags.NoWrap;
g.DrawString(text, myFont, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(0, 0, ImageSize.Width, ImageSize.Height), f);
}
}
int num = ImageSize.Width * ImageSize.Height * 30 / 120;
for (int iCount = 0; iCount < num; iCount++)
{
int x = rand.Next(ImageSize.Width);
int y = rand.Next(ImageSize.Height);
int r = rand.Next(255);
int g = rand.Next(255);
int b = rand.Next(255);
System.Drawing.Color c = System.Drawing.Color.FromArgb(r, g, b);
bmp.SetPixel(x, y, c);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
this.Response.ContentType = "image/Jpeg";
ms.WriteTo(this.Response.OutputStream);
ms.Close();
}
myFont.Dispose();
}
</script>
调用:
string checkcode = Session["checkcode"] as string;
if (this.txtcode.Text.Trim().ToLower().Equals(checkcode)){业务逻辑处理}
生成:
<img src="UserCheck/CheckCode.aspx"title="看不清楚,单击图片换一张。" onclick="this.src = 'UserCheck/CheckCode.aspx?flag=' + Math.random()" border="1"/>
17、CS架构弹出网页?
解决:
LinkButten事件里写System.Diagnostics.Process.Start("www.qingdao100.net");
18、后台代码控制页面?
解决:HtmlMeta hm = new HtmlMeta();
Page.Title = "网站标题";
hm.Name = "keywords";
hm.Content = "网站数据测试正在进行中";
Page.Header.Controls.AddAt(0, hm);
19.文本框内添加图片效果?
解决:
style="background-image:url(../images/menu_icon.gif);background-position:right; background-repeat:no-repeat;">
20.清空页面控件的值?
解决:
HtmlForm htmlForm = (HtmlForm)Page.FindControl("form1");
if (htmlForm != null)
{
foreach (Control item in htmlForm.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = String.Empty;
}
}
}
21、Base64加密方法?
解决:
加密:
Byte[] str = System.Text.Encoding.UTF-8.GetBytes(要加密的字符);
Return Convert.ToBase64String(str);
解密:
Byte[] str = Convert.FromBase64String(要解密的字符);
Return System.Text.Encoding.UTF-8.GetString(str);
22、数据库除去重复的列?
解决:
SELECT distinct DK_KIND,ORG_CODE FROM Code_Glebe_Desc WHERE ORG_CODE=@ORG_CODE
23、实现下拉列表重新选择?
解决:
ddl_httype.ClearSelection();//首新清空选择
ddl_httype.Items.FindByValue("10").Selected = true;//在绑定你的选择
23、GridView控件实现光棒效果?
解决:
Protected void gv_liebiao_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmousemove", "this.style.backgroundColor='#CCCCCC'");//#CCCCCC浅灰色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
}