Jquery slider. On Slider Stop raise server side event - asp.net

i would like to raise a server side event at the mouseup event of the jquery slider.
How can i accomplish this? Can you point me a good place to start?
The server side code i would like to call is
Private Sub LoadBlock(ByVal AA As Integer)
'A lot of stuff here
End Sub
The value gathered from the Jquery slider event should be passed as a parameter in the procedure above
My current JQuery is
<script type="text/javascript">
$(document).ready(function () {
var Country = ['<% =String.Join("', '", arrayString)%>'];
$('#slider-range-max').slider({
max: '<%= arrayLength%>',
min: 0,
value: '<%= iValue%>',
slide: function (event, ui) {
var splitValues = Country[ui.value].split("~");
$('#lblGame').html(splitValues[0]);
$('#hpHome').html(splitValues[1]);
},
stop: function (event, ui) { }
});
});
</script>
Finally this is not a homework!!!

<script type="text/javascript">
$(document).ready(function () {
var Country = ['<% =String.Join("', '", arrayString)%>'];
$('#slider-range-max').slider({
max: '<%= arrayLength%>',
min: 0,
value: '<%= iValue%>',
slide: function (event, ui) {
var splitValues = Country[ui.value].split("~");
$('#lblGame').html(splitValues[0]);
$('#hpHome').html(splitValues[1]);
},
stop: function (event, ui) {
//bof:AJAX hit
var path="YourPageName.aspx/NewMethod";
$.ajax({
url:path,type:"POST",cache:"false",
dataType:"json",contentType:"application/json; charset=utf-8",
data:"{'str1':'Some Temp String'}",
success:function(response){alert("response is "+response.d);
},error:function(){
}
});
//eof:AJAX hit
}
});
});
</script>
and in code behind:
<System.Web.Services.WebMethod> _
Public Shared Function NewMethod(str1 As String) As String
'make same name of variable as in json data
'do your server side stuff
Return "Success"
End Function

I don't have tested this code but certainly this will help you
take one button
<asp:Button ID="img_update" runat="server" Text="Submit" style='display:none' OnClick="img_update_Click" />
your method non-static method
protected void img_update_Click(object sender, EventArgs e)
{
//your code -behind
}
Now your jquery will looks like
<script type="text/javascript">
$(document).ready(function () {
var Country = ['<% =String.Join("', '", arrayString)%>'];
$('#slider-range-max').slider({
max: '<%= arrayLength%>',
min: 0,
value: '<%= iValue%>',
slide: function (event, ui) {
var splitValues = Country[ui.value].split("~");
$('#lblGame').html(splitValues[0]);
$('#hpHome').html(splitValues[1]);
},
stop: function (event, ui) {
document.getElementById('client id of button').click();
}
});
});
</script>
I hope this will give you better idea how to implement non-static method regards.....:)

Related

Error in Focus on textbox using asp.net web appln

In the below code i have a textbox when focus comes to my textbox it should call serverside codebehind file using ajax but in my case i can get success alert but it is not moving to serverside and return the value pls help me to fix the issue.
code:
<script type="text/javascript">
$(document).ready(function () {
$("#<%=txtField.ClientID%>").bind("focus", function () {
$.ajax({
type: "POST",
url: "<%=Request.FilePath%>/txtField_GotFocus",
data: "{foo:'whatever'}",
success: function (msg) {
alert(msg); //awesome, it works!
},
error: function (xhr) {
}
});
});
});
</script>
<asp:TextBox ID="txtField" runat="server" AutoPostBack="true" ClientIDMode="Static" OnTextChanged="txtField_TextChanged"></asp:TextBox>
codebehind
public static string txtField_GotFocus()
{
string foo = HttpContext.Current.Request["foo"];
//code...
return "awesome, it works!";
}
You function should look like the following
[WebMethod]
public static string txtField_GotFocus()
{
string foo = HttpContext.Current.Request["foo"];
//code...
return "awesome, it works!";
}
Here is a sample link
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

j-query date-picker does not work after post back

I have a j-query date-picker that does not work after the first post back. It only works before the post back but the date-picker does not work after post back. the text-box that shows the date-picker is wrapped with ajax update panel. here is my j-query:
<script type="text/javascript">
$().ready(function () {
$('.date-picker').mousedown(function () {
$('.date-picker').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function (dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
});
</script>
<style type="text/css">
.ui-datepicker-calendar {
display: none;
}
</style>
Your issue is that on a partial post back, which is what happens when an UpdatePanel causes a post back, the DOM is not reloaded and thus your $().ready(function () { will not fire and wire up the date picker. This is one of those ASP.NET AJAX gotchas.
One way to deal with this is to have a JavaScript function invoked when the PageRequestManager does the partial post back via the UpdatePanel, like this:
<script type="text/javascript">
$().ready(function () {
$('.date-picker').mousedown(function () {
$('.date-picker').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function (dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
});
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function() {
// Re-bind your jQuery objects here
});
</script>
Note: This is one of those places where jQuery and ASP.NET AJAX do not play nice without a little tweaking.
You should use a rebind for every page load like this :
function pageLoad() {
$('#mydatepick').unbind();
$('#mydatepick').datepicker();
};

What is the reason behind popup opening only first time

I have a strange problem and i m not able to understand why it is happening.
I called a page - Remarks(say) by jquery ajax on row click of grid viw. Then i binded that page (coming in response) into a div - dvRemarks(say). This div is opening in a popup.
Pop-up window is only opening first time, which is working fine. But when i click second time, data is coming in response, But this time popup is not opening. Problem is with popup only but i don't understand why it is ?
When i again refresh the page, it again opens up Ist time only.
Below is jquery :-
jQuery(function() {
// Remarks
jQuery('#<%=dvRemarks1.ClientID %>').dialog({
autoOpen: false,
width: 600,
modal: true
});
// Remarks Link
jQuery('#lnkDialog').click(function() {
jQuery('#<%=dvRemarks1.ClientID %>').dialog('open');
return false;
});
});
Below is the function which i am calling on click :-
function Call_Ajax(id)
{
var d = new Date();
var n = d.getMilliseconds();
var parameters="id=" + id;
$.ajax({
type: "POST",
url: "Remark.aspx",
data: {id:id, n:n},
success: function(response) {
$('#<%=dvRemarks.ClientID %>').html(response);
$("#lnkDialog").click();
},
error: function() {
alert('Some problem has been occured.');
}
});
}
And below is the div - dvRemarks in which i am binding response
<div id="dvRemarks1" runat="server" style="display: none;" title="Enter Remarks">
<div id="dvRemarks" runat="server">
</div>
</div>
Thanks.
Not sure on this but give a try to below one.
jQuery(function() {
// Remarks
jQuery('#<%=dvRemarks1.ClientID %>').dialog({
autoOpen: false,
width: 600,
modal: true, //Calling destroy on close function might help
close: function() {
$(this).dialog("destroy");
}
});
// Remarks Link
jQuery('#lnkDialog').click(function() {
jQuery('#<%=dvRemarks1.ClientID %>').dialog('open');
return false;
});
});
give a try changing ajax call to
function Call_Ajax(id)
{
var d = new Date();
var n = d.getMilliseconds();
var parameters="id=" + id;
$.ajax({
type: "POST",
url: "Remark.aspx",
data: {id:id, n:n},
success: function(response) {
$('#<%=dvRemarks.ClientID %>').empty().html(response); //empty function may be of some help here
$("#lnkDialog").click();
},
error: function() {
alert('Some problem has been occured.');
}
});
}

Trying to use a jquery-ui combobox in asp.net webforms

I'm trying to use the jqueryUI combobox into my asp.net 3.5 webforms application. I've added a dropdownlist and modified it style with jquery. The problem i got is when i try to execute the postback the dropdown normally does when it selected item it's changed. The combobox doesn't change it's value and I'm getting the error that _dopostback is not defined in my firebug error console. I've been reading about this here and in and in in the asp.net forums, and found some answers that told me that should give a try to the GetPostBackEventReference method, but still nothing has happened. Below is the code, thanks.
<script type="text/javascript">
(function ($) {
$.widget("ui.combobox", {
_create: function () {
var input,
self = this,
select = this.element.hide(),
selected = select.children(":selected"),
value = selected.val() ? selected.text() : "",
wrapper = $("<span>")
.addClass("ui-combobox")
.insertAfter(select);
input = $("<input>")
.appendTo(wrapper)
.val(value)
.addClass("ui-state-default")
.autocomplete({
delay: 0,
minLength: 0,
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response(select.children("option").map(function () {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>"),
value: text,
option: this
};
}));
},
select: function (event, ui) {
ui.item.option.selected = true;
self._trigger("selected", event, {
item: ui.item.option
});
_doPostBack('<%= ddlModalities.UniqueID %>', "");
},
change: function (event, ui) {
if (!ui.item) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
valid = false;
select.children("option").each(function () {
if ($(this).text().match(matcher)) {
this.selected = valid = true;
$(select).change();
return false;
}
});
if (!valid) {
// remove invalid value, as it didn't match anything
$(this).val("");
select.val("");
input.data("autocomplete").term = "";
return false;
}
}
}
})
.addClass("ui-widget ui-widget-content ui-corner-left");
input.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
$("<a>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.appendTo(wrapper)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-button-icon")
.click(function () {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
input.autocomplete("close");
return;
}
// work around a bug (likely same cause as #5265)
$(this).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
});
},
destroy: function () {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
$(function () {
$("#<%=ddlModalities.ClientID %>").combobox();
});
</script>
<div class="ui-widget">
<asp:DropDownList runat="server" ID="ddlModalities" Width="150px" AutoPostBack="True"
DataSourceID="odsModalitiesNoWorklist" DataTextField="Ae" DataValueField="Id"
CssClass="ddlStandardWidth" OnDataBound="ddlModalities_DataBound" OnSelectedIndexChanged="ddlModalities_SelectedIndexChanged" />
</div>
It looks like you're calling "_doPostBack". However, the ASP.NET-generated function is "__doPostBack" - there are two "_" characters at the beginning, not just one. That could be the cause of your "function not defined" error.

How can I access ViewData in a PartialView from jQuery in MVC?

I have a View loading a PartialView via jQuery. The PartialView has ViewData that the jQuery needs to access, but I can't seem to get to it from the jQuery. The code below uses the HighChart jQuery plugin (and it is just test code taken from the HighChart example page):
<script type="text/javascript">
var chart1; // globally available
$(document).ready(function () {
$('#reportButton').click(function (event) {
var actionUrl = '<%= Url.Action("UserReport") %>';
var customerId = $('#customersId').val();
var month = $('#monthsId').val();
var employees = '<%= ViewData["Employees"] %>'
alert(employees);
$.get(actionUrl, { customerId: customerId, month: month }, function (data) {
$('#result').html(data);
getHighChart();
});
});
});
function getHighChart() {
var employees = '<%= ViewData["Employees"] %>'
alert(employees);
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'chart-container-1',
defaultSeriesType: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: [employees]
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4]
}, {
name: 'John',
data: [5, 7, 3]
}]
});
}
</script>
This jQuery is in the main view, because I don't know how I could put it in the PartialView with the document ready function. But by putting the call to the HighChart function in the callback of the get() function it works in the partial view. But accessing the ViewData doesn't...
How can I achieve this? I need to get the data from the PartialView action method in order to populate the HighChart data...
The problem is the way ASP.NET traverses the HTML document when rendering.
Even though you have this code executing after the $.get call has finished:
var employees = '<%= ViewData["Employees"] %>'
The ViewData property will get evaluated render-time (e.g before this code is executed).
What i recommend:
Declare a hidden field in your partial view, and place the ViewData in there:
<input type="hidden" id ="Employees_ViewData" value="<%: ViewData["Employees"] %>" />
That will get evaluated when the partial view is rendered.
Then you can access it via jQuery:
var employees = $('#Employees_ViewData').val();

Resources