📄 search.cs
字号:
/// Handles databind for the post body.
/// </summary>
///
// ********************************************************************/
private void DataBindPostBody(Object sender, EventArgs e) {
Label body = (Label) sender;
DataGridItem container = (DataGridItem) body.NamingContainer;
Post post = (Post) container.DataItem;
String postBody = Globals.FormatPostBody(post.Body);
// We only want to display some of the body
if (postBody.Length > MaxBodyLengthToDisplay) {
int whitespace = 0;
// Clip the body
postBody = postBody.Substring(0, MaxBodyLengthToDisplay);
// Find the last occurence of a space
whitespace = postBody.LastIndexOf(" ");
// Rebuild postBody string
postBody = postBody.Substring(0, whitespace) + " ...";
}
body.Text = postBody;
}
// *********************************************************************
// DataBindTitle
//
/// <summary>
/// Handles databind for the post title.
/// </summary>
///
// ********************************************************************/
private void DataBindTitle(Object sender, EventArgs e) {
HyperLink title = (HyperLink) sender;
DataGridItem container = (DataGridItem) title.NamingContainer;
Post post = (Post) container.DataItem;
title.Text = post.Subject;
title.NavigateUrl = Globals.UrlShowPost + post.PostID;
}
// *********************************************************************
// DataBindPostDetails
//
/// <summary>
/// Handles databind for the post details, e.g. who and at what time.
/// </summary>
///
// ********************************************************************/
private void DataBindPostDetails(Object sender, EventArgs e) {
HyperLink hyperlink;
Label label;
PlaceHolder postDetails = (PlaceHolder) sender;
DataGridItem container = (DataGridItem) postDetails.NamingContainer;
Post post = (Post) container.DataItem;
DateTime postDate;
// Construct post details
label = new Label();
label.CssClass = "normalTextSmall";
label.Text = "Posted by ";
postDetails.Controls.Add(label);
// Author
hyperlink = new HyperLink();
hyperlink.CssClass = "normalTextSmall";
hyperlink.Text = post.Username;
hyperlink.NavigateUrl = Globals.UrlUserProfile + post.Username;
postDetails.Controls.Add(hyperlink);
// Posted on date
postDate = post.PostDate;
label = new Label();
label.CssClass = "normalTextSmall";
// Is the post an announcement
if (post.IsAnnouncement) {
label.Text = " <b>(Announcement)</b>";
} else if (DateTime.Now < postDate) {
label.Text = " <b>(Pinned Post)</b>";
} else {
label.Text = " on " + post.PostDate.ToString();
}
postDetails.Controls.Add(label);
}
// *********************************************************************
// CreateSearchOptionsControl
//
/// <summary>
/// This function returns the control that displays the search options.
/// </summary>
/// <returns>Control used to render search options.</returns>
///
// ********************************************************************/
private Control CreateSearchOptionsControl() {
Table table;
TableRow tr;
TableCell td;
TableHeaderCell th;
Button button;
RequiredFieldValidator fieldValidator;
CheckBox checkbox;
// Create a new table and set default properties
table = new Table();
table.CssClass = "tableBorder";
table.CellPadding = 3;
table.CellSpacing = 1;
table.Width = Unit.Percentage(100);
// Column 1 - Search Text
tr = new TableRow();
th = new TableHeaderCell();
th.CssClass = "tableHeaderText";
th.HorizontalAlign = HorizontalAlign.Left;
// Search Text
th.Text = " Search";
tr.Controls.Add(th);
table.Controls.Add(tr);
tr = new TableRow();
tr.ID = "DisplayAdvancedSearchOptions";
td = new TableCell();
// Column 2 - Search Options
td.Wrap = false;
td.CssClass = "forumRow";
td.HorizontalAlign = HorizontalAlign.Center;
// Forums to search
forumsToSearch = new DropDownList();
forumsToSearch.ID = "forumsToSearch";
forumsToSearch.DataTextField = "Name";
forumsToSearch.DataValueField = "ForumID";
// Databind based on postback
if (!Page.IsPostBack) {
forumsToSearch.DataSource = Forums.GetAllForums();
forumsToSearch.DataBind();
}
// Add the "All Forums" option if the user is allowed to search all forums
if (AllowSearchAllForums)
forumsToSearch.Items.Insert(0, new ListItem("All Forums", "-1"));
td.Controls.Add(forumsToSearch);
// White space
td.Controls.Add(new LiteralControl(" "));
// Results / page
resultsPerPage = new DropDownList();
resultsPerPage.Items.Add(new ListItem("10 Results/Page", "10"));
resultsPerPage.Items.Add(new ListItem("25 Results/Page", "25"));
resultsPerPage.Items.Add(new ListItem("50 Results/Page", "50"));
td.Controls.Add(resultsPerPage);
// White space
td.Controls.Add(new LiteralControl(" "));
// Posts or Authors
searchFor = new DropDownList();
searchFor.Items.Add(new ListItem("Posts", "0"));
searchFor.Items.Add(new ListItem("Posted By", "1"));
td.Controls.Add(searchFor);
// White space
td.Controls.Add(new LiteralControl(" "));
// Match
matchingWords = new DropDownList();
matchingWords.Items.Add(new ListItem("Match All Words", "0"));
matchingWords.Items.Add(new ListItem("Match Any Words", "1"));
matchingWords.Items.Add(new ListItem("Match Exact Phrase", "2"));
td.Controls.Add(matchingWords);
tr.Controls.Add(td);
// Are we displaying advanced search options?
if (DisplayAdvancedSearch)
tr.Visible = true;
else
tr.Visible = false;
// Add row1 to table
table.Controls.Add(tr);
// Search Box
tr = new TableRow();
// Text to search for
td = new TableCell();
td.Wrap = false;
td.CssClass = "forumRow";
td.HorizontalAlign = HorizontalAlign.Center;
// Search text box
textToSearchFor = new TextBox();
textToSearchFor.ID = "textToSearchFor";
textToSearchFor.Columns = ContainingTextBoxColumns;
textToSearchFor.MaxLength = defaultContainingTextBoxMaxLength;
textToSearchFor.Width = Unit.Pixel(300);
td.Controls.Add(textToSearchFor);
// White space
td.Controls.Add(new LiteralControl(" "));
button = new Button();
button.Text = defaultSearchButtonText;
button.Click += new EventHandler(SearchButton_Click);
td.Controls.Add(button);
// White space
td.Controls.Add(new LiteralControl(" "));
// Add the advanced features checkbox
checkbox = new CheckBox();
checkbox.Text = "Hide Advanced Options";
checkbox.CssClass = "normalTextSmall";
checkbox.AutoPostBack = true;
checkbox.CheckedChanged += new System.EventHandler(DisplayAdvancedOptions);
td.Controls.Add(checkbox);
// Validate the search box
fieldValidator = new RequiredFieldValidator();
fieldValidator.CssClass = "validationWarningSmall";
fieldValidator.ControlToValidate = "textToSearchFor";
fieldValidator.Display = ValidatorDisplay.Dynamic;
fieldValidator.ErrorMessage = defaultRFVErrorMessage;
td.Controls.Add(fieldValidator);
tr.Controls.Add(td);
table.Controls.Add(tr);
return table;
}
// *********************************************************************
// DisplayAdvancedOptions
//
/// <summary>
/// Event handler raised when the checkbox for displaying advanced options
/// is clicked.
/// </summary>
/// <param name="sender">Checkbox that raised the event</param>
/// <param name="e">Event arguments from the checkbox</param>
private void DisplayAdvancedOptions(Object sender, EventArgs e) {
CheckBox checkbox;
Control advancedSearch;
// Get the checkbox that raised us
checkbox = (CheckBox) sender;
// Get the advanced search option table ro
advancedSearch = FindControl("DisplayAdvancedSearchOptions");
// Change setting based on status of checkbox
if (checkbox.Checked) {
advancedSearch.Visible = false;
DisplayAdvancedSearch = false;
} else {
advancedSearch.Visible = true;
DisplayAdvancedSearch = true;
}
}
// *********************************************************************
// SearchButton_Click
//
/// <summary>
/// This event handler is called when the user clicks on the submit button,
/// firing off the search. It resets the DataGrid's CurrentPageIndex to 0
/// and Displays the Search Results.
/// </summary>
// ***********************************************************************/
private void SearchButton_Click(Object sender, EventArgs e) {
// Exit if the page is invalid
if (!Page.IsValid)
return;
// set the datagrid to start paging at the beginning
searchResultsDataGrid.CurrentPageIndex = 0;
// set the number of Records per page
searchResultsDataGrid.PageSize = Convert.ToInt32(resultsPerPage.SelectedItem.Value);
// display the search results
DisplaySearchResults();
}
// *********************************************************************
// DisplaySearchResults
//
/// <summary>
/// This function grabs the dataset of search results for the particular page.
/// If there are records in the DataSet, it is bound to the DataGrid, else
/// a message is displayed explaining that no results were found.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -