I want to update the test variable through ajax & C# backend code. I have set test variable as string builder in .cs file & want to update the value of the same variable through .cs code using ajax while clicking on div tag.
Code in .aspx:
<asp:UpdatePanel ID="upre" runat="server">
<ContentTemplate>
<%=Convert.ToString(test) %>
</ContentTemplate>
</asp:UpdatePanel>
Ajax code:
$(".mb-2").click(function (e) {
$.ajax({
type: "POST",
url: "one.aspx/Subscribe",
contentType: "application/json; charset=utf-8",
data: '{"name":"test","email":"test#gmail.com"}',
dataType: "json",
success: function (msg) {
if (msg.d) {
alert(msg.d);
}
},
error: function (req, status, error) {
alert("Error try again");
}
});
return false;
});
Backend Code:
[WebMethod]
public static string Subscribe(string name, string email)
{
AllApps abc = new AllApps();
abc.iPriceFrom = 50;
abc.iPriceTo = 100;
abc.test();`enter code here`
return "thanks " + name + ", your email " + email + " is subscribed to our newsletter.";
}
I tried with the above code but it is not updating the variable value at the front side. Data is updating from backend (.cs) file. I want to update the test variable through ajax & C# backend code. I have set test variable as string builder in .cs file & want to update the value of the same variable through .cs code using ajax while clicking on div tag.
Related
I run into a little bit of a problem executing `AJAX request.
When specifying contentType and dataType, success section is not executing.
However, when omitting that, it is executing, but is showing the entire content of generated html page.
Here is my AJAX call:
$.ajax({
type: "POST",
url: "Default.aspx/GeneratePdfs",
data: '{frequency: "' + $('#ddlFrequency option:selected').text() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#rptDisplay').text(data);
alert("1");
},
failure: function () {
// $('#rptDisplay').text("Error");
alert("2");
}
});
This is code behind:
[System.Web.Services.WebMethod]
public static void GeneratePdfs(string frequency)
{
string test = frequency;
HttpResponse response = HttpContext.Current.Response;
response.Write(test);
}
This is the fragment of html page:
<div id="rptDisplay" class="well" runat="server" clientidmode="Static">
</div>
I need to display data returned from Web Method in my div section.
What am I doing wrong?
Hope this will help you:
contentType :When sending data to the server.
dataType: The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response .
Please find the code:
[System.Web.Services.WebMethod]
public static string GeneratePdfs(string frequency)
{
string test = "frequency";//Hard Code value
HttpResponse response = HttpContext.Current.Response;
return test;
}
Design:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<div id="rptDisplay" class="well" runat="server" clientidmode="Static">
</div>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/jquery-ui-1.8.20.min.js"></script>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "Default.aspx/GeneratePdfs",
data: '{frequency: "' + $('#ddlFrequency option:selected').text() + '" }',
contentType: "application/json;charset=utf-8",
// dataType: "text/Json",
success: function (data) {
debugger;
if (data.d!="") {
$('#rptDisplay').text(data.d);
}
alert("1");
},
failure: function () {
// $('#rptDisplay').text("Error");
alert("2");
}
});
</script>
i am calling a function on onclick, onmousemove and onkeypress event in form tag and the function is in code behind but the method is not executing when user clicking, pressing a key and moving mouse on form1. The code is :
<form id="form1" runat="server" onclick="idletime()" onkeypress="idletime()" onmousemove="idletime()">
.cs file :
protected void idletime(object sender, EventArgs e)
{
Response.Write("hello.");
}
You can use ajax to do that. I am writing down some rough sample code. Hope it may help.
<form id="form1" runat="server" onclick="idletime()" onkeypress="idletime()" onmousemove="idletime()">
<script>
function idletime() {
$.ajax({
type: "POST",
url: "demo.aspx/functionName1",
data: "{parameter1:'" + param1 "',parameter2:'" + param2 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
}
</script>
c# code
using System.Web.Services; --- use this class
[WebMethod]
public static string functionName1(string parameter1, string parameter2)
{
------------------------------;
return "Demo";
}
I am stuck with a critical issue.Can some one please suggest.
I have an .ascx page on which there is a pop up which has an aspnet button which must redirect to another page on click.
I followed the following steps:
On ascx page after opening colorbox, this function is called on clicking the button.
$.ajax({
type: 'POST',
url: "/HomeLoan/ProductConfirmationPop_SaveData.aspx/btnSaveData",
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (response) {
alert(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + textStatus + errorThrown);
}
});
On aspx page
[WebMethod]
public static void btnSaveData()
{
function sahil();//this function i want to call after this function is being called
}
I am getting Json parse error.
I removed dataType:json and made it return a html/text then it is giving me object object error.
This would propably be a lot easier if you could use a simple html form, add the value to a hidden input when onsubmit is triggered, and post it to a webservice which will redirect for you.
Anyway:
You need to decorate your btnSaveData method with another attribute and change the return type / parameter to string (or any other type that suits your needs):
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string btnSaveData(string color)
{
// do sth with the color
return "/SaveSuccess.aspx";
}
And in js:
// retrieve the color as a string (this is up to you - I don't know what sort of ColorPicker you're using, so this is a placeholder)
function saveColor()
{
var color = $('#myColorPicker').color.toString();
// create the data for the webmethod,
// the parameternames have to be the same as they are on the WebMethod
var colorData = "{ color:'" + color + "'}";
$.ajax({
type: 'POST',
url: "/HomeLoan/ProductConfirmationPop_SaveData.aspx/btnSaveData",
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: colorData,
success: function (response) {
window.location.href = response.d; // redirect on success
},
error: function (response, text, error)
{
alert(response + ' ' + text + ' ' + error);
}
});
Hope this helps and works!
I am making an Ajax request with data to a Page Method in my code behind in my ASP.NET Web Forms application. I have a Panel control in my aspx page but I cannot get the control from that Page Method with its ID. I can get it from the Page_Load event though. What can I do to get the Panel control from the page method or is it not possible or maybe an alternative?
<asp:Panel ID="pnlImages" runat="server"></asp:Panel>
<script type="text/javascript">
function GetProductId() {
$.ajax({
type: "POST",
url: "Default.aspx/GenerateQrCode",
data: "{'Products':" + JSON.stringify(data) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
},
success: function (msg) {
alert('Success');
}
});
}
</script>
[WebMethod]
public static void GenerateQrCode(List<Product> Products)
{
foreach(var product in Products)
{
string qrCodeLocation = products.Where(pid=>pid.ProductId == product.ProductId).Select(s=>s.QrCode).FirstOrDefault().ToString();
Image image = new Image();
image.ID = product.ProductId.ToString();
image.ImageUrl = qrCodeLocation;
//Cannot get 'pnlImages' here
}
}
You won't be able to do that, WebMethod doesn't follow the same flow as normal page methods. It's not even a instance method but static.
You'll have to return the information on client and create the image there using javascript.
Can I call a method which is in an asp.net web page (in aspx.cs page) without checking it in the pageload?
for example '../test2.aspx/TestMethod
In this link I have noticed that we can give url: "PageName.aspx/MethodName", in jquery ajax method. I have tried it and never works for me.
Page Methods is a new mechanism in ASP.Net application where the server code cab be bound to Asp.Net pages
To enable Page methods we need to drag a ScriptManager control to the page and mark
the EnablePageMethods to “True”.
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="True">
</asp:ScriptManager>
Go the Code behind file of the page and add a static method
[WebMethod]
public static string HelloWorld(string name)
{
return string.Format("Hi {0}",name);
}
In javascript function we can use PageMethods object to call the WebMethod of the page.
<script type="text/javascript">
function GreetingsFromServer() {
var name = 'Jalpesh';
PageMethods.HelloWorld(name,OnSuccess, OnError);
return false;
}
function OnSuccess(response) {
alert(response);
}
function OnError(error) {
alert(error);
}
</script>
you can use ajax call for another page webmethod
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});