function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById('directions_panel');
summaryPanel.innerHTML = '';
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
}
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
my button event is not fired...i want to shows the get directions using two dropdown list controls using server side controls but my button event is not fired ...wht i do?
<asp:Button ID="btnsubmit" runat="server" OnClientClick="return calcRoute();" Text="Submit" />
how to cal javascript method into serverside...please tell me..
Why have you got a server side button in the first place?
The button doesn't call a submit event, nor does it post back to the server. So why not simply use a normal html button?
Even your Javascript function doesn't return a true or a false so using return calcRoute() will always return a false event.
In any case, try using the normal onClick event rather than OnClientClick.
edit
Seems to me a lot of what you are doing here could be solved using jQuery in a much better way than you are trying to do here.
Syntax :
public void RegisterClientScriptBlock(
Type type,
string key,
string script,
bool addScriptTags
)
Parameters
type
Type: System.Type
The type of the client script to register.
key
Type: System.String
The key of the client script to register.
script
Type: System.String
The client script literal to register.
addScriptTags
Type: System.Boolean
A Boolean value indicating whether to add script tags.
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext1 = "alert('Hello World');";
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
}
I am not add "return" false condition...My Button is getting a PostBack right now, that could be a problem. So add
Return false;
condition..
Then it will be working fine...thanks for the response..
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 have a scenario where i have a user control within a page which consists of core asp.net logic external javascript and UI and the page is just an container for this user control.
Now i need to inject a javascript at page load of the user control by calling a javascript function of the external javascript file but its not working.Any Help?
In Page_Load
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "TC.Report.OnSelectedReportChange('" + noParameterReports + "');");
ddlReport.Attributes.Add("onChange", "return TC.Report.OnSelectedReportChange('" + noParameterReports + "');");
In external javascript
OnSelectedReportChange: function (noParameterReports) {
var noParameterReport = noParameterReports.split("|");
var enableParameters = "true";
var pageName = TC.Report.getPageName(window.location.href);
for (i = 0; i < noParameterReport.length; i++) {
if ($("select[id$=ddlReport]").val() == noParameterReport[i]) {
if (pageName == "AGIReports") {
$('.officeTextBox').children().attr('disabled', true);
$('.dropDownTEMethod').children().attr('disabled', true);
} else {
$("select[id$=ddlOpCo]").disabled = true;
$("select[id$=ddlWeekEndingDate]").disabled = true;
}
enableParameters = "false";
}
}
if (enableParameters == "true") {
if (pageName == "AGIReports") {
$('.officeTextBox').children().attr('disabled', false);
$('.dropDownTEMethod').children().attr('disabled', false);
} else {
$("select[id$=ddlOpCo]").disabled = false;
$("select[id$=ddlWeekEndingDate]").disabled = false;
}
}
}
I have a few observation to this..firstly the registerstartup script executed first before the user control is loaded so it throws an error TC is undefined which is rightly so as the external java script is reference in user control only but i am calling this method from user control's page load.
Secondly i tried to add the register startup script on prerender event after adding ethe javascript file reference in the page but then it says object require when ddlreport object references in the javascript method..page render guarantees that controls are loaded and available
I need to use the javascript functions to show and hide an element on my page, but calling it from within a C# method. Is this possible?
EDIT : I tried RegisterStartupScript (see below) but this did not hide the elements as I had hoped :
HidePopup("CompanyHQSetup", "$('#<%=DivDataProvider.ClientID %>').hide();$('#<%=modalOverlay.ClientID %>').hide();");
private void HidePopup(string Key, string jscript)
{
string str = "";
str += "<script language='javascript'>";
str += jscript;
str += "</script>";
RegisterStartupScript(Key, jscript);
}
EDIT : Got around this by using a hidden field boolean to determine whether or not to hide or show the elements
Yes, check out RegisterClientScriptBlock.
Here's a snippet taken from that link:
public void Page_Load(Object sender, EventArgs e)
{
// 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\"> function DoClick() {");
csText.Append("Form1.Message.value='Text from client script.'} </");
csText.Append("script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}
}
One is server side, the other is client side. They can pass variables to each other (Javascript to ASP would be via forms/querystring/cookies and ASP to JS done via response.writing variables), but they can't directly interact.
you can use page.RegisterClientScript method to do that go on the following url
http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx
and give it a try
Javascript is client side, c# is server side. You can't call javascript directly from C#. Take a look at Comet though, it will show you how you can push data from the HTTP server to the webpage.