I've been searching for a longtime, How can I call my public void search() inside my javascript keypress. Newb in asp.net Thanks!
This is my aspx code.
<script language="javascript" type="text/javascript">
window.addEventListener("keyup", checkKeypress, false);
function checkKeypress(key) {
var TestVar = document.getElementById('<%= Search_Employee.ClientID %>');
'<% search(); %>';
}
</script>
[System.Web.Services.WebMethod]
public static void GetData()
{
MessageBox.Show("Calling From Client Side");
//Your Logic comes here
}
<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true"/>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function CallingServerSideFunction() {
PageMethods.GetData();
}
</script>
</form>
</body>
Related
I am trying to get a Yes No Prompt to show client side after an if statement in code, and depending on what the user clicks to execute code.
So far I have the following:
if (barcodes[t].ToString() == txtBarcode.Text)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>confirm('Item Already in Order, Continue?');</script>");
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Ok")
{
itemAdd();
DBConnect.DisplayMessage(this, "Added to order");
}
else
{
DBConnect.DisplayMessage(this, "Not added");
return;
}
}
I envoke a script in the third line of the above code, which shows correctly.
However regardless of what the user chooses, it will always return negative.
Thanks in advance :)
Here's a nice and simple solution from Mudassar Ahmed Khan's website that fits your needs:
PageClientScript.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="PageClientScript.aspx.cs" Inherits="PageClientScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
}
else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplayMessage" runat="server" Text="Click the button to display a confirm dialog."></asp:Label>
<br /><br />
<asp:Button ID="btnConfirm" runat="server" OnClick="OnConfirm" Text="Raise Confirm" OnClientClick="Confirm()"/>
</div>
</form>
</body>
</html>
PageClientScript.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class PageClientScript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
this.lblDisplayMessage.Text = "Added to order!";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.lblDisplayMessage.Text = "Not added to order!";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}
}
Results:
I am using PageMethods to call C# function from Javascript.
My C# function is return as String.
But after i called that function from Javascript it returned current page.
Here my code:
ASPX:
<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<script>
function testPageMethods(){
var id = 45;
PageMethods.returnStr4Frontend(id, onSucess, onError);
}
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(has error);
}
testPageMethods();
</script>
</form>
ASPX.CS
[WebMethod]
public static string returnStr4Frontend(string id)
{
string reStr = string.Empty;
reStr = "Your id is: " + id;
return reStr;
}
When i run my page on browser, i received wrong result was (all html code of current page):
<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<script>
function testPageMethods(){
var id = 45;
PageMethods.returnStr4Frontend(id, onSucess, onError);
}
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(has error);
}
testPageMethods();
</script>
</form>
Check this nugget
https://www.nuget.org/packages/HtmlAgilityPack
it will get the html code depending on the domain you entered
How to set navigateurl for radwindow in javascript ?
I want to use a radwindow in my page, and I use it in tow part.
I have a radwindow that I use it in 2 part.
<telerik:RadWindowManager ID="RadWindowManager1" ShowContentDuringLoad="false"
VisibleStatusbar="false"
RegisterWithScriptManager="True"
EnableShadow="True" ReloadOnShow="true" Width="800px" Height="550px"
runat="server">
<Windows>
<telerik:RadWindow ID="modalPopup" runat="server" Modal="True"
>
</telerik:RadWindow>
</Windows></telerik:RadWindowManager>
I use javascript for show radwindow.
<telerik:RadCodeBlock runat="server" ID="rdbScripts">
<script type="text/javascript">
function showDialogInitially() {
var wnd = $find("<%=modalPopup.ClientID %>");
wnd.show();
Sys.Application.remove_load(showDialogInitially);
}
When I click on a button , set Navigateurl of radwindow = ("DefCall.aspx") and when I click to other button , set navigateurl = ("DefTask.aspx") and passe 2 value to the child page by QueryString or other way.
protected void btnDefCall_Click(object sender, EventArgs e)
{
string strURL = string.Format("../PhoneCall/DefPhoneCall.aspx
?FormTypeID={0}&FormID={1}", number1,number2);
modalPopup.NavigateUrl = strURL;
ScriptManager.RegisterStartupScript(Page, GetType(), "PopupScript", string.Format("javascript:showDialogInitially()"), true);
}
protected void btnDefTask_Click(object sender, EventArgs e)
{
string strURL = string.Format("../Task/DefTask.aspx?FormTypeID={0}&FormID={1}", (int)clsHelper.FormType.Lead, int.Parse(Request.QueryString["ID"].ToString()));
modalPopup.NavigateUrl = strURL;
ScriptManager.RegisterStartupScript(Page, GetType(), "PopupScript", string.Format("javascript:showDialogInitially()"), true);
}
Check out the RadWindow API you can change the url through javascript like this
var wnd = $find("<%=modalPopup.ClientID %>");
wnd.setUrl("http://www.google.com");
You can use window.radopen. The only trick is you need to use pageLoad which is to wait until the RadWindow is fully loaded.
protected void btnDefTask_Click(object sender, EventArgs e)
{
var script = string.Concat(
"function pageLoad() { showDialogInitially('",
"http://www.telerik.com",
"'); }");
ScriptManager.RegisterStartupScript(Page, GetType(),
"PopupScript", script, true);
}
<telerik:RadWindowManager
ID="RadWindowManager1"
ShowContentDuringLoad="false"
VisibleStatusbar="false"
RegisterWithScriptManager="True"
EnableShadow="True"
ReloadOnShow="true"
Width="800px" Height="550px"
runat="server">
<Windows>
<telerik:RadWindow ID="modalPopup" runat="server" Modal="True">
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
function showDialogInitially(url) {
window.radopen(url, "modalPopup");
return false;
}
</script>
</telerik:RadScriptBlock>
I want to call static server-side methods from JS so i decide to use ScriptManager control on my site.
So i have a master page, with such structure:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="TopLevelMasterPage.Master.cs"
Inherits="Likedrive.MasterPages.TopLevelMasterPage" %>
<!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"
xmlns:fb="http://ogp.me/ns/fb#">
<head runat="server">
<title></title>
<script type="text/javascript">
function getGiftFileUrl() {
function OnSuccess(response) {
alert(response);
}
function OnError(error) {
alert(error);
}
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);
}
getGiftFileUrl();
</script>
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManagerMain"
runat="server"
EnablePageMethods="true"
ScriptMode="Release"
LoadScriptsBeforeUI="true">
</asp:ScriptManager>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>
But when page is loading, i have a JS exception - PageMethods is undefined.
I supposed that object will be created implicit so i can use it in my javascript.
To use PageMethods you need to follow these steps:
You need to use ScriptManager and set EnablePageMethods. (You did).
Create a static method in your code behind and use the [WebMethod] attribute.
Call you method in javascript like you should do in C# but you have more parameter do fill, the sucess and error callbacks. (You did).
Did you miss any of these steps?
Edit:
Just realized you did this:
function getGiftFileUrl() {
function OnSuccess...
You have yours callbacks inside a function. You need yours callbacks like this:
function OnSuccess(response) {
alert(response);
}
function OnError(error) {
alert(error);
}
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);
And you code behind probably will end in something like that:
[WebMethod]
public static string GetGiftFileUrl(string name, int width, int height)
{
//... work
return "the url you expected";
}
Bonus: Since it is a static method you can't use this.Session["mySessionKey"], but you can do HttpContext.Current.Session["mySessionKey"].
In your codebehind create this method:
[WebMethod]
public static void GetGiftFileUrl(string value1, int value2, int value3)
{
// Do Stuff
}
your js script should resemble this too:
<script type="text/javascript">
function getGiftFileUrl() {
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSucceeded, OnFailed);
}
function OnSucceeded(response) {
alert(response);
}
function OnFailed(error) {
alert(error);
}
getGiftFileUrl();
</script>
I've realize why the PageMethod object was undefinded, because ScriptManager component placed next from the script that uses PageMethod, so when page is rendered and script executed, there is no PageMethod at this moment. So i need to call getGiftFileUrl() on button click or on window load event, when all scripts on page are ready to use.
<script type="text/javascript">
function Generate()
{
var result = PageMethods.GenerateOTP(your parameter, function (response)
{
alert(response);
});
}
</script>
Will 100% work.
I use asp.net and this code for open local file with IE:
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("d:/11.doc", 1, false);
}
</script>
<input type="button" value="Run File" onclick="RunFile();"/>
result is ok :-)
but I want send address local file to script then run this file:
example:
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run(**AddressFile**, 1, false);
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
**AddressFile** = "d:/11.doc";
}
If you want to call the javascript function dynamically from server-side, simply add a parameter to the JS function to take the file name as so:
<script type="text/javascript" language="javascript">
function RunFile(fileName) {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run(fileName, 1, false);
}
</script>
And then on server-side call the javascript function as so:
this.ClientScript.RegisterStartupScript(this.GetType()
,"somekey"
,"RunFile('"+file_parameter_on_server_side+"')",true)";
More details and full example here.