📄 editbook.cs
字号:
namespace ASPNET.StarterKit.Communities.Books {
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ASPNET.StarterKit.Communities;
//*********************************************************************
//
// EditBook Class
//
// Represents the Edit Book page. Enables users to edit books.
//
//*********************************************************************
public class EditBook : ContentEditPage {
string _skinFileName = "Books_AddBook.ascx";
string _sectionContent = "ASPNET.StarterKit.Communities.Books.BookSection";
TextBox txtBookTitle;
TopicPicker dropTopics;
TextBox txtBookAuthor;
TextBox txtBookISBN;
TextBox txtBookPrice;
TextBox txtBookPurchaseLink;
TextBox txtBookBriefDescription;
HtmlTextBox txtBookFullDescription;
HtmlInputFile txtImageFile;
DisplayBookImage imgBookImage;
TextBox txtBookPublicationDate;
TextBox txtBookPublisher;
TextBox txtBookPublisherLink;
//*********************************************************************
//
// ContentPageID Property
//
// Stores the ID of the content being edited in View State
//
//*********************************************************************
int ContentPageID {
get {
return (int)ViewState["ContentPageID"];
}
set { ViewState["ContentPageID"] = value; }
}
//*********************************************************************
//
// ImageID Property
//
// Represents the Image ID of the book being edited.
//
//*********************************************************************
int ImageID {
get { return (int)ViewState["ImageID"]; }
set { ViewState["ImageID"] = value; }
}
//*********************************************************************
//
// SkinLoadBook
//
// The skin load event happens after a page skin has been loaded.
// Here, we grab the necessary controls from the page skin.
//
//*********************************************************************
void SkinLoadBook(Object s, SkinLoadEventArgs e) {
// Find the Form so that we can change the EncType of the post.
HtmlForm form = (HtmlForm)Page.FindControl("PageForm");
form.Enctype="multipart/form-data";
// Find Book Title
txtBookTitle = (TextBox)GetControl(e.Skin, "txtBookTitle" );
// Find Topic Picker
dropTopics = (TopicPicker)GetControl(e.Skin, "dropTopics");
// Find Author
txtBookAuthor = (TextBox)GetControl(e.Skin, "txtBookAuthor" );
// Find ISBN
txtBookISBN = (TextBox)GetControl(e.Skin, "txtBookISBN" );
// Find Price
txtBookPrice = (TextBox)GetControl(e.Skin, "txtBookPrice" );
// Find Purchase Link
txtBookPurchaseLink = (TextBox)GetControl(e.Skin, "txtBookPurchaseLink" );
//Find Brief Description
txtBookBriefDescription = (TextBox)GetControl(e.Skin, "txtBookBriefDescription" );
// Find Full Description
txtBookFullDescription= (HtmlTextBox)GetControl(e.Skin, "txtBookFullDescription" );
// Find the image upload button
txtImageFile = (HtmlInputFile)GetControl(e.Skin, "txtImageFile");
// Find the Book image
imgBookImage = (DisplayBookImage)GetControl(e.Skin, "imgBookImage");
// Find the Publication Date
txtBookPublicationDate = (TextBox)GetControl(e.Skin, "txtBookPublicationDate");
// Find the Publisher
txtBookPublisher = (TextBox)GetControl(e.Skin, "txtBookPublisher");
// Find the Publisher Link
txtBookPublisherLink = (TextBox)GetControl(e.Skin, "txtBookPublisherLink");
}
//*********************************************************************
//
// OnLoad Method
//
// Assign previous content values to edit form.
//
//*********************************************************************
override protected void OnLoad(EventArgs e) {
string imagePath;
if (!Page.IsPostBack) {
// Get the ContentPageID from QueryString
ContentPageID = Int32.Parse(Page.Request.QueryString["id"]);
// Get the book info
BookInfo _bookInfo = (BookInfo)BookUtility.GetBookInfo(objUserInfo.Username, ContentPageID);
// Store the original Image ID
ImageID = _bookInfo.ImageID;
// Populate the controls
EnsureChildControls();
// Assign value to topics
dropTopics.SelectedTopicID = _bookInfo.TopicID;
txtBookTitle.Text = _bookInfo.Title;
txtBookAuthor.Text = _bookInfo.BookAuthor;
txtBookISBN.Text = _bookInfo.ISBN;
txtBookPurchaseLink.Text = _bookInfo.PurchaseLink;
txtBookPrice.Text = _bookInfo.Price.ToString("n2");
txtBookBriefDescription.Text = _bookInfo.BriefDescription;
txtBookFullDescription.Text = _bookInfo.FullDescription;
txtBookPublicationDate.Text = _bookInfo.PublicationDate.ToString("d");
txtBookPublisher.Text = _bookInfo.Publisher;
txtBookPublisherLink.Text = _bookInfo.PublisherLink;
if (_bookInfo.ImageID != -1) {
imgBookImage.Visible = true;
imagePath = ImageUtility.BuildImagePath(_bookInfo.ImageID, _bookInfo.ImageName);
imgBookImage.ImageUrl = CommunityGlobals.CalculatePath(imagePath);
}
else
imgBookImage.Visible = false;
}
}
//*********************************************************************
//
// SubmitBook Method
//
// This method is raised by clicking the Edit button in the Edit
// form.
//
//*********************************************************************
private void SubmitBook(Object s, EventArgs e) {
if (Page.IsValid) {
// Get Topic
int topicID = -1;
if (objSectionInfo.EnableTopics)
topicID = Int32.Parse(dropTopics.SelectedItem.Value);
// Edit the book
BookUtility.EditBook
(
objUserInfo.Username,
objSectionInfo.ID,
ContentPageID,
txtBookTitle.Text,
txtBookBriefDescription.Text,
txtBookFullDescription.Text,
txtBookAuthor.Text,
txtBookISBN.Text,
txtBookPurchaseLink.Text,
Decimal.Parse(txtBookPrice.Text),
DateTime.Parse(txtBookPublicationDate.Text),
txtBookPublisher.Text,
txtBookPublisherLink.Text,
topicID
);
// Update Image
if (ImageID == -1)
ImageUtility.AddSectionImage(ContentPageID, txtImageFile.PostedFile);
else
ImageUtility.UpdateSectionImage(ImageID, txtImageFile.PostedFile);
// Redirect back to Book Page
Context.Response.Redirect(CommunityGlobals.CalculatePath(String.Format("{0}.aspx", ContentPageID)));
}
}
//*********************************************************************
//
// EditBook Constructor
//
// Calls the base SkinnedCommunityControl constructor
// and assigns the default page skin. Also checks whether
// current user has permissions to edit.
//
//*********************************************************************
public EditBook() : base() {
// Assign a default skin file name
SkinFileName = _skinFileName;
// Specify Section Content
SectionContent = _sectionContent;
// Wire-up event handlers
this.SkinLoad += new SkinLoadEventHandler(SkinLoadBook);
this.Submit += new SubmitEventHandler(SubmitBook);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -