Doloto

Doloto

Meant for Web 2.0 Apps that use a lot of JavaScript, Doloto will speed up the download time of all the JavaScript by basically removing anything that isn’t initially needed and replacing it with stubs. It will then download in the background or wait until it is first called. The result, a seemingly much faster Web App for the user.

For more info have a look at the Doloto page on Microsofts Research site or head over to the download page on MSDN.

Advertisements

Introduction to Real Time Search

The Real Time Search this article relates to is a dll that comes included in the Web Client Software Factory one of Microsoft’s patterns and practices downloads. What it can do and what this example will demonstrate is adding the ability cause a post back that will refresh an update panel as the users types into a search box. This can give a great effect and make a web app really user friendly in scenarios like searching photo’s, emails or any general list.

First late me state though that this is in no way the most optimal way program. In most scenarios you could built a better result using something like JSON as their will be a lot less data transfer, which is also a general reason to avoid update panels. However this is also very quick and very easy to implement, not to mention if you’ve ever used update panels before you already know 90% of what’s needed. This can also only work in situations where you have a good search that is going to return the result quickly, rather than leaving the user sitting there trying to work out why nothing’s happening and where the search button has gone.

Implementing Real Time Search

For this example I will be filtering a table from a DB based on the search criteria and refreshing a Grid View with the results. I will be using a normal C# Web Site project with the Adventure Works sample DB from Microsoft. DB connection will be done using LINQ to EntityFramework, however there is no need to use this it is just my preference for the example.

First off set up you’re website and db and make sure both are working with no problems. As results will be displayed in an Update Panel, get one of these along with a script manager added to your page, so it looks something like this:

<form id=”form1″ runat=”server”>
<div>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
</asp:ScriptManager>
<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>
<ContentTemplate></ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

Next let’s get the search working in the normal method, so I’m going to create my Entity Model and add a textbox and gridview to show the results. Again you can connect and show your results however you want. You should now have something like this in your aspx file:

<form id=”form1″ runat=”server”>
<div>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
</asp:ScriptManager>

<
asp:TextBox ID=”txtSearch” runat=”server” OnTextChanged=”TextChanged” Text=””></asp:TextBox>

<
asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>
<ContentTemplate>

<
asp:LinqDataSource ID=”LinqDataSource1″ runat=”server” onselecting=”LinqDataSource1_Selecting”>
</asp:LinqDataSource>

<
asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataSourceID=”LinqDataSource1″>
<Columns>
<asp:BoundField DataField=”ProductID” HeaderText=”ProductID”
ReadOnly=”True” SortExpression=”ProductID” />
<asp:BoundField DataField=”Name” HeaderText=”Name”
ReadOnly=”True” SortExpression=”Name” />
<asp:BoundField DataField=”ProductNumber” HeaderText=”ProductNumber” ReadOnly=”True” SortExpression=”ProductNumber” />
<asp:BoundField DataField=”Color” HeaderText=”Color”
ReadOnly=”True” SortExpression=”Color” />
<asp:BoundField DataField=”SafetyStockLevel” HeaderText=”SafetyStockLevel”
ReadOnly=”True” SortExpression=”SafetyStockLevel” />
<asp:BoundField DataField=”ReorderPoint” HeaderText=”ReorderPoint”
ReadOnly=”True” SortExpression=”ReorderPoint” />
</Columns>
</asp:GridView>
</ContentTemplate>

</asp:UpdatePanel>
</div>
</form>

And this in your code behind:


protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
Model.AdventureWorks2008Entities AdventureWorkds = new Model.AdventureWorks2008Entities();
var products = from p in AdventureWorkds.Product
where p.Name.Contains(txtSearch.Text)
select p;
e.Result = products;
}

Next its time to add the Real Time Search. Make sure you have the dll downloaded (you may need to compile the download to get it) and add it to your bin folder. Add the following to your page in the relevant places:

<%@ Register Assembly=”RealTimeSearch” Namespace=”RealTimeSearch” TagPrefix=”cc1″ %>

<cc1:RealTimeSearchMonitor ID=”RealTimeSearchMonitor1″ runat=”server” AssociatedUpdatePanelID=”UpdatePanel1″>
<ControlsToMonitor>
<cc1:ControlMonitorParameter EventName=”TextChanged” TargetID=”txtSearch” />
</ControlsToMonitor>
</cc1:RealTimeSearchMonitor>

Important things to notice here are the AssociatedUpdatePanelId which tells the control what it has to refresh and the controls to monitor section which sets what the control to watch is called and the event name that will be fired when the post back is created. You will now need to corresponding control in your code behind like so:

protected void TextChanged(object sender, EventArgs e)
{
GridView1.DataBind();
}

Run the site and  you should find that the grid view now updates as you type (all be it with a slight delay).

To improve you can add other controls like update progress to show something happening which will help with any delays in displaying the results.

Refreshing an UpdatePanel using JavaScript

Update panels provide a really easy way to add partial page refresh’s to a web page in order to give a more user friendly experience that’s more like a common desktop App. By default a button within that update panel will cause a post back and so long as no elements outside of the update panel are affected in the post back only it will refresh. However what do you do if you need to refresh a panel based on something outside of the update panel. One option is just to place a hidden button within the Update Panel and trigger that to post back, however this appears a little hacky. The solution is to use JavaScript and the _doPostBack method.

The following bit of code is all you really need:

<script type=”text/javascript”>
function refreshUpdatePannel(param) {

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._doPostBack(‘<%=UpdatePanel1.ClientID %>’, param);
prm.add_endRequest(refreshUpdatePanelComplete);
}

function refreshUpdatePanelComplete() {
//add code here for when the update pannel is refreshed
}
</script>

All you need to add is something to call the function.

The second parameter is optional and provides a way of passing some information back to the server to let it know what has just happened on the page.

When the update panel has finished refreshing the refreshUpdatePanelComplete function will fire so more JavaScript can be added if needed e.g. To reset whatever just happened on the page that caused the initial postback.