im using this code but when i use filter, the grid pagination doesn't work. After submit, the result is displayed but when i click the second page it displays all the content not filtered. So it doesn't save the value from input field. Could this be an error
from the javascript below?
@{ var db = Database.Open("Northwind");
var query = "SELECT * FROM Customers WHERE CompanyName LIKE @0 AND Country LIKE @1";
var company = "%" + Request["company"] + "%";
var country = "%" + Request["country"] + "%";
var data = db.Query(query, company, country);
var columns = new[]{"CustomerID", "CompanyName", "ContactName", "Address", "City", "Country", "Phone"};
var grid = new WebGrid(data, columnNames: columns, rowsPerPage: 6);
}
<h1>Filter WebGrid</h1>
<form method="post">
<div id="grid">
Company Name: <input type="text" name="company" value="@Request["company"]" />
Country : <input type="text" name="country" value="@Request["country"]" />
I have changed the code, to display the content in a pagination style using this code below, now how can i use filter (search) input to display the result :
@{ var pageSize = 6;
var totalPages = 0;
var count = 0;
var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1;
var offset = (page -1) * pageSize;
var db = Database.Open("fiminfo");
var query = "SELECT Count(*) FROM books";
count = (int)db.QueryValue(query);
totalPages = count/pageSize;
if(count % pageSize > 0){
totalPages += 1;
}
query = "SELECT ISBN, Title, Author FROM books ORDER BY ISBN DESC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY";
var title = "%" + Request["title"] + "%"; var author = "%" + Request["author"] + "%";
var data = db.Query(query, offset, pageSize);
}
<form method="post">
<div id="grid">
<p>Filter
<input type="text" name="title" value="@Request["title"]" /> <input type="text" name="author" value="@Request["author"]" />
<input type="submit" value="search"/></p>
<table>
@foreach(var row in data){
<tr>
<td><img src="@Href("~/_layout/lib", @row.ISBN + ".png")"/></td>
<td><strong>@row.Title</strong><br/>
@row.Author<br/>
@row.ISBN</td>
</tr>
}
</table>
<p>
@{
for (var i = 1; i < totalPages + 1; i++){
<a href="/lib/@i">@i</a>
}
}
<span class="alignRight"> Page @page / @totalPages</span>
</p>
</div>
</form>
dow7
Member
738 Points
452 Posts
filterwebgrid not working
Apr 09, 2012 03:03 PM|LINK
im using this code but when i use filter, the grid pagination doesn't work. After submit, the result is displayed but when i click the second page it displays all the content not filtered. So it doesn't save the value from input field. Could this be an error from the javascript below?
@{ var db = Database.Open("Northwind"); var query = "SELECT * FROM Customers WHERE CompanyName LIKE @0 AND Country LIKE @1"; var company = "%" + Request["company"] + "%"; var country = "%" + Request["country"] + "%"; var data = db.Query(query, company, country); var columns = new[]{"CustomerID", "CompanyName", "ContactName", "Address", "City", "Country", "Phone"}; var grid = new WebGrid(data, columnNames: columns, rowsPerPage: 6); } <h1>Filter WebGrid</h1> <form method="post"> <div id="grid"> Company Name: <input type="text" name="company" value="@Request["company"]" /><input type="submit" /> @grid.GetHtml( tableStyle : "table", alternatingRowStyle : "alternate", headerStyle : "header", columns: grid.Columns( grid.Column("CustomerID", "ID"), grid.Column("CompanyName", "Company"), grid.Column("ContactName", "Contact"), grid.Column("Address"), grid.Column("City"), grid.Column("Country"), grid.Column("Phone") ) ) </div> </form> @section script{ <script type="text/javascript"> $(function(){ $('th a, tfoot a').live('click', function() { $('form').attr('action', $(this).attr('href')).submit(); return false; }); }); </script> }dow7
Member
738 Points
452 Posts
Re: filterwebgrid not working
Apr 09, 2012 07:39 PM|LINK
I have changed the code, to display the content in a pagination style using this code below, now how can i use filter (search) input to display the result :
@{ var pageSize = 6; var totalPages = 0; var count = 0; var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1; var offset = (page -1) * pageSize; var db = Database.Open("fiminfo"); var query = "SELECT Count(*) FROM books"; count = (int)db.QueryValue(query); totalPages = count/pageSize; if(count % pageSize > 0){ totalPages += 1; } query = "SELECT ISBN, Title, Author FROM books ORDER BY ISBN DESC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY"; var title = "%" + Request["title"] + "%"; var author = "%" + Request["author"] + "%"; var data = db.Query(query, offset, pageSize); } <form method="post"> <div id="grid"> <p>Filter <input type="text" name="title" value="@Request["title"]" /> <input type="text" name="author" value="@Request["author"]" /> <input type="submit" value="search"/></p> <table> @foreach(var row in data){ <tr> <td><img src="@Href("~/_layout/lib", @row.ISBN + ".png")"/></td> <td><strong>@row.Title</strong><br/> @row.Author<br/> @row.ISBN</td> </tr> } </table> <p> @{ for (var i = 1; i < totalPages + 1; i++){ <a href="/lib/@i">@i</a> } } <span class="alignRight"> Page @page / @totalPages</span> </p> </div> </form>SonicMan
Participant
1472 Points
228 Posts
Re: filterwebgrid not working
Apr 11, 2012 01:49 AM|LINK
Hi
Use the filter in this way:
http://weblogs.thinktecture.com/cnagel/2011/05/filter-and-display-data-with-aspnet-web-pages-using-microsoft-webmatrix.html
Hope it work.
dow7
Member
738 Points
452 Posts
Re: filterwebgrid not working
Apr 11, 2012 02:45 PM|LINK
Im not using dropdown list,
but if i write this code:
if(IsPost){ var title = "%" + Request["title"] + "%"; var author = "%" + Request["autor"] + "%"; query += " WHERE Title LIKE @0 AND Author LIKE @1"; }i get this error : There was an error parsing the query. [ Token line number = 1,Token line offset = 99,Token in error = WHERE ]
var data = db.Query(query, offset, pageSize);