Javascript __dopostback(): arguments passing error "Invalid postback or callback argument" - javascript

I would like to raise an event in javascript from Asp.net code behind page; whenever a checkbox control is checked. I am able to use __dopostback from javascript to raise an event in the code behind, but I'm not able to pass a variable as argument.
This is the javascript code:
function CallServerCkhkBox(chkvalue) {
alert("_dopostback " + chkvalue);
__doPostBack('btnRefresh', chkvalue);
//__doPostBack('btnRefresh', 'Blue Green');
}
The alert prints the correct value of the variable chkvalue.
This is the C# code behind:
protected void btnRefresh_Click(object sender, EventArgs e)
{
string checkboxes;
if (Request["__EVENTARGUMENT"] != "")
{
checkboxes = "From Javascript " + Request["__EVENTARGUMENT"];
}
else
{
checkboxes = "From Click " + hdnChkbval.Value;
}
lblCheckBoxes.Text = checkboxes;
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation("btnRefresh", "Blue Green");
base.Render(writer);
}
If I pass an argument a fixed string to __dopostback it works, otherwise it returns the error:
Invalid postback or callback argument.
I believe in the RegisterForEventValidation() method, the exact value is not declared.
Is there any way to pass a variable string?

Try this:
function CallServerCkhkBox(chkvalue) {
alert("_dopostback " + chkvalue);
__doPostBack('btnRefresh', chkvalue.toString());
}

Related

Vaadin flow javascript to Java call

Following this tutorial https://vaadin.com/blog/calling-java-from-javascript I'm trying to call a Java function from javascript but that doesn't seem to work as expected.
I'm having a View that contains a button which, on its onClick handler, triggers a call to a Javascript function, which works as expected.
The problem I'm having is that the getElement() that I`m passing to the javascript function is undefined when it reaches the javascript side of things.
My code looks as follows:
#JavaScript("./js/script.js")
public class RouteGraphicsView extends Div {
....
Button b = new Button("Test Button");
b.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
private static final long serialVersionUID = 1L;
#Override
public void onComponentEvent(final ClickEvent<Button> event) {
UI.getCurrent().getPage().executeJs("greet($0, $1)", "test name", UI.getCurrent().getElement());
}
});
....
}
The above call reaches the script.js file which looks like this
window.greet = function greet(name, element) {
console.log("Hello, I am greeting you, " + name);
try {
console.log("Element ", element);
console.log("Logging 1", element.$server);
} catch (e) {
console.log(e);
}
}
The output shown by the greet function above is
Hello, I am greeting you, test name
vaadin-bundle-62ac8b…b56c6.cache.js:4813 Element
vaadin-bundle-62ac8b…b56c6.cache.js:4813 Logging 1 undefined
Since the element.$server is undefined I can not get the javascript function to call my greet function in the View, which is annotated with #ClientCallable
#ClientCallable
public void greet(final String name) {
System.out.println("Called from JavaScript: " + name + " \n\n\n");
}
I've tried various other ways of calling the script.js, like using button's element to invoke the executeJs function or passing the button's element (b.getElement()) as an argument to the function but to no avail.
What am I doing wrong ?
You're doing element.$server on the element that you passed as UI.getCurrent().getElement(). This corresponds to the UI instance and not an instance of the RouteGraphicsView class that (I assume) has the #ClientCallable method. Using the button would also not work for the same reason.
You should pass an instance of the view, which in your case needs to be written as RouteGraphicsView.this because of the way the regular this refers to the click listener.

Return HTML string from the completion handler of the evaluateJavaScript function

I know that I'm not the first one to ask this but I can't solve the problem. I'm trying to take a piece of string from HTML using evaluateJavaScript in Xcode with Swift 3 and the piece of text is called value inside the completion handler, so I did like this:
var userName = String()
func takeData() {
webView.evaluateJavaScript("document.querySelectorAll('.name')[0].innerHTML") { (value, error) in
if let valueName = value as? String {
self.userName = valueName
}
print(value)
print(error)
}
}
print(" The name is : \(self.userName)")
The problem is that the console just prints: The name is ()
The problem is that you are printing the value before your asynchronous function could finish execution. You have several solutions to solve this issue. You can either implement takeData to have a completionHandler as one of its input parameters, use GCD to make your statements execute in the expected order or use a 3rd party library, such as PromiseKit to handle the async requests for you, so they will behave like normal functions with a return value.
I will give you an example with the completion handler:
func takeData(completionHandler: #escaping (_ userName: String?) -> Void){
webView.evaluateJavaScript("document.querySelectorAll('.name')[0].innerHTML") { (value, error) in
if let valueName = value as? String {
completionHandler(valueName)
}
print(value)
print(error)
completionHandler(nil)
}
}
You use the value from the completionHandler like this:
takeData(completionHandler: { userName in
print(userName)
})

Call .NET from JavaScript using CefGlue / CEF3

There is an existing solution for CefGlue: Call .Net from javascript in CefSharp 1 - wpf
I want exactly this, but for CefGlue: I want to communicate with the App using JavaScript. So when I click a button in my HTML site, I want the application to handle this (for example: start a tcp server).
I tried to register an own CefV8Handler but without success, the Execute function on the handler is never called. Here is what I do right now
protected override void OnWebKitInitialized()
{
Console.WriteLine("Registering testy extension");
Xilium.CefGlue.CefRuntime.RegisterExtension("testy", "var testy;if (!testy)testy = {};(function() {testy.hello = function() {};})();", new V8Handler());
base.OnWebKitInitialized();
}
My V8Handler code looks as follows:
public class V8Handler : Xilium.CefGlue.CefV8Handler
{
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
{
if (name == "testy")
Console.WriteLine("CALLED TESTY");
else
Console.WriteLine("CALLED SOMETHING WEIRED ({0})", name);
returnValue = CefV8Value.CreateNull();
exception = null;
return true;
}
}
I'm in multiprocess mode, no console window shows "CALLED TESTY" nor "CALLED SOMETHING WEIRED".
Found a solution for that. The trick is to create a CefV8Value (CreateFunction) and assign it to a V8Handler. Then assign this value to the global context. This is what it looks like:
internal class RenderProcessHandler : CefRenderProcessHandler
{
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
{
CefV8Value global = context.GetGlobal();
CefV8Value func = CefV8Value.CreateFunction("magic", new V8Handler());
global.SetValue("magic", func, CefV8PropertyAttribute.None);
base.OnContextCreated(browser, frame, context);
}
}
Another problem came up: it was called in the renderer process, but I required the callback in the browser process. In the CefV8Handlers execute function i did this:
var browser = CefV8Context.GetCurrentContext().GetBrowser();
browser.SendProcessMessage(CefProcessId.Browser, CefProcessMessage.Create("ipc-js." + name));
This way I can retrive the message in the OnProcessMessageReceived function in the CefClient implementation.

Call Java component from JavaScript and retrieve value

I am trying to call a Wicket component's method from JavaScript and receive a value from this method which I want to use in the remaining bit of the JavaScript function which I used to call the component. However, I only seem to be able to call a Wicket component without waiting for it to produce a result.
More explicitly, I want to implement an AbstractDefaultAjaxBehavior which allows me to conditionally warn a user when he or she is leaving a page. This condition is for now determined by some OuterClass.shouldWarn method. However, even though this method gets called in my example below, I seem to be both unable to wait for a result of this method as well as I am unable to return some sort of result at all. Instead, the JavaScript just continues in its execution concurrently to the Java method call.
I hope the (not correctly running) example below clarifies my question:
class PageExitWarningBehavior extends AbstractDefaultAjaxBehavior {
#Override
protected void respond(AjaxRequestTarget target) {
target.appendJavaScript("return " +
(OuterClass.this.shouldWarn() ? "false" : "true"));
}
#Override
public void renderHead(Component component, IHeaderResponse response) {
String callbackFunktion = String.format(
"Wicket.Event.add(window, 'beforeunload', function( e ) {%n"
+ "if( e ) { e.returnValue = '%s'; }%n"
+ "var attrs = { 'u': '%s', 'c': '%s', 'ep': { } };%n"
+ "Wicket.Ajax.get( attrs );%n"
+ "return false;%n;"
+ "});",
this.getCallbackUrl(),
OuterClass.this.getMarkupId());
response.render(JavaScriptHeaderItem.forScript(callbackFunktion,
"remind-of-running-task"));
}
}
I believe there is an easier way to intercept a page exit event than implementing your own AjaxBehavior:
Try implementing the following Behavior:
public class PageExitWarningBehavior extends Behavior {
private boolean shouldWarn = false;
#Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
if (shouldWarn) {
response.render(new OnDomReadyHeaderItem("window.onbeforeunload = function (e) {"
+ "var message = 'Your confirmation message goes here.',"
+ "e = e || window.event;" + "if (e) {"
+ "e.returnValue = message;" + "}" + "return message;" + "};"));
}
}
#Override
public void onEvent(Component component, IEvent<?> event) {
super.onEvent(component, event);
if (event.getPayload() instanceof PageExitWarningEvent) {
PageExitWarningEvent exitEvent = (PageExitWarningEvent) event.getPayload();
this.shouldWarn = exitEvent.isPageExitWarningEnabled();
}
}
}
In the renderHead method you conditionally add a simple javascript that triggers the browser to show a confirmation dialog when leaving the page (the javascript code is from this post).
In the onEvent method we listen if some other Wicket component has sent an PageExitWarningEvent to inform us that a warning should be displayed at all. You can send such an event from any Wicket component (such as a link or button) like this:
send(HomePage.this, Broadcast.BREADTH, new PageExitWarningEvent(true));
The PageExitWarningEvent class looks like this:
public class PageExitWarningEvent {
private boolean pageExitWarningEnabled = false;
public PageExitWarningEvent(boolean pageExitWarningEnabled) {
this.setPageExitWarningEnabled(pageExitWarningEnabled);
}
public boolean isPageExitWarningEnabled() {
return pageExitWarningEnabled;
}
public void setPageExitWarningEnabled(boolean pageExitWarningEnabled) {
this.pageExitWarningEnabled = pageExitWarningEnabled;
}
}
Let me know if that meets your requirements.

Get value of item which is clicked in datalist by Javascript

When design I have a Datalist with a label inside. when load it will has 10 label(datasource from list has 10 value type int ). I want get value of any label which i click. I think i must resolve 2 problem:
1. Find control(label inside datalist) which is clicked.
2. Get value of it.
protected void Page_Load(object sender, EventArgs e)
{
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
{
list.Add(i);
}
int a=1;
DataList1.DataSource = list;
DataList1.DataBind();
foreach (DataListItem item in DataList1.Items)
{
((Label)item.FindControl("Label1")).Text = a.ToString();
if ((Convert.ToInt32(((Label)item.FindControl("Label1")).Text)) % 2 != 0)
{
((Label)item.FindControl("Label1")).BackColor = System.Drawing.Color.Gray;
}
((Label)item.FindControl("Label1")).Attributes.Add("onclick", "run();");
a++;
}
This is my run() function
function run() {
$("#Panel1").scrollTop(100*gt1);
}
Here, i want gt1 get value of label clicked.
Thanks for helping(sr about my English)
Try it this way...
We will pass the current object of label while binding javascript event with it using keyword this. on server side
((Label)item.FindControl("Label1")).Attributes.Add("onclick", "run(this);");
On Client side we have to change the definition to receive a parameter which is passed from server side. In our case parameter name is lbl.
function run(lbl)
{
alert(lbl.innerText);
}
This is nice article will give you good understanding of grid view here
You can attach click event more elegantly with any item within datalist using css class instead of defining onclick event for each item.
e.g label have the following css property = "itm";
$(document).on('click', '.itm', function (e) {
run(this);
});
I found how to resolve my problem:
First add:
string gt = ((Label)item1.FindControl("lblstt")).Text;
After that:
((Label)item2.FindControl("lblCauHoi")).Attributes.Add("onclick",
"run("+gt+");");
Hope it's useful.

Categories