Access Javascript function on Default.aspx from web Control - c#

I have some ajax functions on my default.aspx that I use for saving data to my db. I have the form on my web control. When I try calling the function from my button which resides on the web control. I get an error.
I need to call that function from my registerform.ascx. How do I do that from within the registerform code behind?
function ShowAvailability() {
$.ajax({
type: "POST",
url: "Default.aspx/CheckEmail",
data: '{usermail: "' + $("#<%=subs_email.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response);
}
});

use
ButtonID.Attributes.Add("onclick", "ShowAvailability()");
if this wont work
provide the detail of the error u r getting.

It seems I was doing it all wrong.
This code seems to be working:
OnClientClick="return ShowAvailability()"
Initially I had onClick

Related

calling ajax server-side method with mappage route c#

Salam aleykum, i'm trying here to call an ajax server-side method using the mappage route but it always says: POST 404 (Not found)
here is the code c#:
[System.Web.Services.WebMethod]
public static bool RemovePhotofromAlbum(string list_photos_hotel)
{
.....
return true;
}
and here the jquery code should like :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: $(location).attr('pathname') + "/RemovePhotofromAlbum",
data: '{list_photos_room_type: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
....
},
failure: function (response) {
alert(response.d);
}
});
}
Without using the mappage route it's working.
but here i want to use the mappage route
i know that there is a problem with the URL in the ajax method but i don't know how to fix it.
any help would be appreciated.
:)
If it's impossible just tell me
$(location).attr('pathname')
This line is used for add attribute in DOM
So if you want to save baseurl,Keep it in web.config, hidden field or any other js file and than use it
You should use $(location).attr('href') and not $(location).attr('pathname')
and you have an error with your parameter name it should be 'list_photos_hotel' and not 'list_photos_room_type'
try this :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: $(location).attr('href') + "/RemovePhotofromAlbum",
data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.d);
}
});
}
assuming you run the script from the same aspx page your server method runs.
Edit :
Because you use map route, you get 404. you should pass the physical location.
Your method is in the path : Manage/admin_2/index.aspx :
function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
type: "POST",
url: "/Manage/admin_2/index.aspx/RemovePhotofromAlbum",
data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
alert(response.d);
}
});
}

$.post or $. ajax post in c# .net

I have implemented the following $.Post Jquery function in my asp.net page MAIN Page.aspx
the
$.post({ url: "MAIN Page.aspx",
data: { "Status":ddl },
contentType: "application/json; charset=utf-8",
dataType: "json",
});
where ddl is my data to be posted ...i have placed this $.post in the document.ready() and i tried accessing the posted value in the code behind as
var status= Request.Form.Get["Status"]...
but when i run the code the value of the status is null..
please help and guide if i am correct in my implementation of the logic
Try like this:
$.post('MAIN Page.aspx', { status: ddl }, function(result) {
alert('success');
});
and on your server:
var status= Request.Form["Status"];
Also you haven't shown what this ddl javascript variable is and how you assigned it a value but make sure that it is not a complex object but it contains some simple type.
Alternatively if you want to use the $.ajax method:
$.ajax({
url: 'MAIN Page.aspx',
type: 'POST',
data: { status: ddl },
success: function(result) {
alert('result');
}
});

Call code behind method in jquery, not finding the method

My code behind:
[WebMethod]
public bool accountExists(string username, string password) {
//code...
}
My jquery:
$.ajax({
type: "POST",
url: "MyPage.ascx/accountExists",
data: JSON.stringify({ username: txtUsername.val(), password: txtPassword.val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d)
},
error: function(msg) {
alert("ERROR: " + msg.d)
}
});
I always reach the alert where it says "ERROR: " + msg.d.
MyPage.ascx is located in a folder "Controls", so I have tried to set the url: "Controls/MyPage.ascx/accountExists" without any change.
ASP.NET AJAX Page Methods are intended to run inside of .aspx pages and not .ascx user controls.
Move your WebMethod logic into an .aspx page and update the AJAX call via jQuery.

Redirect after an ajax request in a click event

I'm using JScript + ASP.NET. I got a form with 2 inputs (user and password) and a button. What I'm trying to do is to:
1- Fire a click event
2- Look inside a database if the user exist
3- Give back the answer
4- If the answer is true, POST some data to an other page AND redirect to it.
I first tried to do this with ASP.NET. To POST data with ASP.NET I need to use PostBackUrl property, but the problem is that PostBackUrl ignore my click event.
I then tried to do this with jscript. On my click event (jquery), I use $.ajax to POST data to access my database, give the answer back in json...and I'm stuck there. In both method, I'm stuck at point 4.
ASP.NET
protected void SignIn_OnClick(object sender, EventArgs e)
{
Clients client = (Clients)clientDAO.getUsername(text1.Text, password2.Text);
if (client != null)
{
Session.Add("SessionNoClient", "1272");
Session.Add("CurrentQuote", "-1");
Session.Add("UnitSystem", "0");
Session.Add("SessionAdministrator", "0");
//How to redirect with POST here
}
}
JScript:
$("#m_bLogin").click(function () {
var username = $("#text1").val();
var password = $("#password2").val();
var form = $("#formClient");
$.ajax({
url: '../../Class/LoginAjax.asmx/GetLoginInformation',
data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
form.submit();
},
error: function (XMLHttpRequest, textStatus, error) {
alert(error);
}
});
});
You could do something like that:
$.ajax({
url: '../../Class/LoginAjax.asmx/GetLoginInformation',
data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
form.submit();
},
error: function (XMLHttpRequest, textStatus, error) {
alert(error);
}
}).done(function() {
window.location.href = "YourNewPage.aspx";
});

jQuery Ajax passing the Form Collection

I was wondering if it is possible to pass the forms collection from within an ajax method in jQuery?
$.ajax({
type: "POST",
url: "/page/Extension/" + $("#Id").val(),
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#result").html(msg);
false;
}
});
If it is possible to pass in the forms collection then when it arrives at the method within c# how to you read it in?
You can do something like this:
var form = $("#myForm").serialize();
$.post("/Home/MyUrl", form, function(returnHtml)
{
//callback
});
Then on the C# side you should be able to do something like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyUrl(FormCollection collection)
{
//do what you gotta do
}
I think this should work.
EDIT: I just noticed that I simply assumed you were referring to ASP.NET MVC. If not, let me know as this answer is specific to MVC.
You can use the Ajax.BeginForm method from ASP.NET MVC. It will use Microsoft's ajax to do the request, but you can have a JQuery method execute upon completion. Or you can use an UpdatePanel and register a javascript to run with the ScriptManager once the UpdatePanel loads. Another thing you could try is using a jquery like the following: $(':input') to get a collection of all input, textarea, select and button elements(JQuery documentation), and pass that as the data into your request.
Got this working all okay, and it returns the input form collection in the FormCollection
input = $(':input')
$.ajax({
type: "POST",
url: "/page/Extension/" + $("#Id").val(),
data: input,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#result").html(msg);
false;
}
});

Categories

Resources