private void btnBookAdd_Click(object sender, System.EventArgs e)
{
if(this.txtProductCode.Text!=null && this.txtProductCode.Text.Length>0 && this.txtProductName.Text!=null && this.txtProductName.Text.Length>0)
{
//////////////////////////
//保存上传新闻图象
int errorno=0;
string filename=" ";//保存头像图片的路径信息
string name=""; //仅保存文件名
if(newsimage.PostedFile==null)
{
// wc.InnerHtml="请先选择一个图片再点粘贴";
errorno=1;
}
else
{
if(newsimage.PostedFile.FileName.Length<3)
{
// wc.InnerHtml="请先选择一个图片再点粘贴";
errorno=1;
}
else
{
if(newsimage.PostedFile.FileName.Substring(newsimage.PostedFile.FileName.Length-3).ToLower()!="gif" && newsimage.PostedFile.FileName.Substring(newsimage.PostedFile.FileName.Length-3).ToLower()!="jpg" )
{
// wc.InnerHtml="图片格式只接受jpg和gif两种格式";
errorno=1;
}
if(newsimage.PostedFile.ContentLength==0)
{
// wc.InnerHtml="请先选择一个图片再点粘贴";
errorno=1;
}
if(newsimage.PostedFile.ContentLength>600000)
{
// wc.InnerHtml="图片大小限定在600K以内";
errorno=1;
}
}
}
if(errorno==0)
{
string path=Server.MapPath("images");
filename=DateTime.Now.ToString();
filename=filename.Replace(":","");
filename=filename.Replace("-","");
filename=filename.Replace(" ","");
Random r=new Random();
filename=filename+r.Next(1000);
filename=filename+"."+newsimage.PostedFile.FileName.Substring(newsimage.PostedFile.FileName.Length-3);
name=filename;
filename=path+"/"+filename;
try
{
newsimage.PostedFile.SaveAs(filename);
}
catch
{
// wc.InnerHtml=ex.Message;
}
}
//////////////////////////
try
{
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connectionString"]);
if(this.ddlCata.SelectedIndex==-1)
{
this.ddlCata.SelectedIndex=0;
}
DateTime time=DateTime.Now;
if(this.txtProductIntroductionDate.Text!=null && this.txtProductIntroductionDate.Text.Length>0)
{
time=DateTime.Parse(this.txtProductIntroductionDate.Text);
}
string ProductCode=this.txtProductCode.Text;//
string ProductType=this.ddlCata.SelectedItem.Value;//
DateTime ProductIntroductionDate=time;
string ProductName=this.txtProductName.Text;//
string ProductDescription="";
if(this.txtProductDescription.Text!=null)
{
ProductDescription=this.txtProductDescription.Text;//
}
string ProductDetail="";
if(this.txtDetail.InnerHtml!=null)
{
ProductDetail=htmlencode(this.txtDetail.InnerHtml);
}
string ProductSize="";
if(this.txtProductSize.Text!=null)
{
ProductSize=this.txtProductSize.Text;//
}
string ProductImageURL=name;//
double UnitPrice=0;
if(this.txtUnitPrice.Text!=null)
{
UnitPrice=double.Parse(this.txtUnitPrice.Text);//
}
int OnSale=Int32.Parse(this.ddlOnSale.SelectedItem.Value);
string strcom="insert into Products ( ProductCode,ProductType,ProductIntroductionDate,ProductName,ProductDescription,ProductDetail,ProductSize,ProductImageURL,UnitPrice,OnSale ) values ( '"+ProductCode+"','"+ProductType+"','"+ProductIntroductionDate+"','"+ProductName+"','"+ProductDescription+"','"+ProductDetail+"','"+ProductSize+"','"+ProductImageURL+"',"+UnitPrice+","+OnSale+")";
SqlCommand com=new SqlCommand(strcom,con);
con.Open();
com.ExecuteNonQuery();
con.Close();
this.wc.InnerHtml="<b>添加成功!</b>";
this.txtProductCode.Text="";
this.txtProductDescription.Text="";
this.txtProductIntroductionDate.Text="";
this.txtProductName.Text="";
this.txtProductSize.Text="";
this.txtUnitPrice.Text="";
}
catch(Exception ex)
{
Response.Redirect("Error.aspx?error="+ex.Message);
}
}
}
<IMG src='<%= Context.Request.ApplicationPath %>/images/<%#DataBinder.Eval(Container.DataItem, "ProductImageURL")%>'>