What I try to do is making a simple list filter tool for SharePoint 2010 lists.
What I have so far: I made a VisualWebPart (VWP) named "ListFilter", which should update the view of the list (see code below). I add the VWP to the already existing WebPartPage of a List in SharePoint.
The problem: After the first button click (btnSearchButton_Click
), the list view does not update, after the second click, it does update. I already tried (as I read about page lifecycle) to put the filterList(String sw)
into Page_Load
or Page_Init
, but without success. Maybe the cause of this is that the list renders again on postback (but as far as I know I can't influence that?!?!)
My goal: Update the list view on the first click on the button.
My Code for VWP "ListFilter":
ListFilterUserControl.ascx:
<asp:TextBox ID="tbxSearchWord" runat="server" ToolTip="Suchwort eingeben" />
<asp:Button ID="btnSearchButton" runat="server" Text="Suche" ToolTip="Suche starten" onclick="btnSearchButton_Click" />
ListFilterUserControl.ascx.cs (code behind):
// Class Variables:
SPWeb currentWeb = SPContext.Current.Web;
String mySearchWord;
SPList list;
SPView view;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearchButton_Click(object sender, EventArgs e)
{
this.mySearchWord = (String.IsNullOrEmpty(this.tbxSearchWord.Text) == false) ? this.tbxSearchWord.Text.ToString() : "No searchword entered";
filterList(this.mySearchWord);
}
public void filterList(String sw)
{
list = currentWeb.Lists["Listname"];
view = list.Views[list.GetUncustomizedViewByBaseViewId(1).Title.ToString()];
String query = "<Where><Contains><FieldRef Name='fieldName'/><Value Type='Text'>" + sw + "</Value></Contains></Where>";
view.Query = query;
currentWeb.AllowUnsafeUpdates = true;
view.Update();
currentWeb.AllowUnsafeUpdates = false;
}
Mayby someone could point me to the right direction ... I'm not that experienced in programming C# / .Net. Thank you very much in advance!
EDIT
I added Response.Redirect(Request.Url.ToString());
to the filterList(String sw)
-Method which reloads the page after the button click. It works now as intended, but I think it's not really a nice solution. If someone knows something better, please let me know.