⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 worksedit.aspx.cs

📁 一个简单的Demo
💻 CS
字号:
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 Surance.DB4ODAL;
using Surance.DBUtility;
using Db4objects.Db4o;
using Surance.Utility;
using System.IO;

public partial class Back_WorksEdit : System.Web.UI.Page
{
    private string listUrl = "WorksList.aspx";
    private string categoryModule = "Works";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string title = Request["Title"];
            if (!string.IsNullOrEmpty(title))
            {
                BindCategory();
                BindData(title.Trim());
                ViewState["Title"] = title.Trim();
            }
        }
    }
    private void BindData(string title)
    {
        IObjectContainer db = DB4OHelper.DB;
        try
        {
            Works item = new Works(db);
            Works target = (Works)item.GetOne(title);
            txtTitle.Text = target.Title;
            txtContent.Text = target.Content;
            ddlCategory.SelectedValue = target.Category;
            Calendar1.Text = target.CreateTime.ToString();
            txtHit.Text = target.Hit.ToString();
            txtUrlAddress.Text = target.Url;

            string imageUrl = Tools.GetPicturePath() + target.Picture;
            Image1.ImageUrl = imageUrl;
            ViewState["Image"] = target.Picture;


            cdBeginDate.Text = target.BeginDate.ToShortDateString();
            txtCustmerName.Text = target.CustomerName;
            txtDevelopCycles.Text = target.DevelopCycles;
            txtSourceCodeAddress.Text = target.SourceCodeAddress;
        }
        catch
        {
        }
        finally
        {
            db.Close();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Valid())
        {
            IObjectContainer db = DB4OHelper.DB;

            //string picture = "";
            try
            {
                Works item = new Works();
                item.Db = db;

                item = (Works)item.GetOne(ViewState["Title"].ToString());
                item.Db = db;

               this.GetFromControls(item);
                




                item.Update();
                Message.Show("Edit Success!");
            }
            catch (Exception e1)
            {
                Response.Write(e1.ToString());
                Message.Show("Edit Fail!");
            }
            finally
            {
                db.Close();
            }
        }
    }
    private Works GetFromControls(Works item)
    {
        //Works item = new Works();
        
        string title = txtTitle.Text.Trim();
        string content = txtContent.Text.Trim();
        string cateogry = ddlCategory.SelectedValue.Trim();
        string url = txtUrlAddress.Text.Trim();
        item.Title = title;
        item.Content = content;
        item.CreateTime = DateTime.Parse(Calendar1.Text);
        item.Hit = int.Parse(txtHit.Text);
        item.Category = cateogry;
        item.Url = url;
        item.BeginDate = DateTime.Parse(cdBeginDate.Text);


        if (FileUpload1.HasFile)
        {
            string picture = Tools.GetPictureName() + Path.GetExtension(FileUpload1.PostedFile.FileName);
            item.Picture = picture;
            string path = Tools.GetPictureSavePath() + picture;

            FileUpload1.SaveAs(path);
        }
        else
        {
            item.Picture = ViewState["Image"].ToString();
        }

        item.CustomerName = txtCustmerName.Text.ToString();
        item.DevelopCycles = txtDevelopCycles.Text.ToString();
        item.SourceCodeAddress = txtSourceCodeAddress.Text.ToString();
        return item;
    }
    private bool Valid()
    {
        bool result = true;
        if (result)
        {
            if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                result = false;
                Message.Show("Title is not empty!");
            }
            else
            {
                result = true;
            }
        }



        if (result)
        {
            if (string.IsNullOrEmpty(txtContent.Text.Trim()))
            {
                result = false;
                Message.Show("Content is not empty!");
            }
            else
            {
                result = true;
            }
        }
        return result;
    }


    protected void btnList_Click(object sender, EventArgs e)
    {
        Response.Redirect(listUrl);
    }

    private void BindCategory()
    {
        IObjectContainer db = DB4OHelper.DB;
        try
        {
            Category c = new Category(db);
            ddlCategory.DataSource = c.GetByModule(categoryModule);
            ddlCategory.DataBind();
        }
        catch
        {

        }
        finally
        {
            db.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -