📄 search.cs
字号:
/// </summary>
// ***********************************************************************/
private void DisplaySearchResults() {
// get the search results
ToSearchEnum ToSearch;
SearchWhatEnum SearchWhat;
// determine what we want to search (post body or poster's username?)
switch (Convert.ToInt32(searchFor.SelectedItem.Value)) {
case 1:
ToSearch = ToSearchEnum.PostsBySearch;
break;
default:
ToSearch = ToSearchEnum.PostsSearch;
break;
}
// determine how we want to search
switch (Convert.ToInt32(matchingWords.SelectedItem.Value)) {
case 1:
SearchWhat = SearchWhatEnum.SearchAnyWord;
break;
case 2:
SearchWhat = SearchWhatEnum.SearchExactPhrase;
break;
default:
SearchWhat = SearchWhatEnum.SearchAllWords;
break;
}
// Clean up search text
textToSearchFor.Text = textToSearchFor.Text.Replace("'","''");
// Perform the Search
PostCollection posts = AspNetForums.Search.PerformSearch(ToSearch, SearchWhat,
Convert.ToInt32(forumsToSearch.SelectedItem.Value),
textToSearchFor.Text, searchResultsDataGrid.CurrentPageIndex+1, searchResultsDataGrid.PageSize);
// if the dataset is empty, show an appropriate message
if (posts.Count == 0) {
((Label) FindControl("lblNoResults")).Visible = true;
((Panel) FindControl("panelSearchResults")).Visible = false;
}
else {
// we have results, bind it to the DataGrid.
((Panel) FindControl("panelSearchResults")).Visible = true;
((Label) FindControl("lblNoResults")).Visible = false;
// see if we have more records to show...
searchResultsDataGrid.VirtualItemCount = posts.TotalRecordCount;
searchResultsDataGrid.DataSource = posts;
searchResultsDataGrid.DataBind();
// display how many results we got and what page we're viewing.
((Label) FindControl("lblInformation")).Text = String.Format("{0:d}", posts.TotalRecordCount) + " matches found. Viewing Page " + (searchResultsDataGrid.CurrentPageIndex+1) + " of " + searchResultsDataGrid.PageCount;
}
}
// *********************************************************************
// OnPreRender
//
/// <summary>
/// This event handler checks to see if its the first time the page has
/// been loaded - if it is, the data is bound.
/// </summary>
// ***********************************************************************/
protected override void OnPreRender(EventArgs e) {
// see if this is our first visit to the page
if (!Page.IsPostBack) {
DropDownList forumsToSearch = (DropDownList) FindControl("forumsToSearch");
// If we found the control
if (forumsToSearch != null) {
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"));
if (CheckForPassedInParameters())
DisplaySearchResults();
}
}
// *********************************************************************
// CheckForPassedInParameters
//
/// <summary>
/// This function checks to see if any querystring parameters were passed
/// in. Since we want to be able to invoke the search via a direct link, we need
/// to allow querystring/form parameters to trip the search. If a querystring/
/// form parameter Contents is passed in, the search is automagically performed.
/// </summary>
// ***********************************************************************/
private bool CheckForPassedInParameters() {
if (Context.Request.Params["SearchText"] != null) {
// we have parameter values!
textToSearchFor.Text = Context.Request.Params["SearchText"].ToString();
if (Context.Request.Params["SearchFor"] != null)
try {
searchFor.Items.FindByValue(Context.Request.Params["SearchFor"].ToString()).Selected = true;
} catch (Exception exp) {
exp = null;
searchFor.Items[0].Selected = true;
}
else
searchFor.Items[0].Selected = true;
if (Context.Request.Params["Forum"] != null)
try {
forumsToSearch.Items.FindByValue(Context.Request.Params["Forum"].ToString()).Selected = true;
} catch (Exception exp) {
exp = null;
forumsToSearch.Items[0].Selected = true;
}
else
forumsToSearch.Items[0].Selected = true;
if (Context.Request.Params["Results"] != null)
try {
resultsPerPage.Items.FindByValue(Context.Request.Params["Results"].ToString()).Selected = true;
} catch (Exception exp) {
exp = null;
resultsPerPage.Items[1].Selected = true;
}
else
resultsPerPage.Items[1].Selected = true;
if (Context.Request.Params["Matching"] != null)
try {
matchingWords.Items.FindByValue(Context.Request.Params["Matching"].ToString()).Selected = true;
} catch (Exception exp) {
exp = null;
matchingWords.Items[0].Selected = true;
}
else
matchingWords.Items[0].Selected = true;
// Set the Records per page
searchResultsDataGrid.CurrentPageIndex = 0;
searchResultsDataGrid.PageSize = Convert.ToInt32(resultsPerPage.SelectedItem.Value);
return true;
}
return false; // no querystring values
}
/// <summary>
/// The message to display when no results are found for a search.
/// </summary>
[
Category("Style"),
Description("The message to display when no results are found for a search."),
DefaultValue("No records were found matching your search query.")
]
public String NoRecordsFoundMessage {
get {
if (ViewState["noRecsFoundMsg"] == null) return defaultNoRecordsMessage;
return (String) ViewState["noRecsFoundMsg"];
}
set { ViewState["noRecsFoundMsg"] = value; }
}
/// <summary>
/// The title that should appear at the top of the search page.
/// </summary>
[
Category("Style"),
Description("The title that should appear at the top of the search page."),
DefaultValue("Search")
]
public String SearchTitle {
get {
if (ViewState["searchTitle"] == null) return defaultSearchTitle;
return (String) ViewState["searchTitle"];
}
set { ViewState["searchTitle"] = value; }
}
/// <summary>
/// Used to display advanced search options.
/// </summary>
[
Description("Display advanced search options."),
DefaultValue("Search")
]
public bool DisplayAdvancedSearch {
get {
if (ViewState["displayAdvancedSearch"] == null) return true;
return (bool) ViewState["displayAdvancedSearch"];
}
set { ViewState["displayAdvancedSearch"] = value; }
}
/// <summary>
/// This property determines whether or not the user can search all forums or must search
/// from a particular forum. It defaults to true.
/// </summary>
/// <remarks>For sites with a large number of forums and a large number of posts with a large
/// user base performing many searches, setting this property to false may help the overall
/// performance of the site.</remarks>
[
Category("Style"),
Description("Whether or not a user can search all forums."),
DefaultValue(true)
]
public bool AllowSearchAllForums {
get {
if (ViewState["allowSearchAllForums"] == null) return defaultAllowSearchAllForums;
return (bool) ViewState["allowSearchAllForums"];
}
set { ViewState["allowSearchAllForums"] = value; }
}
/// <summary>
/// Specifies the text for the button to launch the search.
/// </summary>
[
Category("Style"),
Description("Specifies the text for the button to launch the search."),
DefaultValue(" Search ")
]
public String SearchButtonText {
get {
if (ViewState["searchButtonTxt"] == null) return defaultSearchButtonText;
return (String) ViewState["searchButtonTxt"];
}
set { ViewState["searchButtonTxt"] = value; }
}
/// <summary>
/// Specifies how many columns the Containing text box should have.
/// </summary>
[
Category("Style"),
Description("Specifies how many columns the Containing text box should have."),
DefaultValue(50)
]
public int ContainingTextBoxColumns {
get {
if (ViewState["containingTextBoxColumns"] == null) return defaultContainingTextBoxColumns;
return (int) ViewState["containingTextBoxColumns"];
}
set { ViewState["containingTextBoxColumns"] = value; }
}
/// <summary>
/// Determines how many characters of the Body should be displayed. This property setting only
/// takes affect if the ShowBody property is set to true.
/// <seealso cref="ShowBody"/>
/// </summary>
[
Category("Style"),
Description("Determines how many characters of the Body should be displayed."),
DefaultValue(500)
]
public int MaxBodyLengthToDisplay {
get {
if (ViewState["mblToDisplay"] == null) return defaultMaxBodyLength;
return (int) ViewState["mblToDisplay"];
}
set { ViewState["mblToDisplay"] = value; }
}
/// <summary>
/// Determines whether or not the Body of the message should be displayed in the search results.
/// </summary>
[
Category("Style"),
Description("Determines whether or not the Body of the message should be displayed."),
DefaultValue(true)
]
public bool ShowBody {
get {
if (ViewState["showBody"] == null) return defaultShowBody;
return (bool) ViewState["showBody"];
}
set { ViewState["showBody"] = value; }
}
/// <summary>
/// Specifies the UI and stylistic settings of the search results Pager. The Pager allows the
/// end user to navigate between multiple pages of search results.
/// </summary>
[
Category("Style"),
Description("Specifies the UI and stylistic settings of the search results Pager.")
]
public SearchPagerStyle PagerStyle {
get { return datagridPagerStyle; }
}
/*******************************************************/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -