A simple way to perform asynchronous calls to the server from the client is by using the ICallbackEventHandler interface in .NET 2.0.
1.) Create a class and specify ICallbackEventHandler interface:
public partial class TestClass : System.Web.UI.UserControl, ICallbackEventHandler
2.) Implement ICallbackEventHandler.RaiseCallbackEvent and ICallbackEventHandler.GetCallbackResult()
private string results;
void ICallbackEventHandler.RaiseCallbackEvent(string argument)
{
string[] list;
int i=0;
string id= argument.ToLower();
DataView dv = RetrieveData(argument);
list = new string[dv.Table.Rows.Count];
foreach (DataRow row in dv.Table.Rows)
{
list[i] = row[1].ToString() + "," + row[2].ToString();
i++;
}
results = String.Join("-", list);
return;
}
string ICallbackEventHandler.GetCallbackResult()
{
return results;
}
3.) Create async call-back function for use on client side.
private string GetDataRefreshScript()
{
StringBuilder sb = new StringBuilder("function RefreshResults(data){");
sb.AppendLine(Page.ClientScript.GetCallbackEventReference(this, "data", "UpdateData","null", "UpdateData_Error", true));
sb.AppendLine("}");
sb.AppendLine(GetUpdateDataScript());
return sb.ToString();
}
4.) Create client script to handle data retreived from server through async call.
private string GetUpdateDataScript()
{
StringBuilder sb = new StringBuilder("function UpdateData(response, context) {");
sb.AppendLine("var id = document.getElementById(\"" + ResultsGrid.ClientID + "\");");
sb.AppendLine("if(id != null){");
sb.AppendLine("var rows = new Array();");
sb.AppendLine("rows = response.split(\"-\");");
sb.AppendLine("for(j=0; j sb.AppendLine("var tmpRow = rows[j];");
sb.AppendLine("addResultGridRow(id, tmpRow);\r\n}}}");
//Error
sb.AppendLine("function UpdateData_Error(response, context) {");
sb.AppendLine("var err = document.getElementById(\"" + errorMessage.ClientID +"\");");
sb.AppendLine("if(err != null){");
sb.AppendLine("err.innerHTML = 'Error processing data.';");
sb.AppendLine("err.style.visibility = 'visible';\r\n}}");
return sb.ToString();
}
5.) The last thing that needs done is triggering the async call from client
btn.Attributes.Add("onmousedown", "RefreshResults('" + data + "')");
Monday, December 8, 2008
Communicate data between two browser windows
To communicate data between two browser windows "window.opener" can be of assistance. window.opener returns a reference to the window that opened the current window. So this mock function below should be inserted into the opened window.
Make note that "__datatarget" and "__datawindow" are two variables that were defined in the parent window.
function SetData(data)
{
if ((window.opener != null)
&& (!window.opener.closed)
&& (window.opener.__datatarget != null))
{
var changed = (window.opener.__datatarget.value != data);
window.opener.__dataetarget.value = employee;
window.opener.__datawindow.value = null;
if (changed)
window.opener.__datatarget.fireEvent("onchange");
}
window.close();
}
Make note that "__datatarget" and "__datawindow" are two variables that were defined in the parent window.
function SetData(data)
{
if ((window.opener != null)
&& (!window.opener.closed)
&& (window.opener.__datatarget != null))
{
var changed = (window.opener.__datatarget.value != data);
window.opener.__dataetarget.value = employee;
window.opener.__datawindow.value = null;
if (changed)
window.opener.__datatarget.fireEvent("onchange");
}
window.close();
}
Thursday, October 30, 2008
_PendingCallbacks[...].async is null or not an object BUG
_PendingCallbacks[...].async is null or not an object error can be related to a bug in the javascript that VS creates for you to deal with callbacks. You can alleviate PendingCallback errors by checking all of your own javascript for any variables named i. Replace them and you should be golden.
Friday, October 3, 2008
Show and hide a panel with javascript
* Make note of the Z-INDEX on the row. This will allow the visible panel to appear above other items on the page.
In the HTML
<tr style="Z-INDEX: 10; POSITION: absolute">
<td>
<asp:Panel Runat="server" ID="ThePanel" BorderWidth="1px" BorderStyle=Solid>
<asp:Literal Runat="server" ID="TheResults"></asp:Literal>
</asp:Panel>
</td>
</tr>
In the js file
function ShowPanel(target)
{
if (target != null)
{
target.style.display = '';
}
}
function HidePanel(target)
{
if (target != null)
{
target.style.display = 'none';
}
}
In the code behind
protected override void OnPreRender(EventArgs e)
{
this.TheLabel.Attributes.Add("onmouseover", "ShowPanel("+this.ThePanel.ID+");");
this.TheLabel.Attributes.Add("onmouseout", "HidePanel("+this.ThePanel.ID+");");
this.ThePanel.Style.Add("display", "none");
base.OnPreRender (e);
}
In the HTML
<tr style="Z-INDEX: 10; POSITION: absolute">
<td>
<asp:Panel Runat="server" ID="ThePanel" BorderWidth="1px" BorderStyle=Solid>
<asp:Literal Runat="server" ID="TheResults"></asp:Literal>
</asp:Panel>
</td>
</tr>
In the js file
function ShowPanel(target)
{
if (target != null)
{
target.style.display = '';
}
}
function HidePanel(target)
{
if (target != null)
{
target.style.display = 'none';
}
}
In the code behind
protected override void OnPreRender(EventArgs e)
{
this.TheLabel.Attributes.Add("onmouseover", "ShowPanel("+this.ThePanel.ID+");");
this.TheLabel.Attributes.Add("onmouseout", "HidePanel("+this.ThePanel.ID+");");
this.ThePanel.Style.Add("display", "none");
base.OnPreRender (e);
}
Thursday, September 25, 2008
DataSet / DataTable Primary Key and using DataRow.Find
It's quick and easy to find items in a DataTable through setting the primary key. This method can be used to easily populate a CheckBoxList
DataSet ds = //populate dataset
if(ds != null && ds.Tables.Count > 0)
{
GroupCBL.DataSource = ds;
GroupCBL.DataTextField="textField";
GroupCBL.DataBind();
DataColumn[] dc = new DataColumn[1];
dc[0] = ds.Tables[0].Columns["key"];
ds.Tables[0].PrimaryKey = dc;
foreach(ListItem item in GroupCBL.Items)
{
DataRow row = ds.Tables[0].Rows.Find(item.Value);
item.Selected = Convert.ToBoolean(row["selected"]);
}
}
DataSet ds = //populate dataset
if(ds != null && ds.Tables.Count > 0)
{
GroupCBL.DataSource = ds;
GroupCBL.DataTextField="textField";
GroupCBL.DataBind();
DataColumn[] dc = new DataColumn[1];
dc[0] = ds.Tables[0].Columns["key"];
ds.Tables[0].PrimaryKey = dc;
foreach(ListItem item in GroupCBL.Items)
{
DataRow row = ds.Tables[0].Rows.Find(item.Value);
item.Selected = Convert.ToBoolean(row["selected"]);
}
}
Friday, September 12, 2008
Restart a remote machine.
Having problems with the remote machine you are using? Don't want to leave home to head to the office for a restart? Simple cmd can take care of it.
C:\Documents and Settings\unplug1.6>shutdown -m \\Hum -r -y
shutdown is the command you're looking for, "-r" is to specify a restart instead of total shutdown. "-m \\Hum" specifies the remote computer you are trying to restart. Below are a few other arguments.
Usage:
shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]
C:\Documents and Settings\unplug1.6>shutdown -m \\Hum -r -y
shutdown is the command you're looking for, "-r" is to specify a restart instead of total shutdown. "-m \\Hum" specifies the remote computer you are trying to restart. Below are a few other arguments.
No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without warning
-d [u][p]:xx:yy The reason code for the shutdown
Usage:
shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]
Monday, September 8, 2008
Refreshing the project failed. Unable to retrieve folder information from the server.
I've been working with Visual Studios 2003 and have had the error "Refreshing the project failed. Unable to retrieve folder information from the server." pop up as I'm loading the project. I normally ignore it and go about my business. I finally tired of seeing the pop up and hit the interweb for answers.
To correct the problem simply delete the "VSWebCache" from the "\Documents and Settings\[Username]" directory.
Subscribe to:
Posts (Atom)