gridview显示格式
HtmlEncode属性更改。设为HtmlEncode=false
形式 语法 结果 注释
数字 {0:N2} 12.36
数字 {0:N0} 13
货币 {0:c2} $12.36
货币 {0:c4} $12.3656
货币 "¥{0:N2}" ¥12.36
科学计数法 {0:E3} 1.23E+001
百分数 {0:P} 12.25% P and p present the same.
日期 {0:D} 2006年11月25日
日期 {0:d} 2006-11-25
日期 {0:f} 2006年11月25日 10:30
日期 {0:F} 2006年11月25日 10:30:00
日期 {0:s} 2006-11-26 10:30:00
时间 {0:T} 10:30:00
gridview经典的分页代码
if (gdvPurchasesRecord.EditIndex != -1)
{
e.Cancel = true;
int newPageNumber = e.NewPageIndex + 1;
}
else
{
gdvPurchasesRecord.PageIndex = e.NewPageIndex;
gdvPurchasesRecord.DataBind();
}
gridview转换数据库中的值
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lb1 = (Label)e.Row.FindControl("lblState");
if (lb1.Text == "0")
{
lb1.Text = "未接";
}
else if (lb1.Text == "1")
{
lb1.Text = "待送";
}
}//模板列
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[5].Text.Length > 30)
{
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(0, 30) + "...";
}
if (e.Row.Cells[3].Text == "1")
{
e.Row.Cells[3].Text = "集团新闻";
}
}//绑定列
滚动图片代码
result = result + "<a href='XX.aspx?name=" + XX + "' target=_blank><img src=" + XX + " width=187 height=200/></a>";
result = "<MARQUEE οnmοuseοver=stop(); οnmοuseοut=start(); scrollAmount=1 scrollDelay=5 direction=up width=180 height=600 align=center>" + result + "</MARQUEE>";
//datalist分页代码
if (Request.QueryString["xx"] != null)
{
sentence = "select * from News where Content like '%" + Request.QueryString["condition"] + "%'";
}
else
{
sentence = "select * from News";
}
DataSet ds = new DataSet();
SqlConnect sqlCon = new SqlConnect("sqlserver");
ds = sqlCon.LoadDataSet(sentence);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 20;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
objPds.CurrentPageIndex = CurPage - 1;
lblCurrentPage.Text = "当前页: 第" + CurPage.ToString() + "页";
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
if (!objPds.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
DataList1.DataSource = objPds;
DataList1.DataBind();
//新闻绑定
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("Title") %>' NavigateUrl='<%# "NewsDetail.aspx?nid="+ HttpUtility.UrlEncode(Eval("ID").ToString())%>'></asp:HyperLink>
//生成验证码
protected void Page_Load(object sender, EventArgs e)
{
string rand = RndNum(4);
Session["rand"] = rand;
ValidateCode(rand);
}
private void ValidateCode(string VNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 12;//定义图像的大小
Img = new Bitmap(gheight, 24);//生成图像的实例
g = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象
g.Clear(Color.DarkBlue);
Font f = new Font("Arial Black", 10);//生成Font类的实例
SolidBrush s = new SolidBrush(Color.White);//生成笔刷类的实例
g.DrawString(VNum, f, s, 3, 3);//将VNum写入图片中
ms = new MemoryStream();//生成内存流对象
Img.Save(ms, ImageFormat.Jpeg);//将此图像以Jpeg图像文件的格式保存到流中
Response.ClearContent();//更改http ContentType
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());//将整个二进制流的内容写入字节数组
//回收资源
g.Dispose();
Img.Dispose();
Response.End();
}
/// <summary>
/// 该方法用于生成指定位数的随机数
/// </summary>
/// <param name="VcodeNum">参数是随机数的位数</param>
/// <returns>返回一个随机数字符串</returns>
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
",q,r,s,t,u,v,w,x,y,z";
string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成数组
string VNum = "";
int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数
Random rand = new Random();
//采用一个简单的算法以保证生成随机数的不同
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
//web.config配置
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx"></customErrors>
<httpRuntime maxRequestLength="4096" executionTimeout="240"/>
<add name="sqlserver" connectionString="Data Source=LocalHost;Initial Catalog=Business;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
<add name="access" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/CLCDB.mdb" providerName="System.Data.OleDb"/>
DropDownList绑定时追加默认值
AppendDataBoundItems="True"
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
ListItem li = new ListItem("请选择");
ddlCategory.Items.Insert(0, li);
}//类别添加默认值
protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
ddlCategory.Items.Clear();
}//清空类别值
Form或Panel DefaultButton 响应回车