The idea is that this div contains a quote from a customer that is retrieved from the server via a get random function, and every few seconds a jQuery runs that fades this quote out and brings another one into view.
This is my Div code in my .ascx:
< div class="testimonial" ID="Fader" onload="runTestimonial">
<q>"<asp:Literal runat="server" ID="Quote"></asp:Literal>"</q>
</div>
Code Behind (.ascx.cs):
protected void runTestimonial(object sender, EventArgs e)
{ --lots 'o code--
Partnership partnership = Partnership.GetRandomTestimonial(cmPage.CMPageId);
if (partnership != null)
{
Quote.Text = partnership.Testimonial;
Visible = true;
}
}
I am using this jQuery code:
setInterval(
(function () {
$('#Fader').fadeOut('slow', function () {
setTimeout(function () { $('#Fader').load().fadeIn('slow'); }, 300);
});
})
, (200))
The jquery should be fine. It links into the Div's Fader ID and does the fading and loading.
Originally the div generated the quote using a Page_Load method of the same structure and this worked. Now the change is I need to call it when I need to, not on Page_Load but on jQuery refresh.
So far I have the div refreshing in and out, but it's blank (If I revert it to the on Page_Load method, the same quote comes in and out). It's not getting to the ASP line or it's not executing it. I can just not get runTestimonial to work at all like the Page_Load does, probably because I don't know how to call it.
I don't know how to do C#, jQuery ASP or code behinding stuff really. Please help!
These are the steps of what you need to do, with jQuery and WebMethod:
1) You will change your runTestimonial() function into a WebMethod that will return a string (the random testimonial). So your function's signature would look like this:
[WebMethod]
public static string runTestimonial()
{
return randomTestimonial; //include your code
}
2) Add jQuery library in the head of your file.
<script src="http://code.jquery.com/jquery-latest.js"></script>
3) Create a function that will do an ajax call to your webmethod.
function getTestimonial()
{
$.ajax({
type: "POST",
url: "Default.aspx/runTestimonial",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Fader").html(msg); //msg contains the response from the server, this appends it to the div
}
});
}
4) Your div in your markup will NOT be a server control, so remove the onload. So it will look like this:
<div class="testimonial" ID="Fader"></div>
5) We are almost done, just make sure to call your getTestimonial() function inside your setInterval.
The [WebMethod] attribute is found in the System.Web.Services library, so make sure to include it at the top of your page:
using System.Web.Services;
That's all, that should work.
To put it simply, C# is server side, so the method runTestimonial doesn't exist on the client's browser. You need to create a javascript function that uses jQuery to call to the server with an ajax request. I would suggest checking out some tutorials on jQuery/ajax/ASP.Net. It seems like you are missing some of the fundamentals. Hope that helps!
Related
Is there a way to change the values for these two attributes on the client side and have it reflected on the server side after the postback. I tried it but it does not seem to work. I wanted to have one button on the page that I would delegate submits too, and assign these two arguments on the client side. Seems like not possible. Any idea?
Assuming there is a button named "cmd" in the form
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#<%=cmd.ClientID %>").click(function () {
$(this).attr("CommandName", "do").attr("CommandArgument", "arg2");
});
});
</script>
If one checks the value after postback they are still the same as they were before postback.
I tried you're code and it works fine.
Just make sure you're button is not generating a postback by adding OnClientClick="return false;":
<asp:Button ID="cmd" runat="server" Text="Button" OnClientClick="return false;"></asp:Button>
Also you won't see the difference in "view source" on your browser. But the change has been made in the DOM. Use firebug and add the console.log to see for yourself:
$("#<%=cmd.ClientID %>").click(function () {
$(this).attr("CommandName", "do").attr("CommandArgument", "arg2");
console.log(this);
});
The console.log(this) gave me the following:
EDIT:
If you think about it. If the button creates a postback, then the button will reset itself to normal once the page loads again.
EDIT #2:
I don't need the change on the client
side, I need it on the server side.
That was the whole point of the
question. I need to see the change on
the server side, and it does not seem
to be possible. – epitka
Okay... Well, in that case. It is not possible. "CommandArgument" and "CommandName" means nothing to the client and is not accessible.
However there are work arounds. But depending on the context of your application they might not be useful to you.
You could try using your own attributes like the answer suggested here.
Or you could execute the __doPostBack on the client side and pick up the __EVENTARGUMENT on the code behind.
(The link button is there to generate the __doPostBack function by asp.net.)
Like such:
<script type="text/javascript" language="javascript">
function DoPostBack() {
__doPostBack('cmd', 'thesearemyarguments');
}
</script>
Page:
<asp:Button ID="cmd" runat="server" Text="Button"
OnClientClick="DoPostBack(); return true;"
onclick="cmd_Click" ></asp:Button>
<asp:LinkButton ID="LinkButton1" runat="server" Visible="false">LinkButton</asp:LinkButton>
Code Behind:
protected void cmd_Click(object sender, EventArgs e)
{
Response.Write(Request.Params["__EVENTARGUMENT"]);
}
I was having the same problem here, I found the solution was to use an ajax call to send my buttons id to a function where i can set it as a session variable. Because the asp control I wanted to update could not be accessed from within a static call. On success of the ajax call I click a hidden button which uses a non static click event to manipulate the session variable i set and update the control
My links were generated within a repeater, and they correspond to different rooms of a house. When you click on the link there is another repeater that has to update to show products that are sold which are relevant to the room of the house that was clicked on
my link that is generated from the repeater
<%#Eval("DocumentName") %>
my client side method
$('.changeroom').each(function () {
$(this).on('click', function () {
var id = $(this).attr('data-id');
var object = { 'sender': id };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/App/Page Templates/FindByRoom.aspx/UpdateRoomID",
data: JSON.stringify(object),
success: function() {
$('#btnID').click();
}
});
});
});
btnID is a simple aspButton with a server side click event
and finally my server side methods
protected void btnChangeRoom_OnClick(object sender, CommandEventArgs e)
{
int id = 0;
if (Session["RoomID"] == null) return;
Int32.TryParse(Session["RoomID"].ToString(), out id);
if (id == 0) return;
//do something with your buttons id
//i updated the path of a repeater and reloaded the data
}
[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void UpdateRoomID(string sender)
{
HttpContext.Current.Session["RoomID"] = sender;
}
I have web page in ASP.NET. I have created Tree View control with jQuery. I drag items from the tree and drop into the div element.
<div id="rows" runat="server" class="droppable">
</div>
I add items to that div using .append function from jQuery.
$(".droppable").append(event.originalTarget.innerHTML);
It works like I want. But now I want to get all dropped items from ASP.NET code. I use following code:
protected void Button2_Click(object sender, EventArgs e)
{
HtmlGenericControl control = (HtmlGenericControl)Page.FindControl("rows");
Label1.Text = control.InnerHtml;
}
But it doesn't work. I've also tried InnerText function, but still nothing. I've also put the button and label controls into UpdatePanel so the page doesn't refresh and my dropped item are still in div element.
How can I get dynamically added items from ASP.NET code.
Lukas
Your append() call simply changes the DOM structure. ASP.NET has no idea you did this.
You need to store your changes into a hidden "state" field on the page, and in your code-behind pluck them out.
var droppedItem = event.originalTarget;
$(".droppable").append(droppedItem.innerHTML);
$("#myHiddenStateField").get(0).value += "," + droppedItem.id;
Code behind:
string[] droppedItemIds = myHiddenStateField.Value.Split(",");
ASP.NET will only be able to work with form elements.
So if these rows are for example (input[type=text]) you can do this:
Request.Form["rows"]
EDIT
When the user drags over the element why don't you create a new hidden input and put the relevant value inside of it. This will make it easy to grab the value from the server with the example I used above.
better yet why not use jquery's ajax on droppable's success event and record each drop via asp.net's PageMethod, then you don't have to deal with parsing html inside your droppable element.
this should get you started
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
here is an actual example i used
$('.droppable').droppable({
accept: '#dragList > li, .columnRight li',
activeClass: 'ui-state-highlight',
hoverClass: 'hoverBorder',
drop: function(ev, ui) {
$.ajax({
type: "POST",
url: "yourPage.aspx/AddDroppable",
data: "{'id':'" + ui.draggable.context.id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Result").html(msg.d);
}
});
}
});
I have the following javascript:
<script type="text/javascript">
function showjQueryDialog() {
$("#dialog").dialog("open");
}
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true,
buttons: { "Renew Membership": function() { $(this).dialog("close"); } }
});
});
</script>
I have an asp:Button on the page which logs the user it. This is the sample of what I want to occur when the button is clicked on the server-side:
protected void LoginButton_OnClick(object sender, EventArgs e)
{
UserProfile profile = UserProfile.GetUserProfile(txtUserName.Text);
TimeSpan ts = profile.Expiration.Subtract(DateTime.Now);
if(ts.Days <= 30)
//call showJQueryDialog() to open the dialog box
Page.ClientScript.RegisterStartupScript(typeof(Login2), "showjquery",
"showJQueryDialog();", true);
else
//log the user in as normal.
}
Also is how would I attach a method such as the following to the Renew Button on the Dialog
public void Renew()
{
Response.Redirect("Renew.aspx");
}
As calling client side function is not possible I would suggest to emit in javascript the information required for the decision and make everything happen on the client side.
Alternatively you can do need a page reload, as suggested from previous commenter.
if(ts.Days <= 30)
ScriptManager.RegisterStartupScript(
typeof(MyPage), "showjquery",
"$(document).ready(function() { showJQueryDialog(); };",
true
)
else
//log the user in as normal.
Put that right where you have: //call showJQueryDialog() to open the dialog box
Update 1: You seem to be using an update panel, in that case you need to use ScriptManager.RegisterStartupScript
Update 2: You also want to wrap the js call in a jquery .ready call, so it isn't triggered before the dialog has been configured. This is better than hooking up the body onload because onload waits for images to be loaded so .ready will show sooner (depending on the images and other bits of info loaded).
I really don't understand Freddy's approach to this at all. I am misunderstanding something maybe. The way I see it, there are only two possibilities here, as devdimi point out. Either:
a) Do all the logic in the client-side onClick javascript. You could call an AJAX method that performs the action in the server-side OnClick, then call your jQuery popup in the AJAX callback.
b) Do a postback, handle the server-side OnClick, then attach javascript for the page that runs in the body onLoad event:
body.Attributes.Add("onLoad", "showJQueryDialog();")
I would keep a hidden LinkButton and then call the __doPostBack method in javascript.
<asp:LinkButton runat="server" ID="Renew" OnClick="Renew_Click" style="display:none" />
jQuery
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true,
buttons: { "Renew Membership": function() {
$(this).dialog("close");
__doPostBack('Renew', '');
// or if inside a master page something like this
__doPostBack('ctl00$ContentPlaceHolder1$Renew', '');
} }
});
});
I have a somewhat similar issue with IE8.
We're using ASP.NET and anytime we do a Response.Redirect within the PageLoad/Control-Events IE8 sets all the base DOM objects to undefined (Image, window, document)
But if we do the redirect during the PreInit event then IE8 is fine.. Lovely
asp.net 2.0 / jQuery / AJAX
<script type="text/javascript">
//updated to show proper method signature
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(hideMessage);
function hideMessage(sender, args)
{
var ctl = args.get_postBackElement();
//check if ctl is the disired control
//hide user notification message
}
</script>
i have several controls on the page that might initiate the AJAX request, but i only want my js to fire when i click one particular button. how do i check what control initiated the request so i can fire JS accordingly.
EDIT: I worked around it, but I'd still like to know if I can do it this way.
Clarification: I can't call the JS from onclick event, because the page is inside of the UpdatePanel, and i only want the JS to execute when AJAX Request ends and it was triggered by one particular button on the page. On server side, i set the myLabel.Text to some text, and then js checks if the $(myLabel.CliendID)'s innerHTML is not blank and fires the js. checking the innerHTML is my work-around since i can't figure out how to check the "sender" of AJAX Request. Hope this makes more sense now.
edit2: I've read some documentation, and turns out you CAN check the "sender" control.
Thank you.
This is what I am doing in my code to identify what control has initialized the request. All javascript code.
function pageLoad() {
if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequest);
}
}
function endRequestHandler(sender, args) {
if (sender._postBackSettings.sourceElement.id == '<%= gvResults.ClientID %>') {
//do something because of this...
}
}
function initializeRequest(sender, args) {
if (CheckForSessionTimeout()) {
args.set_cancel(true);
}
else {
if (sender._postBackSettings.sourceElement.id == '<%= gvResults.ClientID %>') {
//do something because of this
}
}
}
EDITHere is the method of checking for timeout on the client side.
var sessionTimeoutDateTime = new Date();
var sessionTimeoutInterval = <%= this.SesstionTimeoutMinutes %>;
function CheckForSessionTimeout() {
var currentDateTime = new Date()
var iMiliSeconds = (currentDateTime - sessionTimeoutDateTime);
if (iMiliSeconds >= sessionTimeoutInterval) {
ShowSessionTimeout();
return true;
}
return false;
}
I would recommend that you do not have each control execute the same javascript function. OR, if they do, pass a parameter that indicates which one executed it.
Then, you can include your ajax in the js function that the control executes.
And, if I'm not understanding the issue correctly, perhaps you could explain it in more detail or post some code.
I've read some documentation, and turns out you CAN check the "sender" control. JS in the question is updated to show the proper method signature.
This article gives even better explanation.
I have an asp.net page with a save button within an updatepanel and contenttemplate. The save works nicely, but I am trying to add a "wait" gif while the save is happening using JQuery, but the ajaxStart event is not firing. I put a simple catch shown below:
$(document).ajaxStart(function () {
alert('starting');
}).ajaxStop(function () {
alert('done');
});
No alerts show when I click the save. Is there a problem when trying to capture ASP.net Ajax events, is asp doing some funky type of Ajax calls that can't be captured by Jquery?
Thanks, let me know if you have any ideas about this,
Mark.
The ASP.NET update panels seem to do their own thing... Tap into the PageReuqestManager and setup your own calls here...
EDIT
I simplified the functions a bit below to match your sample a little more...
<script type="text/javascript">
function pageLoad() {
if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(AjaxEnd);
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AjaxBegin);
}
}
function AjaxEnd(sender, args) {
alert("I am done...");
}
function AjaxBegin(sender, args) {
alert("I am about to start...");
}
</script>