Пример #1
0
        /// <summary>
        /// 提交按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonOK_Click(object sender, System.EventArgs e)
        {
            //构造book信息哈希表
            Hashtable ht = new Hashtable();

            ht.Add("BookName", TextBoxBookName.Text.Trim());
            ht.Add("CategoryId", DropDownListCategory.SelectedValue);
            ht.Add("Price", TextBoxPrice.Text.Trim());
            ht.Add("Publisher", TextBoxPublisher.Text.Trim());
            ht.Add("PublishDate", TextBoxPublishDate.Text.Trim());
            ht.Add("Author", TextBoxAuthor.Text.Trim());
            ht.Add("PageNum", TextBoxPageNum.Text.Trim());
            ht.Add("Description", TextBoxDescription.Text.Trim());

            //图片名,以当前时间为文件名,确保文件名没有重复
            string uploadName  = InputPictureFile.Value.Trim();
            string pictureName = "";

            if (uploadName != "")
            {
                int    idx    = uploadName.LastIndexOf(".");
                string suffix = uploadName.Substring(idx);//文件后缀
                //Ticks属性的值为自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。
                pictureName = System.DateTime.Now.Ticks.ToString() + suffix;
                ht.Add("PictureUrl", pictureName);
            }

            ht.Add("SaleCount", 1);

            //添加图书,如果数据类型不正确,给出提示.
            ArrayList WarningMessageList = new ArrayList();

            LabelWarningMessage.Text = "";
            if (BookHelper.Add(ht, ref WarningMessageList) == false)
            {
                LabelWarningMessage.Text = "<font color=red>";
                foreach (string item in WarningMessageList)
                {
                    LabelWarningMessage.Text += item + "<br>";
                }
                LabelWarningMessage.Text += "</font>";
            }

            //上传图片到目录"\BookPics\"中
            else
            {
                if (uploadName != "")
                {
                    string path = Server.MapPath(".\\BookPics\\");
                    InputPictureFile.PostedFile.SaveAs(path + pictureName);
                }
                Response.Redirect("BookList.aspx");
            }
        }
Пример #2
0
        private void Button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("请填写完整的新增信息", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return;
            }
            Book model = new Book();

            model.name   = textBox1.Text;
            model.author = textBox2.Text;
            model.press  = textBox3.Text;
            if (BookHelper.Add(model))
            {
                ShowDataGird(); MessageBox.Show("添加成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }