I am getting error while implementing Signalr in asp.net C# like:
Uncaught Error: SignalR: Connection has not been fully initialized. Use .start().done() or .start().fail() to run logic after the connection has started.
I am implementing signalr in visual studio 2010 and I have implemented same code before but that is working fine. Please help me to resolve this error.
My code is like this:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="SignalRTest._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-1.0.0-rc1.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
$(function () {
var IWannaChat = $.connection.myChatHub;
IWannaChat.client.addMessage = function (message) {
$('#listMessages').append('<li>' + message + '</li>');
};
$("#SendMessage").click(function () {
IWannaChat.server.send($('#txtMessage').val());
});
$.connection.hub.start();
});
</script>
<div>
<input type="text" id="txtMessage" />
<input type="button" id="SendMessage" value="broadcast" />
<ul id="listMessages">
</ul>
</div>
</form>
</body>
</html>
my code behind is like this:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Microsoft.AspNet.SignalR.Hubs
Imports Microsoft.AspNet.SignalR
Imports System.Net
Namespace SignalRChat
<HubName("myChatHub")> _
Public Class LetsChat
Inherits Hub
Public Sub send(message As String)
message = "User : " + Dns.GetHostName() + " User :" + System.Security.Principal.WindowsIdentity.GetCurrent().Name + " Message : " + message
Clients.Caller.addMessage(message)
End Sub
End Class
End Namespace
my global.asax is like this:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
RouteTable.Routes.MapHubs()
End Sub
I would write this, to set the hook on the button once connection started:
$(function () {
var IWannaChat = $.connection.myChatHub;
IWannaChat.client.addMessage = function (message) {
$('#listMessages').append('<li>' + message + '</li>');
};
$.connection.hub.start().done(function() {
$("#SendMessage").click(function () {
IWannaChat.server.send($('#txtMessage').val());
});
});
}
Related
I need to call the service and retrieve the resulted data in call back function from it but I am getting js error:
Uncaught ReferenceError: InvoiceHTMLService is not defined.
Please help - below are my pages and class
My Aspx page
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="InvoiceHTML.aspx.vb" Inherits="WebApplication2.InvoiceHTML" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Web Service call from client-side JavaScript</title>
<script type="text/javascript">
function SendRequest() {
debugger;
InvoiceHTMLService.GetBillInvoiceHtmlData();
}
function OnComplete(arg) {
alert(arg);
}
function OnTimeOut(arg) {
alert("timeOut has occured");
}
function OnError(arg) {
alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service/InvoiceHTMLService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input type="text" value="" id="MyTextBox" />
<input type="button" value="Send Request to the Web Service"
id="RequestButton" onclick="return SendRequest()" />
</div>
</form>
</body>
</html>
my asmx page
Imports System.Web.Script.Services
Imports System.Web.Services
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.IO
Imports ClassLibrary1
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService()> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ScriptService()> _
Public Class InvoiceHTMLService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetBillInvoiceHtmlData() As Object
Dim objDAOBill As DAOInvoice
Dim Obj As Object
objDAOBill = New DAOInvoice()
Obj = objDAOBill.GetBillInvoiceHtmlData()
Return Obj
End Function
End Class
I got the answer for it thanks everyone, I am posting the answer so it may can help others the service call from client side can be done by using the complete class name present in .asmx page.In my Case
<%# WebService Language="vb" CodeBehind="InvoiceHTMLService.asmx.vb" Class="App.InvoiceHTMLService" %>
So I have to call the service in following way:
function SendRequest() {
App.InvoiceHTMLService.GetBillInvoiceHtmlData(callback);
}
I've been banging my head on this for a few days now, and seem to be no closer than before. Roughly it seems like the clientContext.executeQueryAsync is not working properly for creating multiple items in sharepoint. Here is the basic design. I created a web part in my share point site that connects to an html page. The page is on the home.aspx of the site, that has the following content added to it via a content page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" x-undefined="" />
<script type="text/javascript" src="/apps/dre/SiteAssets/Javascript/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="/apps/dre/SiteAssets/Javascript/BatchControl.js"></script>
<script type="text/javascript" src="/apps/dre/SiteAssets/Javascript/jquery.SPServices-2014.01.min.js"></script>
<script type="text/javascript" src="/apps/dre/SiteAssets/Javascript/PageLoader.js"></script>
<script type="text/javascript" src="/apps/dre/SiteAssets/Javascript/BatchExecution.js"></script>
<script type="text/javascript">ExecuteOrDelayUntilScriptLoaded(function(){loadmasterpage();}, "sp.js")</script>
<title>Home Page</title>
</head>
<body>
<div id="batch" style="visibility:hidden">
<button id="batchcontrol" onclick="ExecuteBatchControl()">Create DRE Records</button>
</div>
</body>
</html>
So it's a very simple piece of HTML. The PageLoader library loads the button after SP.js is loaded.
function loadmasterpage()
{
$(document).ready(function() {
//alert("Here");
$('#batch').css('visibility','visible');
});
}
That's all the web content does in the Home.aspx page. It shows a simple HTML button. Now comes the baffling mystery. So if I click the button the BatchControl library executes the following code:
function ExecuteBatchControl()
{
createListItem();
}
function createListItem() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Discrepancy Report');
for(x=0;x<4;x++){
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', "B2Batch");
oListItem.update();
clientContext.load(oListItem);
}
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
//alert('Item created: ' + oListItem.get_id());
alert('Success');
// $("input[Title='Caller ID']").val("");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
It creates the records, but the onQuerySucceeded function never executes. If you remove the for...loop, it will execute, and it works fine. Why doesn't the batch coding work. It's almost like there is a stability issue with it, but I can't say. No one seems to know the answer here. Is it because I hooked up my html page to a web content web part? I am completely lost and baffled. PLEASE I NEED HELP!!!!!!
I have a problem using Ext.Net 2.5 and App.Direct:
Ext.onReady(function() {
App.direct.GetAll({
success: function (result) {
currentMessageId = result;
}
});
});
The problem exist in body onload too.
When I Call the direct method in Ext.onReady it gives me this error: "Cannot Call GetAll of undefined."
But, when I call it instead a click button handler it works without problem.
So, the question is:
When App.direct is defined?
In the Page Sources you could see the following.
Ext.onReady(function () {
Ext.ns("App.direct");
Ext.apply(App.direct, {
TestDirectMethod: function (config) {
return Ext.net.DirectMethod.request("TestDirectMethod", Ext.applyIf(config || {}, {}));
}
});
});
It is how a DirectMethod is rendered to a browser.
As you can see it is inside an Ext.onReady function. So, your onReady function is executed before that.
You can force rendering of our onReady function before your one using a ResourcePlaceHolder.
<%# Page Language="C#" %>
<%# Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
[DirectMethod]
public void TestDirectMethod()
{
X.Msg.Alert("DirectMethod", "Hello from Server!").Show();
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
<ext:ResourcePlaceHolder runat="server" Mode="Script" />
<script>
Ext.onReady(function() {
App.direct.TestDirectMethod();
});
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
</form>
</body>
</html>
Here is a description of possible options for a ResourcePlaceHolder's Mode.
I have the following coffeescript code to generate and alert box:
show_alert = () ->
alert("Hello! I am an alert box!")
which compiles to:
(function() {
var show_alert;
show_alert = function() {
return alert("Hello! I am an alert box!");
};
}).call(this);
in my html I have the following
<input onclick='show_alert()' type='button' value='Show alert box' />
However, no alert box shows? The following is the html copied from the browser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Test Rails Application</title>
<style type='text/css'>.application h1 {
color: lime; }
</style>
<script type='text/javascript'>(function() {
var show_alert;
show_alert = function() {
return alert("Hello! I am an alert box!");
};
}).call(this);
</script>
</head>
<body>
<h1>Hello from applicaiton.html.haml</h1>
<div class='application'><h1>Hello World</h1>
<input onclick='show_alert()' type='button' value='Show alert box' />
</div>
</body>
</html>
Why am I not able to get an alert box to show up?
Your problem is that the generated javascript code is in another scope. You have to solve this
by either adding the -b parameter to the coffeescript compiler or export your function
explicitly via
root = exports ? this
root.show_alert = () -> alert("Hello! I am an alert box!")
For more information about the exporting and scope issue have a look at https://stackoverflow.com/a/4215132/832273
I created a working jsfiddle with the above coffeescript code
I found two ways to solve this issue
FIRST add # before function name
#say_hi = () ->
$(alert('Hello!!!'))
SECOND at the end of coffee file add
window["say_hi"] = say_hi
in your coffeescrpt code, try to save the function to window: window["show_alert"] = show_alert
i have default.aspx page and one user control.
usercontrol is having following code for multiple file uploads.
now the problem is when i add a file for upload that current context file is not giving me any value it is still zero i guess because it is rendering from user control.
what should i do?
My Usercontrol UPLOAD.ASCX
<%# Control Language="C#" AutoEventWireup="true" CodeFile="FileUpload.ascx.cs" Inherits="FileUpload" %>
<script type="text/javascript" src="_scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
var i = 1;
$(document).ready(function () {
$("#addfile").click(function () {
$("#dvfiles").append("<input name=" + i + "fu type=file /><a href=#>remove</a><br>");
i++;
});
$("#dvfiles a").live('click', function () {
$(this).prev("input[type=file]").remove();
$(this).remove();
});
});
$(document).submit(function () {
var flag = true;
$("#dvfiles input[type=file]").each(function () {
if ($(this).val() == "") {
$(this).css("background", "Red");
flag = false;
}
});
return flag;
});
</script>
<div id="Fileuploader">
Attach a file..<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
onclick="btnUpload_Click" />
</div>
UPLOAD.ASCX.CS
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection filecolln = Request.Files;
//here i don't get values of current files.
// this is zero. because of this following if condition failed
//please help here
if (filecolln.Count > 0)
{
for (int i = 0; i < filecolln.Count; i++)
{
HttpPostedFile file = filecolln[i];
if (file.ContentLength > 0)
{
file.SaveAs(ConfigurationManager.AppSettings["FilePath"] + System.IO.Path.GetFileName(file.FileName));
}
}
lblMessage.Text = "Uploaded Successfully!";
}
else
{
lblMessage.Text = "No files selected!";
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
Default.aspx code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<%# Register TagPrefix="ucFileuploader" tagName="Fileuploader" src="FileUpload.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ucFileuploader:Fileuploader ID="Fileuploder" runat="server" />
</div>
</form>
</body>
</html>
The problem is that you are using javascript an id names.
so when you have control with id addfile in .aspx it is rendered as is,
but when you have control with id addfile in user control with id Fileuploader,
than the rendered id is Fileuploader_addfile,
so change id name in java script with the proper id.
To chechk what is name of rendered id, open page in browser, open source of the page and find you element and copy id into java script.
Change all ids in java script with rendered id names.
I would suspect it could be this line:
<script type="text/javascript" src="_scripts/jquery-1.4.1.min.js"></script>
Which should be:
<script type="text/javascript" src="/_scripts/jquery-1.4.1.min.js"></script>