JS code:
function save( ref, text){
$.post("save.aspx", {cc:"us", ref:ref, text:text}, function(data){
$("[data-ref='"+ref+"'] .loader").animate({opacity:0},500,function(){
$(this).parent().removeClass("saving");
$(this).remove();
});
});
}
CS file Code:
public partial class save : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// $_REQUEST['cc'];
// HttpContext.Current.Response
string strcc = Request.Form["cc"];
string strRef = Request.Form["ref"];
string strtext =Request.Form["text"];
}
}
Unable to get the values passed from JS code to next page, what i'm doing wrong here?
Your code is fine. I'm almost sure that ASPX page code executes before JS function calls it. You may trace this with Fiddler
Related
I want get javascript response,
and follow cef document to do,
this is my step,
first create a html file
...
<button onclick='test()'>click</button>
...
<script>
function test(){
alert('123');
callbackObj.getMessage('123');
}
</script>
and then I register javascript response method
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
this.MyBrowser.JavascriptObjectRepository.Register("callbackObj", new PagaJavaScriptResponse(), true);
and create class to do PagaJavaScriptResponse
public class PagaJavaScriptResponse
{
public void getMessage(string s)
{
....
}
}
next step to check the register is bound
var isBound = this.MyBrowser.JavascriptObjectRepository.IsBound("callbackObj");
the result is true,
last step to url http://127.0.0.1/index.html
this.MyBrowser.Address = #"http://127.0.0.1:8887/test.html";
here I think when I click button it should be return 123 to my method in C#, but not,
hot it's correct?
I try a solution
just when page loaded excute this javascript
this.MyBrowser.WebBrowser.ExecuteScriptAsyncWhenPageLoaded(#"(async function() {await CefSharp.BindObjectAsync('callbackObj', 'bound');})();");
it's will work
Any one can help me because i'm Trying to register a script first and bind a function called "DoClick()" into button using C# but some error is occurred during runtime. please see my code below. so that when button was click they call the function "DoClick(). Thanks Guys
public void regiterAdsScript(int loc)
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
Type csType = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { <script type='text/javascript' src='//abcd.site?id=123'></script> } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "return DoClick()");
}
}
Your script registration code should be like this
Button1.Attributes.Add("onClick", "javascript:DoClick();");
Also, your script looks wrong. Whatever you want your script to do should be followed by function declaration. Like if you want to put an alert it should be like this
csText.Append("function DoClick() { alert('MK'); } \n");
You also need to call the function regiterAdsScript() before the Button1_Click is called. I've called it in the Page_load itself. Below is sample program for you:
protected void Page_Load(object sender, EventArgs e)
{
regiterAdsScript();
}
protected void Button1_Click(object sender, EventArgs e)
{
//functionality to be implemented
}
public void regiterAdsScript()
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
Type csType = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { alert('MK'); } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "javascript:DoClick();");
}
}
I have made previous posts about my custom visualization not working in Spotfire:
https://stackoverflow.com/questions/25390099/awesomium-javascript-handler-being-called-indefinitely
Returning value to C# function from Javascript not working in Awesomium
and I have finally narrowed it down to the offending line.
In my document, I load a source script:
<script src="http://d3js.org/d3.v3.min.js"></script>
This seems to break my entire custom visualization; it infinitely tries to reload the page, from what I've seen. Here is my C# code:
private void WebViewOnDomReady(object sender, EventArgs eventArgs)
{
webView.DomReady -= WebViewOnDomReady;
webView.CreateObject("jsobject");
//webView.SetObjectCallback("jsobject", "callNETNoReturn", JSHandler);
webView.SetObjectCallback("jsobject", "callNETWithReturn", JSHandler);
//webView.ExecuteJavascript("myMethod()");
var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
MessageBox.Show("Stuff:" + result.ToString());
}
private void JSHandler(object sender, JSCallbackEventArgs args)
{
var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
MessageBox.Show(result.ToString());
MessageBox.Show("Got method call with no return request");
}
And here is my Javascript code:
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function myMethod() {
document.write("In myMethod, calling .NET but expecting no return value.<br/>");
jsobject.callNETNoReturn();
}
function myMethodExpectingReturn() {
document.write("In myMethodExpectingReturn, calling .NET and expecting return value.<br/>");
var returnVal2 = jsobject.callNETWithReturn("foo");
document.write("Got value from .NET: " + returnVal2 + "<br/>");
}
function myMethodProvidingReturn(whatToReturn) {
var returnVal = whatToReturn + "bar";
document.write("Returning '" + returnVal + "' to .NET.");
return returnVal;
}
</script>
Interestingly enough, the HTML loads fine if I don't try and call a Javascript function and get the return value in C#. However, when I try to return the result of the JS function and print it in C#, including the script src line breaks my entire code; it infinitely returns a blank message judging from the MessageBoxes that I have set.
This is completely baffling me, as it seems to mean that the HTML is being loaded over and over again. Setting the script src tag, for some odd reason, causes this infinite loop.
What exactly is happening?
Thanks
I need a code segment to call a javascript function recordInserted() which shows up an alert, from my following code behind method,
protected void add_Click(object sender, EventArgs e)
{
String gradename = txt_gradename.Text;
int allocatedclasses = Int32.Parse(txt_allocatedclasses.Text);
String headid = txt_head_id.Text;
int numberofstudents = Int32.Parse(txt_numberofstudents.Text);
db = new DBConnection();
db.getConnection();
db.executeUpdateQuery("INSERT INTO Grade (GradeName,AllocatedClasses,GradeHeadID,NumberOfStudents) VALUES ('"+gradename+"','"+allocatedclasses+"','"+headid+"','"+numberofstudents+"')");
//I Need to call it from here before redirecting
Response.Redirect("AdminReferenceGradeAdd.aspx");
}
Please helpp me with this.
I have tried the following but never worked,
Page.ClientScript.RegisterStartupScript(this.GetType(),"Call my function","recordInserted()",true);
This will never work .. beacuse you are saying to redirect.
when you say Response.Redirect every thing which you have prepared to send is not sent,instead response is redirect to a new page.So your client script never reaches to browser.
you can use it like this :-
Page.ClientScript.RegisterStartupScript(this.GetType(),"Call my function","recordInserted();window.location.href='wwW.google.com'",true);
use window.location.href to redirect to your page("yourpage.aspx').
Try this:
ClientScript.RegisterClientScriptBlock(typeof(Page), "Call your function", "recordInserted()", true);
Or try calling Javascript function after a second:
ClientScript.RegisterClientScriptBlock(typeof(Page), "Call your function", "setTimeout('recordInserted()', 1000)", true);
I have a class. There is only deleteRecord function
protected virtual void DeleteRecord
{
if(..)
{}
else(..)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language='javascript'>alert('Are you sure?')</script>", true);
}
}
I want to show javascript message. But I think I made a mistake.
How can I do it?
You've added true to the last parameter on Page.ClientScript.RegisterStartupScript which is addScriptTags. See http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
Therefore you have essentially added <script> within a <script>
Try this:
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Are you sure?')", true);
Also ensure the key parameter is unique to the page. If you already have a StartupScript with the key of "Alert" then this can also stop it from calling the JavaScript code.
I created this static class that I can call from any web page: (I use AJAX Toolkit for script manager, but you can use the default one in ASP.NET as well)
public static class ClientJS {
public static void send(string js) {
Page page = HttpContext.Current.Handler as Page;
ToolkitScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), "setTimeout(function(){" + js + "},1);", true);
}
}
Use it like so:
ClientJS.send("alert('Are you sure?');");