动态生成js

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
public partial class ManageMusic_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {//当页面载入的时候建立表
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("~/ManageMusic/musicList.xml"));//读取xml数据
            if (ds.Tables.Contains("music")==false)
            {
                Response.Write("Data不存在!");
                CreateDataTable();
            }
            Session["ds"] = ds;
            fill();
        }
    }
    private void fill()
    {
        DataSet ds = (DataSet)Session["ds"];
        if (ds.Tables.Contains("music") == true)
        {
            GridView2.DataSource = ds;
            GridView2.DataBind();
        }
    }
    protected void btnOutPut_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["ds"];
        if (ds.Tables.Contains("music") && ds.Tables[0].Rows.Count > 0)
        {
            ds.WriteXml(Server.MapPath("~/ManageMusic/musicList.xml"));
            DataSet dsRead = new DataSet();
            dsRead.ReadXml(Server.MapPath("~/ManageMusic/musicList.xml"));
            if (dsRead.Tables[0].Rows.Count > 0)
            {
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < dsRead.Tables[0].Rows.Count; i++)
                {
                    str.Append("mkList(\"" + dsRead.Tables[0].Rows[i]["url"] + "\",\"");
                    str.Append(dsRead.Tables[0].Rows[i]["name"]);
                    if (i == dsRead.Tables[0].Rows.Count - 1)
                    {
                        str.Append("\");");
                    }
                    else
                    {
                        str.Append("\");\n");
                    }
                }
                try
                {
                    using (StreamWriter sw = new StreamWriter(Server.MapPath("../js/xzr.js"), false, System.Text.Encoding.GetEncoding("GB2312")))
                    {
                        sw.WriteLine(str);
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch
                {
                    CommonFile.CymFunction.MessageBox.AlertBack("输出js失败!");
                }
            }
        }
        else
        {
            CommonFile.CymFunction.MessageBox.AlertBack("暂无数据!");
        }
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["ds"];
        if (ds != null)
        {
            if (ds.Tables.Contains("music")&&ds.Tables[0].Rows.Count>0)
            {
                string flag = "";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["name"].Equals(this.txtNameAdd.Text))
                    {
                        flag = "true";
                    }
                    else
                    {
                        flag = "false";
                    }
                }
                if (flag == "false")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["id"] = int.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["id"].ToString()) + 1;
                    dr["name"] = this.txtNameAdd.Text;
                    dr["person"] = this.txtPersonAdd.Text;
                    dr["url"] = this.txtUrlAdd.Text;
                    dr["dtt"] = DateTime.Now;
                    ds.Tables[0].Rows.Add(dr);
                }
                if (flag == "true")
                {
                    Response.Write("<script>alert('歌曲名称经存在!')</script>");
                }
            }
            else
            {
                DataTable dt = CreateDataTable();
                DataRow dr = dt.NewRow();
                dr["id"] = 0;
                dr["name"] = this.txtNameAdd.Text;
                dr["person"] = this.txtPersonAdd.Text;
                dr["url"] = this.txtUrlAdd.Text;
                dr["dtt"] = DateTime.Now;
                dt.Rows.Add(dr);
                ds.Tables.Clear();
                ds.Tables.Add(dt);
            }
            Session["ds"] = ds;
            fill();
        }
    }
    public DataTable CreateDataTable()
    {
        DataSet ds = new DataSet("musics");
        DataTable dt = new DataTable("music");
        DataColumn newColumn; // 创建新列
        newColumn = dt.Columns.Add("id", Type.GetType("System.Int32"));
        dt.Columns.Add(new DataColumn("person", typeof(string)));
        dt.Columns.Add(new DataColumn("name", typeof(string)));
        dt.Columns.Add(new DataColumn("url", typeof(string)));
        dt.Columns.Add(new DataColumn("dtt", typeof(DateTime)));
        newColumn.AutoIncrement = true;     // 自动增加
        newColumn.AutoIncrementSeed = 1;      // 起始为1
        newColumn.AutoIncrementStep = 1;      // 步长为1
        newColumn.AllowDBNull = false;
        return dt;
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      
        DataSet ds = (DataSet)Session["ds"];
        DataTable dt = ds.Tables[0];
        if (e.CommandName == "Del")
        {
            string id = e.CommandArgument.ToString();
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["id"].ToString() == id)
                        {
                            dt.Rows.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
            Session["ds"] = ds;
            fill();
    }
    protected void btnUpdate_Click1(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["ds"];
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            GridViewRow gvr = GridView2.Rows[i];
            string lblName = ((TextBox)gvr.Cells[1].FindControl("txtPerson")).Text.Replace("'", "@_~!");
            string lbPerson = ((TextBox)gvr.Cells[2].FindControl("txtPerson")).Text.Replace("'", "@_~!");
            string lbUrl = ((TextBox)gvr.Cells[3].FindControl("txtUrl")).Text.Replace("'", "@_~!");
            ds.Tables[0].Rows[i]["name"] = lblName;
            ds.Tables[0].Rows[i]["person"] = lbPerson;
            ds.Tables[0].Rows[i]["url"] = lbUrl;
            Session["ds"] = ds;
        }
        fill();
    }
}
posted @ 2008-05-18 22:18  熊忠荣  阅读(574)  评论(0编辑  收藏  举报