Put id in input hidden, working with jTemplate? - asp.net

I'm working with jquery.ajax() I'm getting a object and I use jTemplate to write the html. My problem is now that I need to place the id of the object in a input hidden. I have no idea how I should do this. I tried to do a <script> in the template.htm with jquery to place the id in hidden but with no luck.
Any suggestions?
this is my jTemplate html file
<div style="background-color: #ccc">
{$T.Email}
</div>
<div style="background-color: #ddd">
{$T.Password}
</div>
This is my jquery
$('a').live('click', function(evt) {
evt.preventDefault();
var id = $(this).attr('id');
$.ajax({
type: "POST",
url: "GetUserWeb.asmx/GetUser",
data: "{'value': '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
ApplyTemplate(msg);
},
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
function ApplyTemplate(msg) {
$('#Container').setTemplateURL('template.htm');
$('#Container').processTemplate(msg.d);
}

If you place the id in a JavaScript variable outside the function then you can access it in the template simply by putting {ItHere} in the template.
So do var id outside the function, and set it within the function.
var id;
$('a').live('click', function(evt) {
evt.preventDefault();
id = $(this).attr('id');
$.ajax({
type: "POST",
url: "GetUserWeb.asmx/GetUser",
data: "{'value': '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
ApplyTemplate(msg);
},
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
function ApplyTemplate(msg) {
$('#Container').setTemplateURL('template.htm');
$('#Container').processTemplate(msg.d);
}
Then you can add the id in the template like this:
<img src="HelloWorld.jpg" id="{id}"/>

Related

Unable to redirect to another page using javascript while using jquery

I am unable to redirect to another after successful login through my webservice. I am getting correct response from web service But page doesn't redirect.
<script type="text/javascript">
function registerUser() {
try {
var username = document.getElementById("UserName");
var pwd = document.getElementById("Password");
$.ajax({
datatype: "json",
type: "POST",
url: "http://localhost:51290/CMSWebService.asmx/LoginUser",
data: "{'username':'" + username.value + "','pwd':'" + pwd.value + "'}",
async:false,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("hello");
window.location.replace("default.aspx");// to redirect error occurs here
},
error: function (data) {
debugger;
if (data.d) {
}
}
});
}
catch (e) {
debugger;
alert(e);
}
}
</script>
Why don't you try window.location.replace(" full url of your page ") .
<script type="text/javascript">
function registerUser() {
try {
var username = document.getElementById("UserName");
var pwd = document.getElementById("Password");
$.ajax({
datatype: "json",
type: "POST",
url: "http://localhost:51290/CMSWebService.asmx/LoginUser",
data: "{'username':'" + username.value + "','pwd':'" + pwd.value + "'}",
async:false,
contentType: "application/json; charset=utf-8",
success: function (data) {
window.location = "./default.aspx";
alert("hello");
},
error: function (data) {
debugger;
if (data.d) {
}
}
});
}
catch (e) {
debugger;
alert(e);
}
}
</script>
Try this

Unable to find dynamically created control when JSON is used

I am calling JSON method on selected index changed of the drop down list which is as follows
function StepImplementationScript(SelectedValue,UniqueField) {
debugger;
$.ajax({
type: "POST",
url: "DynamicForm.aspx/StepImplementationScript",
data: "{strSelectedValue: " + SelectedValue + ", strUniqueField: '" + UniqueField + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(response) {
alert("OnSuccess" + response.d);
if (msg) {
msg.close();
}
},
error: function(jqXHR, exception) {
alert(jqXHR.responseText);
},
failure: function(response) {
//alert("Onfailure" + response.d);
}
});
}
In this 'StepImplementationScript' method of server side when i try to find dynamically created control, the controls are not getting find and coming as null.
Selected change event i have written in Jquery through which i have called the JSON method.
This is the same page (DynamicForm.aspx) on which controls are created dynamically.
Why this is so ? and how should i resolve this issue ???

How to make texbox to null after I got success in jQuery AJAX method

I am strangling when I added my comments with AJAX method of jQuery, after stressful, I want to make this textbox to null
my Ajax method like this :
function AddComments() {
$(".Comment input[type=text]").keypress(function (e) {
if (e.which == 13) {
var comment = $("#" + this.id).val();
var textId = "#" + this.id.replace("txt", "div");
$(textId).append(comment);
$.ajax({
type: "POST",
url: "Posts.aspx/AddComments",
data: "{'id': '" + this.id.replace("txt", "") + "','comments': '" + comment + "'}",
dataType: "json",
contentType: "application/json",
success: function (response) {
$("#" + this.id).val("");
//alert(response.d);
}
});
}
});
}
what I want is on Success, I want make current textbox to NULL
$("#" + this.id).val("");
This is a scoping issue. this in the success handler holds a reference to the window object, not the input the keypress fired on. You just need to cache the input in a variable you can use in the success handler. Try this:
function AddComments() {
$(".Comment input[type=text]").keypress(function (e) {
if (e.which == 13) {
var $input = $(this);
var comment = $input.val();
var textId = "#" + this.id.replace("txt", "div");
$(textId).append(comment);
$.ajax({
type: "POST",
url: "Posts.aspx/AddComments",
data: "{'id': '" + this.id.replace("txt", "") + "','comments': '" + comment + "'}",
dataType: "json",
contentType: "application/json",
success: function (response) {
$input.val("");
//alert(response.d);
}
});
}
});
}
you can't access this in jquery ajax do it like this save your object in any variable
function AddComments() {
$(".Comment input[type=text]").keypress(function (e) {
if (e.which == 13) {
var Element=this;
var comment = $("#" + this.id).val();
var textId = "#" + this.id.replace("txt", "div");
$(textId).append(comment);
$.ajax({
type: "POST",
url: "Posts.aspx/AddComments",
data: "{'id': '" + this.id.replace("txt", "") + "','comments': '" + comment + "'}",
dataType: "json",
contentType: "application/json",
success: function (response) {
$(Element).val("");
//alert(response.d);
}
});
}
});
}
Your success function is in another context, you can't use this, you have to send the id on the json response and do something like:
success: function (response) {
$('#'+response.div_id).val("");
}
and your json response should include the div_id that's passed on the ajax request

Accessing Data in Httphandler sent using post request

I have created a httphandler. Everything is working fine If you take the request type GET. But I really don't want GET request due to security reasons.
I am making a POST request using Jquery using following code:
$.ajax({
type: "POST",
async: false,
url: "SaveAccounts",
data: { a: "Sent Data" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(a) {
alert(a);
},
error: function OnError(request, status, error) {
alert('Error: ' + request + ' ' + status + ' ' + error);
},
complete: function () {
}
});
And the code for processing request is:
context.Response.ContentType = "application/json; charset=utf-8"
Dim s As String = context.Request.Params("a")
context.Response.Write(JsonConvert.SerializeObject(s))
This code is returning 'null' in javascript alert in client side.
Please note every thing is working fine when request type is changed to GET in $.ajax() function.
I have also tried to access posted data using context.Request.Form("a") and context.request.SserverVariables("a")
context.request.Params is a combined collection of 'Form', 'ServerVariables', 'Cookies' etc.
Please tell me where I am going wrong...??
Thanks for looking....
This is working by just removing contentType and dataType specification from the request.
Anything other than this will remain unchanged.
Try
data: { "a=Sent Data" },
e.g. from http://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Also the HttpHandlers don't like JSON data so get rid of the contentType and the dataType.
Escaping it - a more complete example
var dataString = "{ 'first' : '" + escape($('#fname').val()) +
"', 'mail' : '" + escape($('#tbMail1').val()) +
"', 'mailinglist' : '" + $("input[name='ml']:checked").val() +
"' }";
/*alert(dataString.replace('\',','\',\n'));*/ // just for debugging ;)
$.ajax({
type: "POST",
url: "myAjaxHandler.asmx/ProcessSampleForm",
data: dataString,
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$(".loadingimg").fadeOut();
window.location.replace(msg.d);
},
error:function(xhr,err){
$(".loadingimg").fadeOut();
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
Taken from http://www.jphellemons.nl/post/JQuery-with-webform-input-validation-ajax-posting-json-to-aspnet-C.aspx

JQuery and Ajax - Coming to Grips

I've read through several tutorials on the web about ajax posting with JQuery, all of them reference the response object from the web service as response / response.d -- This lead me to believe that this is the built in object for JQuery's response handler.
Code Snippet:
$('.submit').click(function () {
var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
alert(theURL);
$.ajax({
type: "POST",
url: theURL,
data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
contentType: "applications/json; charset=utf-8",
dataType: "json",
success: alert("Success: " + response.d), // this will change
failure: function (response) {
alert("Failure: " + response.d);
}
});
});
however the code is returning "Uncaught ReferenceError: response is not defined" in Chrome's Javascript console. What assumptions am I making that I need to re-evaluate.
You need to supply success with a function to execute:
success: function(response) {
alert(response.d);
}
Success (Like Failure) need a function to pass the response object through.
$('.submit').click(function () {
var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
alert(theURL);
$.ajax({
type: "POST",
url: theURL,
data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
contentType: "applications/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Success: " + response.d);
},
failure: function (response) {
alert("Failure: " + response.d);
}
});
});

Resources