I've been trying to run the code if any of the conditions get true. Its a dialog box which popups after postback created on Jquery Impromptu. I've tried to run the same code on a different project and its working fine however It doesn't show anything the condition met true
ASPX CODE
<!-- JavaScript -->
<script type="text/javascript" src="Scripts/jquery.js"></script>
<script type="text/javascript" src="Scripts/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="Scripts/wufoo.js"></script>
<script type="text/javascript" src="Scripts/jquery-impromptu.2.6.min.js"></script>
<script type="text/javascript">
function confirmSubmit() {
$.prompt('Are you sure you want to submit?'
, {
buttons: { Ok: true, Cancel: false }
, callback: confirmSubmitResult
}
);
return false;
}
function confirmSubmitResult(v, m, f) {
if (v) //post back if the user clicked OK
$('#<%# btnSubmit.ClientID %>').click();
}
</script>
.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
TextBox2.Attributes.Add("readonly", "readonly");
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string prompt = "<script>$(document).ready(function(){{$.prompt('{0}!');}});</script>";
string message = string.Format(prompt, "This message is coming after postback");
if (TextBox2.Text.Length == 0 || TextBox1.Text.Length == 0)
{
ClientScript.RegisterStartupScript(typeof(Page), "message", message);
}
A little help will be highly appreciated
Related
So when using signalR. I followed this example and got it working on a test web form, where in I open 2 tabs of the same page and tested it out:
SignalR Tutorial
Now I tried to modify it a little bit and tried to have 1 page as a sender and another as a receiver,
Page with Button to send message:
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var msg = $.connection.myHub1; //change MyHub1.cs should be in camel myHub
// Create a function that the hub can call to broadcast messages.
msg.client.broadcastMessage = function (name, message) {
// $("[id*=btnRefresh]").click();
};
// Start the connection.
$.connection.hub.start().done(function () {
function RefreshData() {
// Call the Send method on the hub.
msg.server.send('admin', 'Refresh Grid');
};
});
});
</script>
protected void btnSendMsg_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "SigalRFunction", "RefreshData()", true);
}
Page with gridview:
<asp:Button runat="server" ID="btnRefresh" OnClick="btnRefresh_Click" Style="display: none" />
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var msg = $.connection.myHub1; //change MyHub1.cs should be in camel myHub
// Create a function that the hub can call to broadcast messages.
msg.client.broadcastMessage = function (name, message) {
$("[id=btnRefresh]").click();
};
// Start the connection.
$.connection.hub.start().done(function () {
function RefreshData() {
// Call the Send method on the hub.
//msg.server.send('admin', 'Refresh Grid');
};
});
});
</script>
protected void btnRefresh_Click(object sender, EventArgs e)
{
grdview.DataSource = grdviewData();
grdview.DataBind();
}
My idea was every time a message is received, the grid view/page should should automatically refresh. The grdviewDatasource and Databind works i.e placed it in pageload.Sadly nothing happens.
script src="assets/js/app.min.js"></script>
<script src="assets/js/scripts.js"></script>
<script src="assets/js/custom.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.10.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
Let me start by saying that you can invoke the hub's method either from client-side or code-behind code. Since it is not clear which way you want I'll cover both.
Your hub, MyHub1 should define the Send method, which you are going to invoke when the button is clicked:
MyHub1.cs
public class MyHub1 : Hub
{
public void Send(string name, string message)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub1>();
hubContext.Clients.All.broadcastMessage(name, message);
}
}
Note that the Send method calls the broadcastMessage javascript function (to notify clients), which you should define in the Receiver. You should add any code necessary to refresh your grid inside that function.
Receiver.aspx
<script type="text/javascript">
$(function () {
var msg = $.connection.myHub1;
// Create a function that the hub can call to broadcast messages.
msg.client.broadcastMessage = function (name, message) {
console.log(name + ", " + message);
// do whatever you have to do to refresh the grid
};
// Start the connection.
$.connection.hub.start().done(function () {
});
});
</script>
The Sender contains the two buttons: btnSendMsg will invoke the hub's Send method from code-behind; btnSendMsg2 will perform the same invocation from javascript. You can pick either depending on your needs.
Sender.aspx
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSendMsg" runat="server" Text="Server-Side" OnClick="btnSendMsg_Click" />
<input type="button" id="btnSendMsg2" value="Client-Side" />
</div>
</form>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var msg = $.connection.myHub1;
// Start the connection.
$.connection.hub.start().done(function () {
$('#btnSendMsg2').click(function () {
// Call the Send method on the hub.
msg.server.send('admin', 'Refresh Grid');
});
});
});
</script>
Sender.aspx.cs
protected void btnSendMsg_Click(object sender, EventArgs e)
{
var myHub1 = new MyHub1();
myHub1.Send("admin", "Refresh Grid");
}
Last but not least, make sure both the sender and the receiver pages reference the necessary jQuery and SignalR scripts and the autogenerated SignalR hub script.
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>
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 new to this and i am creating simple application in which i click a button and a notification on desktop should be displayed. i am doing this in windows form c#
the error is " NullReferenceException was unhandled
i have one button Notify in form1. i have tried this:
form1.cs
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.ScriptErrorsSuppressed = true;
}
private void btnNotify_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("notifyMe");
}
private void Form1_Load(object sender, EventArgs e)
{
string CurrentDirectory = Directory.GetCurrentDirectory();
webBrowser1.Navigate(Path.Combine(CurrentDirectory,"HTMLPage1.html"));
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.ObjectForScripting = this;
code for HTMLPage1.html :
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function () {
window.open("http://stackoverflow.com/a/13328397/1269037");
};
}
}
</script>
</head>
<body>
</body>
</html>
even if i simply put alert("Hi") in notifyMe() function, nothing else. still it displays the same error.
I have tried your code.. you should use
document.attachEvent('DOMContentLoaded', function () {..
Instead of
document.addEventListener("..
That worked from here...read more about it here https://stackoverflow.com/a/1695383/4155741
you should also remove that comma at the end of .. body: "Hey there! You've been notified!", as it prevent the script from be compiled.
You have to put your html and scripts in the debug directory if they are not automatically placed. Thats where getcurrentdirectory() hits.
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.