using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using Shop.SystemControls;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using Shop.Systemcontrols;
using System.Xml;
using System.Text;
using picmark;

namespace Shop.Controls


{

/**//// <summary>
/// addproduct 的摘要说明。
/// </summary>
public class addproduct:TemplatedWebControl

{
public string sql,name2,StrPath;
protected DataSet ds = new DataSet();
protected Systemcontrols.DAL.product dbproduct = new Shop.Systemcontrols.DAL.product();
protected Systemcontrols.DAL.bigclass dbbigclass = new Shop.Systemcontrols.DAL.bigclass();
protected Systemcontrols.DAL.author dbauthor = new Shop.Systemcontrols.DAL.author();
protected Systemcontrols.DAL.year dbyear = new Shop.Systemcontrols.DAL.year();
protected System.Web.UI.WebControls.TextBox temp;
TextBox TextBox1;
TextBox TextBox2;
TextBox TextBox4;
TextBox TextBox6;
TextBox TextBox7;
DropDownList DropDownList1;
DropDownList DropDownList2;
DropDownList DropDownList3;
DropDownList DropDownList4;
Button Button1;
Button Button2;
Label Label9;
HtmlInputFile fileup1;
private string AllowExtendName = ".JPEG|.JPE|.JPG"; // 允许的文件扩展名称。大写
public addproduct()

{
StrPath = HttpContext.Current.Server.MapPath("../fpv/fpv_xmls/photos.xml");
}


method#region method
private string big

{
get

{
if(ViewState["big"] != null && ViewState["big"].ToString() != "")

{
return ViewState["big"].ToString();
}
else

{
if(HttpContext.Current.Request["big"] != null && HttpContext.Current.Request["big"].ToString() != "")

{
return HttpContext.Current.Request["big"];
}
else

{
return "";
}
}
}
set

{
ViewState["big"] = value;
}
}
#endregion

private void down1_bind()

{
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "Big_classid";
DropDownList1.DataSource = dbbigclass.GetList();
DropDownList1.DataBind();
// DropDownList1.Items.Insert(0,new ListItem("请选择",""));
}

private void myinit2()

{
DropDownList3.DataTextField = "author_name";
DropDownList3.DataValueField = "id";
DropDownList3.DataSource = dbauthor.GetList();
DropDownList3.DataBind();
DropDownList3.Items.Insert(0,new ListItem("请选择",""));
}

private void myinit3()

{
DropDownList4.DataTextField = "name";
DropDownList4.DataValueField = "id";
DropDownList4.DataSource = dbyear.GetList();
DropDownList4.DataBind();
DropDownList4.Items.Insert(0,new ListItem("请选择",""));
}

protected override void AttachChildControls()

{
TextBox1 = (TextBox)FindControl("TextBox1");
TextBox2 = (TextBox)FindControl("TextBox2");
TextBox4 = (TextBox)FindControl("TextBox4");
TextBox6 = (TextBox)FindControl("TextBox6");
TextBox7 = (TextBox)FindControl("TextBox7");
Button1 = (Button)FindControl("Button1");
Button2 = (Button)FindControl("Button2");
DropDownList1 = (DropDownList)FindControl("DropDownList1");
DropDownList2 = (DropDownList)FindControl("DropDownList2");
DropDownList3 = (DropDownList)FindControl("DropDownList3");
DropDownList4 = (DropDownList)FindControl("DropDownList4");
Button1.Click += new EventHandler(Button1_Click);
Button2.Click += new EventHandler(Button2_Click);
fileup1 = (HtmlInputFile)FindControl("fileup1");
Label9 = (Label)FindControl("Label9");
temp = (System.Web.UI.WebControls.TextBox)FindControl( "temp" );

StringBuilder sbHidden = new StringBuilder( );
sbHidden.Append( "<script language = javascript >" );
sbHidden.Append( " document.getElementById( \"" + temp.ClientID + "\" ).style.visibility = \"hidden\";" );
sbHidden.Append( "</script>" );
this.Page.RegisterStartupScript( "", sbHidden.ToString( ));

DropDownList2.Attributes.Add( "OnChange", "AttachValue( );" );
script();
if (!Page.IsPostBack)

{
myinit2();
myinit3();
down1_bind();
DropDownList1.Attributes.Add("onchange","XmlPost2(this);");
}
if(big != "")

{
down2_bind(big);
}
}

脚本#region 脚本
private void script()

{
StringBuilder sb = new StringBuilder( );
sb.Append( "<SCRIPT language=\"javascript\">");
sb.Append( "function XmlPost2(obj)" );
sb.Append( "{" );
sb.Append( " var svalue = obj.value;" );
sb.Append( " var webFileUrl = \"?big=\" + svalue;" );
sb.Append( " var result = \"\";" );
sb.Append( " var xmlHttp = new ActiveXObject(\"MSXML2.XMLHTTP\"); " );
sb.Append( " xmlHttp.open(\"POST\", webFileUrl, false); " );
sb.Append( " xmlHttp.send(\"\");" );
sb.Append( " result = xmlHttp.responseText;" );
sb.Append( " if(result != \"\")" );
sb.Append(" {");
sb.Append( " document.all(\"" + DropDownList2.ClientID + "\").length = 0;" );
sb.Append( " var piArray = result.split(\",\");" );
sb.Append( " for(var i=0;i<piArray.length;i++)" );
sb.Append( " {");
sb.Append( " var ary1 = piArray[i].toString().split(\"|\");" );
sb.Append( " document.all(\"" + DropDownList2.ClientID + "\").options.add( new Option( ary1[ 1 ].toString( ),ary1[ 0 ].toString( ) ) );" );
sb.Append( " }" );
sb.Append( " }" );
sb.Append( " else" );
sb.Append(" {");
sb.Append( " alert('此大类下还没有小类,请先添加');");
sb.Append( " document.all(\"" + DropDownList2.ClientID + "\").length = 0;" );
sb.Append( " }" );
sb.Append( "}" );
sb.Append( "function AttachValue() " );
sb.Append( "{" );
sb.Append( " var i;");
sb.Append( " for( i = 0; i < document.all(\"" + DropDownList2.ClientID + "\").length; i++ )" );
sb.Append( " {");
sb.Append( " if( document.all(\"" + DropDownList2.ClientID + "\").options[ i ].selected == true )" );
sb.Append( " {");
sb.Append( " document.getElementById( \"" + this.temp.ClientID + "\" ).value = document.all(\"" + DropDownList2.ClientID + "\").options[ i ].value;" );
sb.Append( " break;" );
sb.Append( " }");
sb.Append( " }" );
sb.Append( "}");
sb.Append( "function checkData() " );
sb.Append( "{" );
sb.Append( " debugger;");
sb.Append( " var fileName = document.getElementById(\""+ fileup1.ClientID +"\").value;");
sb.Append( " if(fileName == \"\")");
sb.Append( " return;");
sb.Append( " var exName=fileName.substr(fileName.lastIndexOf(\".\")+1).toUpperCase();");
sb.Append( " if(exName==\"JPG\")");
sb.Append( " {");
sb.Append( " document.getElementById(\"previewImage\").innerHTML='<img src=\\''+fileName+'\\' width=100 height=100 >';");
sb.Append( " }");
sb.Append( " else");
sb.Append( " {");
sb.Append( " alert(\"请选择正确的图片文件\");");
sb.Append( " document.getElementById(\""+ fileup1.ClientID +"\").value=\"\";");
sb.Append( " }");
sb.Append( "}");
sb.Append( "</SCRIPT>" );
this.Page.RegisterClientScriptBlock("", sb.ToString());
}
#endregion


上传图片#region 上传图片
private void Button2_Click(object sender, EventArgs e)

{
if (fileup1.PostedFile.ContentLength == 0)

{
Label9.Text="上传失败或指定的文件不存在";
}
else

{
string extendName = System.IO.Path.GetExtension(fileup1.PostedFile.FileName).ToString();
if (CheckValidExt(extendName))

{
name2 = GetRandomFileName();
string path1 = HttpContext.Current.Server.MapPath("../upload/")+name2+extendName.ToUpper();
string path2 = HttpContext.Current.Server.MapPath("../upload/") + name2+"_water"+".jpg";
fileup1.PostedFile.SaveAs(path1);

picmark.ImageModification wm= new ImageModification();
wm.DrawedImagePath = HttpContext.Current.Server.MapPath("../upload/") + "backlogo.gif"; //水印图片
wm.ModifyImagePath = path1; //图片的路径
wm.RightSpace = 111; //水印位置
wm.BottoamSpace = 110; //水银位置
wm.LucencyPercent = 100; //透明度
wm.OutPath = path2;
wm.DrawImage();
if(File.Exists(path1))

{
File.Delete(path1);
}

TextBox6.Text="upload/" + name2+"_water"+".jpg";
Label9.Text="<font color=red>上传成功</font>";
}
else

{
HttpContext.Current.Response.Write("<script>alert('上传的格式只能是jpg格式!');</script>");
}
}
}
#endregion

// 获取一个随机的文件名称
private string GetRandomFileName()

{
string strData = DateTime.Now.ToString("yyyyMMddHHmmss");
strData += System.Guid.NewGuid();
return strData;
}


// 检查文件类型是否有效
private bool CheckValidExt(string extendName)

{
bool isValid = false;
extendName = extendName.ToUpper();
string[] extends = AllowExtendName.Split('|');
foreach(string extend in extends)

{
if(extend == extendName)

{
isValid = true;
break;
}
}
return isValid;
}


添加产品#region 添加产品
private void Button1_Click(object sender, EventArgs e)

{
if (DropDownList1.SelectedValue == string.Empty || temp.Text == "" || DropDownList3.SelectedValue == "" || DropDownList4.SelectedValue == "")

{
HttpContext.Current.Response.Write("<script>alert('请选择下拉框选项!');</script>");
}
else

{
if(!PageValidate.IsNumber(TextBox4.Text))

{
MessageBox.Show(this.Page,"价格的格式不正确!");
return;
}
else if(!PageValidate.IsNumber(TextBox2.Text))

{
MessageBox.Show(this.Page,"数量的格式不正确!");
return;
}
else if (TextBox6.Text == "")

{
MessageBox.Show(this.Page,"请上传图片!!");
return;
}
FileInfo f = new FileInfo(StrPath);
if(!f.Exists)

{
CreateXML();
}
else

{
AddXML();
}
dbproduct.Add(TextBox1.Text,TextBox6.Text,Int32.Parse(DropDownList1.SelectedValue),Int32.Parse(temp.Text),Int32.Parse(DropDownList3.SelectedValue),Int32.Parse(DropDownList4.SelectedValue),Int32.Parse(TextBox4.Text),TextBox7.Text,Int32.Parse(TextBox2.Text));
HttpContext.Current.Response.Write("<script>alert('添加成功!');window.location='manageproduct.aspx';</script>");
}
}
#endregion

private void down2_bind(string id)

{
string mystr = "";
ds = dbproduct.GetDropDownList(Int32.Parse(id));
if(ds.Tables[0].Rows.Count != 0)

{
mystr += ",|请选择";
for(int i=0;i<ds.Tables[0].Rows.Count;i++)

{
mystr += "," + ds.Tables[0].Rows[i]["small_classid"].ToString() + "|" + ds.Tables[0].Rows[i]["name2"].ToString();
}
mystr = mystr.Substring(1);
}
HttpContext.Current.Response.Write(mystr);
HttpContext.Current.Response.End();
}


创建XML文档#region 创建XML文档
private void CreateXML()

{

// XmlDocument xDoc = new XmlDocument();
//
// XmlElement xElmntRoot;
// XmlElement xElmntFamily;
//
// //创建根节点
// xElmntRoot = xDoc.CreateElement("album");
// xDoc.AppendChild(xElmntRoot);
//
// //创建子节点
// xElmntFamily = (XmlElement) (xElmntRoot.AppendChild(xDoc.CreateElement("photo")));
//
// XmlElement xesub2=xDoc.CreateElement("url");
// xesub2.InnerText = TextBox6.Text.Trim();
// xElmntFamily.AppendChild(xesub2);
//
// XmlElement xesub3=xDoc.CreateElement("description");
// xesub3.InnerText = TextBox1.Text.Trim();
// xElmntFamily.AppendChild(xesub3);
//
// xDoc.Save(StrPath);
XmlDocument xDoc = new XmlDocument();
XmlProcessingInstruction xPI;
XmlElement xElmntRoot;
xPI = xDoc.CreateProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
xDoc.AppendChild(xPI);

xElmntRoot = xDoc.CreateElement("Photos");
xDoc.AppendChild(xElmntRoot);

xDoc.Save(StrPath);
}
#endregion


向已存在的XML文档里添加数据#region 向已存在的XML文档里添加数据
private void AddXML()

{
// XmlDocument xDoc = new XmlDocument();
// xDoc.Load(StrPath);
// XmlNode root=xDoc.SelectSingleNode("album");
//
// XmlElement xe1=xDoc.CreateElement("photo");
//
// XmlElement xesub2=xDoc.CreateElement("url");
// xesub2.InnerText = TextBox6.Text.Trim();
// xe1.AppendChild(xesub2);
//
// XmlElement xesub3=xDoc.CreateElement("description");
// xesub3.InnerText = TextBox1.Text.Trim();
// xe1.AppendChild(xesub3);
//
// root.AppendChild(xe1);
// xDoc.Save(StrPath);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(StrPath);
XmlNode root=xDoc.SelectSingleNode("Photos");
XmlDocumentFragment docFrag = xDoc.CreateDocumentFragment();
string insstr="<img src='../"+TextBox6.Text.Trim()+"' ssrc='../"+TextBox6.Text.Trim()+"' t='"+ TextBox1.Text + "' d='"+ TextBox7.Text +"'/>";
docFrag.InnerXml = insstr;
root.InsertAfter(docFrag,root.LastChild);
xDoc.Save(StrPath);
}
#endregion
}
}

posted @
2006-12-10 21:34
海浪~~
阅读(
509)
评论()
收藏
举报