我正在尝试创建一个搜索工具。它可以搜索并对我的搜索按钮点击做出反应。我添加了一些用户可以用来设置一些设置的单选按钮。我希望每次检查单选按钮时webpart都会进行新搜索,但这是我无法工作的。
我曾尝试做this answer中所说的但是没有帮助。
public class HelloWorld : Microsoft.SharePoint.WebPartPages.WebPart
{
Button btn;
TextBox textBx;
string _myProperty = "";
DataTable resultsDataTable = new DataTable();
RadioButton exactSearch;
RadioButton wildSearch;
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category("My Property Group")]
[WebDisplayName("MyProperty")]
[WebDescription("Meaningless Property")]
public string MyProperty
{
get
{
if (_myProperty == null)
{
_myProperty = "Hello SharePoint";
}
return _myProperty;
}
set { _myProperty = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
textBx = new TextBox();
textBx.Text = "";
btn = new Button();
btn.Text = "Search";
exactSearch = new RadioButton();
exactSearch.GroupName = "searchSetting";
exactSearch.Checked = false;
wildSearch = new RadioButton();
wildSearch.GroupName = "searchSetting";
wildSearch.Checked = true;
//Events
btn.Click += new EventHandler(btn_Click);
wildSearch.CheckedChanged += new EventHandler(wildSearch_CheckedChanged);
//Adding to controls
this.Controls.Add(btn);
this.Controls.Add(textBx);
this.Controls.Add(exactSearch);
this.Controls.Add(wildSearch);
}
void wildSearch_CheckedChanged(object sender, EventArgs e)
{
search();
}
void btn_Click(object sender, EventArgs e)
{
search();
}
protected override void Render(HtmlTextWriter writer)
{
//Title
writer.Write("Super Lookup Service <br/>");
//Search block
textBx.RenderControl(writer);
writer.Write(" ");
btn.RenderControl(writer);
writer.Write("<br/>");
writer.Write("Exact Search ");
exactSearch.RenderControl(writer);
writer.Write(" ");
writer.Write("Wild Search ");
wildSearch.RenderControl(writer);
writer.Write("<br/><br/>");
//Search results
if (resultsDataTable.Rows.Count > 0)
{
writer.Write("<b> {0} Results:</b><br/>", resultsDataTable.Rows.Count);
foreach (DataRow row in resultsDataTable.Rows)
{
writer.Write("<a href=\"{0}\">{1}</a> ({2}) - size: {3}<br/>", row["path"].ToString(), row["title"].ToString(), row["author"].ToString(), row["Size"].ToString());
}
}
else
{
writer.Write("<b>0 Results:</b>");
}
}
private void search()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
FullTextSqlQuery query = new FullTextSqlQuery(site);
string queryText = "";
if (wildSearch.Checked == true)
{
queryText = "SELECT title, path, author, Size from Scope() where title LIKE '%" + textBx.Text + "%' order by title asc ";
}
else if (exactSearch.Checked == true)
{
queryText = "SELECT title, path, author, Size from Scope() where title = '" + textBx.Text + "' order by title asc ";
}
query.QueryText = queryText;
query.ResultTypes = ResultType.RelevantResults;
query.RowLimit = 3000;
ResultTableCollection resultTables = query.Execute();
if (resultTables.Count > 0)
{
ResultTable relevantResults = resultTables[ResultType.RelevantResults];
resultsDataTable.Load(relevantResults, LoadOption.OverwriteChanges);
}
}
}
}
希望你能帮助我。
BR
答案 0 :(得分:1)
在OnCreateChildControl上创建单选按钮时,必须将单选按钮的AutoPostBack属性设置为true,否则它不会回发并且不会触发事件。
wildSearch.AutoPostBack = true;