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

📄 search.cs

📁 微软的.NET论坛的源代码(COOL!!!)
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetForums;
using AspNetForums.Components;
using System.ComponentModel;

namespace AspNetForums.Controls.Search {

    // *********************************************************************
    //  Search
    //
    /// <summary>
    /// This Web control presents the Web controls for searching the Web site.  Additionally,
    /// the search results are displayed via this control.  This Web control allows the user
    /// to visit the page and enter search criteria, and also for search criteria to be passed
    /// through the QueryString or Form collections to have the search results automatically
    /// displayed.
    /// </summary>
    /// 
    // ********************************************************************/ 
    [
    ParseChildren(true)
    ]
    public class Search : WebControl, INamingContainer {
        DataGrid searchResultsDataGrid;
        DropDownList resultsPerPage;
        DropDownList searchFor;
        DropDownList matchingWords;
        DropDownList forumsToSearch;
        TextBox textToSearchFor;
        Panel searchResultsPanel;

        const bool defaultAllowSearchAllForums = true;			            // the default choice on whether the user can search all forums or not
        const bool defaultShowBody = true;
        const String defaultSearchTitle = "Search";			                // the default title to show at the top of the search page
        const int defaultContainingTextBoxMaxLength = 250;		            // the default max # of characters for the Containing textbox
        const int defaultContainingTextBoxColumns = 50;		                // the default # of cols. in the Containing textbox
        const String defaultSearchButtonText = " Search ";		            // the default text for the submit button
        const int defaultMaxBodyLength = 500;					            // how many characters to show of a body, at most
		
        const String defaultNoRecordsMessage = "No records were found matching your search query.";
        private readonly String defaultRFVErrorMessage = Globals.HtmlNewLine + "You must enter at least one search term to search on.";

        
        int userTimeZoneOffset = Globals.DBTimezone;                        // store the user's time zone setting

        SearchPagerStyle datagridPagerStyle = new SearchPagerStyle();       // create the DataGrid for displaying the results

        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// This event handler adds the children controls.
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected override void CreateChildControls() {
            User user;

            // Get the timezone offset
            if (Page.Request.IsAuthenticated) {
                user = Users.GetUserInfo(Context.User.Identity.Name, true);
                userTimeZoneOffset = user.Timezone;
            }

            // Create the table used to display the search options
            Controls.Add(CreateSearchOptionsControl());

            // create the label that will display the "No Records Found" message, if needed
            Label lblTmp = new Label();
            lblTmp.CssClass = "normalTextSmallBold";
            lblTmp.ID = "lblNoResults";
            lblTmp.Visible = false;
            lblTmp.Text = NoRecordsFoundMessage;
            Controls.Add(lblTmp);

            // Create the panel that displays the search results
            CreateSearchResultsPanel();

        }
		
		
        // *********************************************************************
        //  DataGrid_PageIndexChange
        //
        /// <summary>
        /// This event handler fires when the user clicks the Next or Prev page
        /// links on the search page.  It simply assigns the new Page Index to
        /// the DataGrid's CurrentPageIndex and rebinds the data
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DataGrid_PageIndexChange(Object sender, DataGridPageChangedEventArgs e) {
            searchResultsDataGrid.CurrentPageIndex = e.NewPageIndex;		// set the page index
		    DisplaySearchResults();		// rebind the data
        }

        // *********************************************************************
        //  CreateSearchResultsPanel
        //
        /// <summary>
        /// This function creates the panel to display the search results in.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void CreateSearchResultsPanel() {
            Table table;
            TableHeaderCell th;
            TableRow tr;
            TableCell td;
            Label searchResultsText;

            // Create the panel
            searchResultsPanel = new Panel();
            searchResultsPanel.ID = "panelSearchResults";
            searchResultsPanel.Visible = false;
            
            // Create and set some properties on the table
            table = new Table();
            table.CellPadding = 3;
            table.CellSpacing = 1;
            table.Width = Unit.Percentage(100);
            table.CssClass = "tableBorder";

            // Display the title
            tr = new TableRow();
            th = new TableHeaderCell();
            th.CssClass = "tableHeaderText";
            th.HorizontalAlign = HorizontalAlign.Left;
            searchResultsText = new Label();
            searchResultsText.ID = "lblInformation";
            th.Controls.Add(searchResultsText);
            tr.Controls.Add(th);
            table.Controls.Add(tr);

            // Add some white space
            searchResultsPanel.Controls.Add(new LiteralControl("<P></P>"));

            // Add the search results
            tr = new TableRow();
            td = new TableCell();
            
            // add the dataGrid
            searchResultsDataGrid = new DataGrid();
            searchResultsDataGrid.ID = "searchResultsDataGrid";
            searchResultsDataGrid.CellPadding = 5;
            searchResultsDataGrid.CellSpacing = 0;
            searchResultsDataGrid.BorderWidth = 0;
            searchResultsDataGrid.AllowCustomPaging = true;
            searchResultsDataGrid.AllowPaging = true;
            searchResultsDataGrid.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid_PageIndexChange);
			
            // Set the pager style attributes
            searchResultsDataGrid.PagerStyle.CopyFrom(datagridPagerStyle);
            searchResultsDataGrid.PagerStyle.CssClass = "searchPager";
            searchResultsDataGrid.PagerStyle.Position = datagridPagerStyle.Position;
            searchResultsDataGrid.PagerStyle.Mode = datagridPagerStyle.Mode;
            searchResultsDataGrid.PagerStyle.NextPageText = datagridPagerStyle.NextPageText;
            searchResultsDataGrid.PagerStyle.PageButtonCount = datagridPagerStyle.PageButtonCount;
            searchResultsDataGrid.PagerStyle.PrevPageText = datagridPagerStyle.PrevPageText;
            searchResultsDataGrid.PagerStyle.Visible = datagridPagerStyle.Visible;
            searchResultsDataGrid.PagerStyle.HorizontalAlign = HorizontalAlign.Right;

            // Set the item styles
            searchResultsDataGrid.ItemStyle.CssClass = "searchItem";
            searchResultsDataGrid.AlternatingItemStyle.CssClass = "searchAlternatingItem";

            // Set some display properties
            searchResultsDataGrid.Width = Unit.Percentage(100.0);
            searchResultsDataGrid.AutoGenerateColumns = false;
            searchResultsDataGrid.ShowHeader = false;

            // Add a template column to the Columns property - this column is used to display count
            TemplateColumn searchResultsCountColumn = new TemplateColumn();
            searchResultsCountColumn.ItemTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildCountItemTemplate));
            searchResultsCountColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            searchResultsCountColumn.ItemStyle.VerticalAlign = VerticalAlign.Top;
            searchResultsDataGrid.Columns.Add(searchResultsCountColumn);

            // Add a template column to the Columns property - this column is used to display results
            TemplateColumn searchResultsColumn = new TemplateColumn();
            searchResultsColumn.ItemTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildItemTemplate));
            searchResultsDataGrid.Columns.Add(searchResultsColumn);

            // Add the datagrid with our search results
            td.Controls.Add(searchResultsDataGrid);
            tr.Controls.Add(td);
            table.Controls.Add(tr);

            // Add the table
            searchResultsPanel.Controls.Add(table);

            this.Controls.Add(searchResultsPanel);
        }

        // *********************************************************************
        //  BuildItemTemplate
        //
        /// <summary>
        /// This function is called to create the template for the datalist.
        /// It calls BeginBuildItemTemplate, which creates the DataBoundLiteralControl
        /// needed.  It then adds this control to the parser object
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void BuildItemTemplate(Control _ctrl) {

            // add the DataBoundLiteralControl to the parser
            System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(_ctrl));
            __parser.AddParsedSubObject(BeginBuildItemTemplate());
        }

        // *********************************************************************
        //  BuildCountItemTemplate
        //
        /// <summary>
        /// This function is called to create a template for the datalist.
        /// This column displays the count for items returned.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void BuildCountItemTemplate(Control _ctrl) {
            Label label;

            // add the DataBoundLiteralControl to the parser
            System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(_ctrl));

            label = new Label();
            label.CssClass = "normalTextSmallBold";
            label.DataBinding += new System.EventHandler(DataBindResultCount);

            __parser.AddParsedSubObject(label);
        }

        // *********************************************************************
        //  BeginBuildItemTemplate
        //
        /// <summary>
        /// This function creates a new DataBoundLiteralControl and populates
        /// the Static Strings.  Additionally, the DataBinding event handler is
        /// wired up to a local event handler.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private Control BeginBuildItemTemplate() {
            PlaceHolder placeHolder;
            PlaceHolder postDetails;
            HyperLink hyperlink;
            Label postBody;

            // Create the PlaceHolder control
            postDetails = new PlaceHolder();

            // Display the title of the post
            hyperlink = new HyperLink();
            hyperlink.CssClass = "normalTextSmallBold";
            hyperlink.DataBinding += new System.EventHandler(DataBindTitle);
            postDetails.Controls.Add(hyperlink);

            // Line break
            postDetails.Controls.Add(new LiteralControl("<br>"));

            // Display post details
            placeHolder = new PlaceHolder();
            placeHolder.DataBinding += new System.EventHandler(DataBindPostDetails);
            postDetails.Controls.Add(placeHolder);

            // Are we displaying the post body?
            if (ShowBody) {
                // Line break
                postDetails.Controls.Add(new LiteralControl("<br>"));
            
                // 
                postBody = new Label();
                postBody.CssClass = "normalTextSmaller";
                postBody.DataBinding += new System.EventHandler(DataBindPostBody);
                postDetails.Controls.Add(postBody);
            }

            return postDetails;
        }

        // *********************************************************************
        //  DataBindResultCount
        //
        /// <summary>
        /// Handles databind for counting results.
        /// </summary>
        /// 
        // ********************************************************************/ 
        private void DataBindResultCount(Object sender, EventArgs e) {
            int count = 0;
            Label countLabel = (Label) sender;
            DataGridItem container = (DataGridItem) countLabel.NamingContainer;

            count = searchResultsDataGrid.PageSize * searchResultsDataGrid.CurrentPageIndex;
            count+= + (container.ItemIndex + 1);
            countLabel.Text = count.ToString() + ".";
        }
        

        // *********************************************************************
        //  DataBindPostBody
        //
        /// <summary>

⌨️ 快捷键说明

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