Changing EventSouce event name not working in asp.net - javascript

i have an asp.net mvc application with knockout.js
i'm using HTML5 EventSource for push messages
c#
private static void UpdateOnlineUsers()
{
var ou = Clients.Select(c => new { UserName = c.User.Name, Time = c.DateTime.ToString("G") }).ToList();
Clients.ForEach(c =>
{
c.Stream.WriteLine("data:" + new JavaScriptSerializer().Serialize(ou) + "\n\n");
});
}
and javascript is
eventSource.addEventListener('message', function (e) {
if (e.data.length) {
var json = JSON.parse(e.data);
if (json.Text) {
messages.push(json);
}
else onlineUsers(json);
}
}, false);
which is working fine.
but i want to add a specific name for the event so i added new line in c# method like
Clients.ForEach(c =>
{
c.Stream.WriteLine("event: messenger \n"); // added new
c.Stream.WriteLine("data:" + new JavaScriptSerializer().Serialize(ou) + "\n\n");
});
and changed script to
eventSource.addEventListener('messenger',...`
and it stopped working after adding event name
Please help!.
Thank you.

looks like it works if you remove the space between the event name and "\n", like so:
Clients.ForEach(c =>
{
c.Stream.WriteLine("event: messenger\n"); // added new w/o space
c.Stream.WriteLine("data:" + new JavaScriptSerializer().Serialize(ou) + "\n\n");
});

Related

two way communication between c# and JS using webview2

I'm using a webview2-control in a winforms application. I use messages to communicate between c# and Javascript
window.chrome.webview.addEventListener / window.chrome.webview.postMessage in Javascript
event .CoreWebView2.WebMessageReceived and method CoreWebView2.PostWebMessageAsString in C#
The communication works BUT only after the page in webview2 has been somehow refreshed. The first message sent by c# is always ignored/not received by JS. The messages after that are correcly received and processed.
My UI code:
public GUI()
{
InitializeComponent();
browser.Source = new Uri(System.IO.Path.GetFullPath("HTML/ui.html"));
InitializeAsync();
}
async void InitializeAsync()
{
await browser.EnsureCoreWebView2Async(null);
browser.CoreWebView2.WebMessageReceived += MessageReceived;
}
void MessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs args)
{
String content = args.TryGetWebMessageAsString();
if (content.StartsWith("getData"))
{
ReadDataFromCATIA();
var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
string jsonRootNode = JsonConvert.SerializeObject(this.RootNode, Formatting.Indented, serializerSettings); //here I've got the message I want to post
//String input = args.TryGetWebMessageAsString();
//MessageBox.Show("string from JS: " + input);
browser.CoreWebView2.PostWebMessageAsString(jsonRootNode);
}
else //object received
{
ProductNode received = JsonConvert.DeserializeObject<ProductNode>(content);
MessageBox.Show(received.PartNumber + " received");
}
}
and my JS in ui.html
window.chrome.webview.addEventListener('message', event => {
alert(event.data);
WriteDataFromCsharp(event.data);
});
function WriteDataFromCsharp(data) {
var target = document.getElementById('target');
if (target === null) { alert('target not found') };
//alert(target.id);
//target.textContent = event.data;
rootNode = JSON.parse(data);
target.innerHTML = addTable(rootNode); //addTable create an HTML table from the deserialized object rootNode
}
function RequestData() {
//function triggered by a button on the html page
//alert('post to c#');
window.chrome.webview.postMessage('getData');
}
So far, i've tried to:
ensure the javascript is as late as possible in the page (defer, at the end of body). No changes.
inject the javascript to the page after it has been loaded using .CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(jsCode). Same behavior.
inject the javascript after once the event NavigationCompleted has fired. same behavior.
What do I miss ?
Finally found the culprit: in my HTML-page, i've used a "submit" instead of "button". With
<input type="button" value="Load data from V5" onclick="RequestData()" />
The page behavior is as expected.

JSON List ASP.net MVC

I have /Views/Movies/Index.cshtml with
<input type="button" id="getmoviex" value="Get moviex" />
<ul id="moviex_list"/>
<p>
Title: #Html.TextBox("SearchTitle") <br />
</p>
I have /Controllers/MoviesController.cs with
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult moviex(string SearchGenre, string SearchTitle, string SearchActor)
{
var db = new CinemaContext();
db.Configuration.ProxyCreationEnabled = false;
var Movie = from m in db.Movie
select m;
if (!String.IsNullOrEmpty(SearchTitle))
{
Movie = Movie.Where(s => s.Title.Contains(SearchTitle));
}
return Json(db.Movie.ToList(), JsonRequestBehavior.AllowGet);
}
I have Javascript.js with
$(document).ready(function () {
$('#getmoviex').click(function () {
$.getJSON("/Movies", null, getmoviex);
});
});
Have I correctly written /Movies? Or this should be /Views/Movies?
function getmoviex(moviex) {
$("#moviex_list").text("");
$.each(moviex, function (i) {
$("#moviex_list").append("<li>" + this + "</li>");
});
}
How can I display info or list info from my query? Or view some output with error?
First make sure you button click does not trigger a request to server. Preventing default behavior is a standard way of doing it:
$('#getmoviex').click(function (event) {
$.getJSON("/Movies", null, getmoviex);
event.preventDefault();
});
As for the URL, it should not be to view, but to action instead. Your action is moviex and controller is Movies, so
$.getJSON("/Movies/moviex", null, getmoviex);
The rest looks fine, so that should do it.
you need to pass your arguments as well in url (GET).
Something like this could work:
$('#getmoviex').click(function(event) {
event.preventDefault();
$.getJSON("/Movies/moviex?SearchGenre=yuorgenre&SearchTitle=Cal&SearchActor=youractor", function(moviex) {
var lis;
//please check the console
console.log(moviex);
$.each(moviex, function(b) {
lis += "<li id='" + b.Id + "'>" + b.Title + "</li>");
}); document.getElementById("moviex_list").innerHTML += lis;
});
});
To avoid circular reference in Serializing you may use:
if (String.IsNullOrEmpty(SearchTitle)) {
return View("Error");
}
var db = new CinemaContext();
var Movie = (from m in db.Movie
Where m.Title.Contains(SearchTitle)
select new {
Id = m.MovieID,
Title = m.Title // can add more properties
}).ToList();
return Json(Movie, JsonRequestBehavior.AllowGet);

How can i handle confirm dialog in webview? UWP windows 10 app C#

i am developing uwp app (win 10) by c#
i want put my website in xaml webview element.
most of function are workable.
but i can't handle the confirm dialog
for example
this is sample html & js code
<button id="btnConfirm" onclick="confirmBox('sure to delete?')">click me to confirm</button>
<button id="btnAlert" onclick="alert('alert message')">click me to alert</button>
<script type="text/javascript">
function confirmBox(message) {
if (confirm(message)) {
alert("yes");
} else {
alert("no");
}
}
</script>
this is my xaml code
<WebView Grid.Row="1" x:Name="webView1" Source="ms-appx-web:///HTMLPage1.html" Width="auto" Height="auto"/>
this is my C# code
webView1.ScriptNotify += webView1_ScriptNotify;
webView1.NavigationCompleted += webView1_NavigationCompleted;
async void webView1_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
await webView1.InvokeScriptAsync("eval", new string[] { "window.confirm = function(confirmMessage) { window.external.notify('typeConfirm:' + confirmMessage) }" });
await webView1.InvokeScriptAsync("eval", new string[] { "window.alert = function(AlertMessage) { window.external.notify('typeAlert:' + AlertMessage) }" });
}
private async void webView1_ScriptNotify(object sender, NotifyEventArgs e)
{
Windows.UI.Popups.MessageDialog dialog;
string[] messageArray = e.Value.Split(':');
string message;
string type;
if (messageArray.Length > 1)
{
message = messageArray[1];
type = messageArray[0];
}
else
{
message = e.Value;
type = "typeAlert";
}
dialog = new Windows.UI.Popups.MessageDialog(message);
Debug.WriteLine("type=" + type + " ,message=" + message);
if (type.Equals("typeConfirm"))
{
dialog.Commands.Add(new UICommand(
"Yes",
new UICommandInvokedHandler(this.CommandInvokedHandler)));
dialog.Commands.Add(new UICommand(
"Cancel",
new UICommandInvokedHandler(this.CommandInvokedHandler)));
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
}
var result = await dialog.ShowAsync();
if (result.Label.Equals("Yes"))
{
// return true;
}
else
{
// return false
}
}
the problem is that confirm js function will always return false
before user clicked the yes or no.
i can get user choosed button. but it's too late.
js function "confirm" will never return true in this situation.
anyone can help me?
thanks.
This will not work. JS runs on single thread, and will not wait for the result of the c# call. So the alert will not wait for the result from the MessageDialog.
Please note that webview in winrt project more suitable for displaying some static html. If you do want to pop up a MessageDialog act as a confirm dialog, then you need to complete all rest work that you previous do in javascript in your c# code.
For example, if you want to change some text of a html control, you need to prepare a complete html string, then check command status in CommandInvokedHandler callback and let webview navigate to(webView.NavigateToString) the new html string.
If you need to receive notifications and data in your app code sent from WebView-hosted script, you need to handle the ScriptNotify event. You can refer to this sample (The scenario6).

CRM 2011 - Contact Form Loads with cannot assign to a function result onchange of a birthdate field

Here is the Code that Manipulates the Birthdate and Age, If age is blank I get an error with the field's customized event. I can't seem to figure out why this JavaScript is throwing the "Cannot assign to a function result" error on a Event: onchange, when this error is appearing on form load. Is there a way to Allow the code to have no action if the Birthdate field is blank?
function birthdate_onchange()
{
var age = Xrm.Page.getAttribute("new_age");
var bDay = Xrm.Page.getAttribute("birthdate");
if (bDay.getValue() != null)
{
var now = new Date();
var thisYear = now.getFullYear();
var birthYear = bDay.getValue().getFullYear();
if((bDay.getValue().getMonth()-now.getMonth())>=0)
{
if((bDay.getValue().getMonth()-now.getMonth())==0 && (bDay.getValue().getDate()-now.getDate())<=0)
{
age.setValue(thisYear-birthYear);
}
else
{
age.setValue(thisYear-birthYear-1);
}
}
else
{
age.setValue(thisYear-birthYear);
}
}
else
{
age.getValue()=null;
}
}
I would love some feed back on this as I am new to JavaScript, but want to learn this language very much. Can provide more code or elaboration as needed, as this is just a segment of our Script for Contacts.
Thanks,
PGM
Edit 1 (5/20/2014):
Now that the Age/birthdate field changes are resolved, I am getting a 'fireonchange' object doesn't support property or method 'fireonchange'.
I have a feeling it is coming from this segment of code:
//TODO: could use to be upgraded to 2011 syntax
if (Xrm.Page.getAttribute("srobo_birthdatepre1900").getValue() != null) {
crmForm.all.srobo_birthdatepre1900.style.display = 'inline';
crmForm.all.srobo_birthdatepre1900_c.style.display = 'inline';
crmForm.all.birthdate.style.display = 'none';
crmForm.all.birthdate_c.style.display = 'none';
}
else {
crmForm.all.birthdate.style.display = 'inline';
}
try {
//Sets Age
//TODO: test if this works
Xrm.Page.getAttribute("birthdate").fireOnChange();
}
catch (err1) {
alert("Contact onLoad Error 1" + err1 + " " + err1.description);
}
try {
}
catch (err2) {
alert("Contact onLoad Error 2 " + entity + ": " + err2 + " " + err2.description);
}
} //end on load
I have been trying to switch the CRM 4.0 JavaScript Versioning to the 2011 friendly syntax, but I am still getting the error in my CRM. Could anyone show me where my problem areas are in terms of Syntax?
I have already switched this line:
Xrm.Page.getAttribute("birthdate").fireOnChange();
but still don't see why it would be throwing that fireonchange error?
Thank you so much for all the help so far! I really appreciate it!
Try to change line
age.getValue()=null;
to
age.setValue(null);

Enlarge image on click of link

#foreach (var item in Model)
{
<img src='ShowShowcaseImage/#Html.Encode(item.ProductID)' id='#item.ProductID' />
<b>#Html.DisplayFor(m => item.ProductName)</b>
Enlarge
}
<div id="EnlargeContent" class="content">
<span class="button bClose"><span>X</span></span>
<div style="margin: 10px;" id="imageContent">
</div>
<p align="center"></p>
</div>
//Popup javascript
$('.enlargeImg').bind('click', function (e) {
$.post('/Home/EnlargeShowcaseImage/' + $(this).attr('id'), null, function (data) {
document.getElementById("imageContent").innerHTML += data;
});
$('#EnlargeContent').bPopup();
});
});
//
C# method
public ActionResult EnlargeShowcaseImage(string id)
{
var imageData = //linq query for retrive bytes from database;
StringBuilder builder = new StringBuilder();
if (imageData != null)
builder.Append("<img src='" + imageData.ImageBytes + "' />");
return Json(builder);
}
I want to show pop up of enlarged image on click of enlarge link. Image is stored in bytes in database. Two images are stored in database for each product - one is thumbnail and the other is enlarged. I am showing thumbnail image and I want to show enlarged image on click of enlarge link. I can't retrieve it from database.
I can't retrieve it from database
So your question is about retrieving an image from a database, right? It has strictly nothing to do with ASP.NET MVC?
Unfortunately you haven't told us whether you are using some ORM framework to access to your database or using plain ADO.NET. Let's assume that you are using plain ADO.NET:
public byte[] GetImage(string id)
{
using (var conn = new SqlConnection("YOUR CONNECTION STRING COMES HERE"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
// TODO: replace the imageData and id columns and tableName with your actual
// database table names
cmd.CommandText = "SELECT imageData FROM tableName WHERE id = #id";
cmd.Parameters.AddWithValue("#id", id);
using (var reader = cmd.ExecuteReader())
{
if (!reader.Read())
{
// there was no corresponding record found in the database
return null;
}
const int CHUNK_SIZE = 2 * 1024;
byte[] buffer = new byte[CHUNK_SIZE];
long bytesRead;
long fieldOffset = 0;
using (var stream = new MemoryStream())
{
while ((bytesRead = reader.GetBytes(reader.GetOrdinal("imageData"), fieldOffset, buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, (int)bytesRead);
fieldOffset += bytesRead;
}
return stream.ToArray();
}
}
}
}
and if you are using some ORM it could be as simple as:
public byte[] GetImage(string id)
{
using (var db = new SomeDataContext())
{
return db.Images.FirstOrDefault(x => x.Id == id).ImageData;
}
}
and then inside your controller action:
public ActionResult EnlargeShowcaseImage(string id)
{
var imageData = GetImage(id);
if (imageData != null)
{
// TODO: adjust the MIME Type of the images
return File(imageData, "image/png");
}
return new HttpNotFoundResult();
}
and it is inside your view that you should create an <img> tag pointing to this controller action upon button click:
$('.enlargeImg').bind('click', function (e) {
$('#imageContent').html(
$('<img/>', {
src: '/Home/EnlargeShowcaseImage/' + $(this).attr('id')
})
);
$('#EnlargeContent').bPopup();
});
But hardcoding the url to your controller action in javascript like this is very bad practice because when you deploy your application it might break. It might also break if you decide to change the pattern of your routes. You should never hardcode urls like this. I would recommend you generating this url on the server.
For example I see that you have subscribed to some .enlargeImage element. Let's suppose that this is an anchor. Here's how to properly generate it:
#Html.ActionLink("Enlarge", "EnlargeShowcaseImage", "Home", new { id = item.Id }, new { #class = "enlargeImage" })
and then adapt the click handler:
$('.enlargeImg').bind('click', function (e) {
// Cancel the default action of the anchor
e.preventDefault();
$('#imageContent').html(
$('<img/>', {
src: this.href
})
);
$('#EnlargeContent').bPopup();
});
Try jQuery,
Here is one
http://superdit.com/2011/06/11/hover-image-zoom-with-jquery/
http://demo.superdit.com/jquery/zoom_hover/

Categories