ラベル call の投稿を表示しています。 すべての投稿を表示
ラベル call の投稿を表示しています。 すべての投稿を表示

2012年3月28日水曜日

Reset Button Onclick Adds WebForm_DoPostBackWithOptions

I'm trying simply call JavaScript to reset the form and this is added, I'm guessing, from script manager. When I click the reset butt on now, rather than resetting the form I get a postback. Any idea why script manager does this and what I can do to prevent the postback?

Hi Travich,

Here is the sample that you can reference to.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager><%=DateTime.Now.ToString()%> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input id="Reset1" type="reset" value="reset" style="display:none" /> <asp:Button ID="Button1" runat="server" Text="ResetForm" OnClientClick="resetForm();return false;"/> <script language="javascript" type="text/javascript"> function resetForm(){ $get("Reset1").click(); } </script> </form></body></html>

I hope this help.

Best regards,

Jonathan

Reset scroll after call back

Hi,

How do I reset the scroll position of the page when a call back has returned?

Cheers, WT.

http://aspnet.4guysfromrolla.com/articles/111704-1.aspx

assign using client side script

document.body.scrollLeft = 0;

document.body.scrollTop = 0

2012年3月26日月曜日

response.redirect cannot be called in a callback call

And I really need to.. anyone can tell me the reason why? The problem is that in the callback I call some session variables and if the session has expired, instead of throwing an exception, I want to redirect the user to the login page.

Any idea will be awesome!!

Thanks

check for session before use it

Session["Value"] ! = null

Also you can redirect the user to the login page for example if the session has been expired by handling Session_End method in the Global.asax


Can you post the code of your callback function?


Well... maybe I need to explain myself better. I have a base class from which all my ASPX file inherit; in this class I have this code:

1 protected override void OnPreInit(EventArgs e)

2 {
3 if (Context.Session !=null)
4 {
5 if (Session.IsNewSession)
6 {
7 string cookieHeader = Request.Headers["Cookie"];
8 if (!String.IsNullOrEmpty(cookieHeader) && cookieHeader.IndexOf("ASP.NET_SessionId") >= 0)
9 {
10 if (Page.IsCallback)
11 {
12 //This is where I need to redirect the user in the callback
13 }
14 else
15 {
16 Response.Redirect("~/SessionExpiredPage.aspx",true);
17 }
18 }
19 }
20 }
21
22 base.OnPreInit(e);
23 }

So I basically check if the session has expired and if so, I redirect the user to a session expired page. I want to do it in the same class, but I have not found a way to stop the callback, redirect or anything... any advice is welcome.

Thanks in advance


What error are you getting?

hi, i'm assuming your talking about a page method callback? if so I don't think there is anyway to re-direct the whole page in the way you describe. You would just end up re-directing the callback call if you get what I mean. Your gonna have to handle this on the client-side

Couple of ways you could acheive what your trying to do.(these are untested theories)

if you using an updatepanel, if the session has timed out, add this to the response

string script = string.Format("document.location.href = '{0}');","loginPage.aspx");

scriptManager.RegisterClientScriptBlock(Page,typeof(Page),"redirect", script,true);

if your using a page method callback it's a little more difficult

if the session has expired you could use a cookie

HttpCookie objCookie = new HttpCookie("MyCookie");
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add("sessionExpired","true");
DateTime dtExpiry = DateTime.Now.AddDays(1);
Response.Cookies["MyCookie"].Expires = dtExpiry;

then in your client-side callback function, check for the existance of this cookie and re-direct using document.location.href (but be sure to clear the timeout cookie)

Cheers Si


thanks for the answer... and you're right.. there is no way to make that callback redirect...

Response.Redirect doesnt work with AJAX Extensions Beta 1 (Atlas)

I was using the July Atlas CTP and I decided to give the new AJAX Extensions beta a try... now anytime I call Response.Redirect() when I handle an event for a control that is inside an UpdatePanel I get an error that says, "Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed." This happens regardless of whether I'm doing a partial postback or a regular postback as long as the control that caused the event is inside an UpdatePanel.

I found it odd that ScriptManager.IsInAsyncPostBack == true when I clicked a button that wasn't assigned to an AsyncPostBackTrigger... I'm assuming that that probably has something to do with why the Response.Redirect() didn't work. (Yes, I did verify that UpdatePanel.UpdateMode was set to Conditional.)

This is all fine with me I guess as long as there's another way to redirect to another page in server side code. Anyone know what I need to do?

Thanks,
Jon

Calling Response.Redirect() during an async post is supported. Make sure you have the ScriptModule on web.config for this work:

- Federico

  
 <httpModules> <add name="ScriptModule" type="Microsoft.Web.UI.ScriptModule, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpModules>

Right after I posted the original message I noticed that I had put the <httpModules> stuff in the wrong place. :)

return a html table with ajax C#

hi

I am writing a website using C#, web services and ajax. I have my ajax web service call returning either an array or a DataTable object. Does anyone have any example of writing either one of these returned values to a html table using javascript?

thanks

C

It might help http://dotnetslackers.com/articles/ajax/ASPNETAjaxGridAndPager.aspx

return alert msg on ajax call

Hello guys

Quick question. Scenario: I have a update button that does an AJAX call and updates DB. I want to return a success/failure message such that it shows up as an alert message box on UI rather than showing a lblMessage and updating its visiblity and text property. Any suggestions?

Thanks in advance.

Hi There,

Try following:

protectedvoid Button1_Click(object sender,EventArgs e)

{

// Alert with messagebox after callback

// Your process here e.g. add/edit/delete from database and etc

// Assume your process take 5 second to complete

System.Threading.Thread.Sleep(5000);

// Build your alert script

StringBuilder sbScript =newStringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>");

sbScript.Append("alert('hello world!')");

sbScript.Append("</script>" );

// Make use ScriptManager to register the script

ScriptManager.RegisterStartupScript(this,this.GetType(),"@.@.@.@.MyCallBackAlertScript", sbScript.ToString(),false);

}


Hi,

You can write a javascript function taking a string parameter on your page having an alert function call displaying the message in the parameter. I have posted a similar solution in this forum. You can have a look at that. I hope you find it useful. It is at http://forums.asp.net/t/1122462.aspx .

If you have any problem in that, let us know.



Thanks for this d4dennis..it worked..but I am not happy man yet. I have two questions..first, on postback is this still sitting on the client page. If so, is there any chance it might called again unwantedly?

Second, I was hoping to trap the Sys object of ajax. I have seen that when there is an exception thrown, it shows up as an alert message on screen. I want to somehow be able to trap that guy. Any ideas there? sorry for being so picky but that's what I want.

This works and right now to continue, I am using this. thanks again.


Hi There,

This is the solution i can think of.

tinuverma:

First, on postback is this still sitting on the client page. If so, is there any chance it might called again unwantedly?

THIS WILL NOT POP UP ON POSTBACK

tinuverma:

Second, I was hoping to trap the Sys object of ajax. I have seen that when there is an exception thrown, it shows up as an alert message on screen. I want to somehow be able to trap that guy. Any ideas there? sorry for being so picky but that's what I want.

IM NOT SURE THIS IS ACHIEVEABLE, IF SOMEONE HAVE ANY IDEA. ANYWAY, YOU CAN RAISE THIS AS ANOTHER QUESTION.

2012年3月24日土曜日

Returning results from a page method call

In my ASP.NET AJAX web site, I am attempting to use Page Methods to return data from the web server. I would like to be able to return the value from a page method like so:

function GetValue()
{
return PageMethods.GetValueFromServer();
}

function ManipulateValue()
{
var myValue = GetValue();

// Do something with myValue
}

Unfortunately, calling a page method requires the use of a callback function. I really need to retrieve the value without having to store it in a global variable in the callback function; is there any way to do this?

function GetValue(){ return PageMethods.GetValueFromServer( function(result){// The result that is returned from server //Now do what ever you would like to do.} );}
Maybe you can try something like this

KaziManzurRashid:

function GetValue()
{
return PageMethods.GetValueFromServer(

function(result)
{
return result;
}

);
}

Maybe you can try something like this


I tried your code, returning the result in the nested callback function (see above), but when I returned to the code that calls GetValue() the value that ends up getting returned is undefined. :-(


Yes that becasue the ajax call is async. it does not wait for the ajax call to complete, why do not you call the GetValue in the Callback?


KaziManzurRashid:

Yes that becasue the ajax call is async. it does not wait for the ajax call to complete,why do not you call the GetValue in the Callback?



Okay, I'm somewhat confused. I call GetValue(), which calls an asynchronous server method and is passed an in-line Javascript callback function. Are you saying that I should, in the callback function, call GetValue() again? If so, won't that just cause an infinite loop?


Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call the GetValue.


KaziManzurRashid:

Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call the GetValue.

Okay, I tried that but still no luck. Here is the code I am using; perhaps you can shed some insight on to what I'm doing wrong? It looks like this is, in fact, causing an infinite loop.

Javascript code:

function PrintDateTime()
{
var dateTimeFromServer = RetrieveDateTime();
$get("myLabel").innerHTML = dateTimeFromServer;
}

function RetrieveDateTime();
{
return GetAjaxValue();
}

function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
return RetrieveDateTime();
}
);
}

C# code:

[WebMethod]
public static string GetCurrentDateTime()
{
return DateTime.Now.ToString();
}


You can do it in the following way:

function GetAjaxValue(){ return PageMethods.GetCurrentDateTime( function (result) { $get("myLabel").innerHTML = result; } );} function showCurrentDateTime(){GetAjaxValue();}

Just call the showCurrentDateTime in your code when you want to show it.


KaziManzurRashid:

You can do it in the following way:

function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
$get("myLabel").innerHTML = result;
}
);
}
function showCurrentDateTime()
{
GetAjaxValue();
}

Just call the showCurrentDateTime in your code when you want to show it.

Thanks for the time you've spent addressing my questions, Kazi. Unfortunately, in my code I really need to return the value in the RetrieveDateTime() function. The code in PrintDateTime() is just used for illustration; I need to do more than just put it in a label. To clarify, here's exactly what I want to do:

var myValue = RetrieveDateTime();

The value is not being displayed on screen but is being used by logic within a JavaScript function. I really just want to be able to call a JavaScipt function and return a value that I can use in the rest of the code.


Hi, Well you have to undertand the nature, it is always. and thats why the best place to assume the value is availabe is the Callback. If you can describe more of your scenerio then I can help.


Hi,

Thank you for your post!

It seems that you have misunderstanding on AJAX and callback.

The result cann't be obtained like this:

var myValue = GetValue();

It must be obtained like this:

function SetSessionValue(key, value)
{
PageMethods.SetSessionValue(key, value,
OnSucceeded, OnFailed);
}

// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
displayElement.innerHTML = "Current session state value: " +
result;
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
displayElement.innerHTML = "An error occurred: " +
error.get_message();
}
}

For more information about callback and webmethod, seehttp://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx

If you have further questions,let me know.

Best Regards,

Returning web service result from calling function in javascript (Atlas)

When using Atlas is there a way that I can call a web service method and have the result returned from the calling function. I understand that Atlas works asynchronously and therefore uses callback functions but I need the calling function to return the result somehow.

I tried something similar to the following but it didn't work. It seems to be that the global variable only gets updated once the calling function completes.

var getResult;
function LMSGetValue(param) {
// Note that the LMSAPI is setup with a ScriptManager
LMSAPI.LMSGetValue(param,LMSGetValueComplete);
pause(1000);// helper function that pauses by # millisecs
return getResult;
}
function LMSGetValueComplete(param) {
getResult = param;
}

The reason that I have to return the result from the calling function is that the javascript that calls the function (which is located in a iframe) follows a strict specification called SCORM which is used for building sharable elearning content and learning management systems (LMS). The learning content in the iframe calls the LMSGetValue function to get the data from the LMS and expects a return value.

Any help or suggestions on how I might be able make this scenario work would be grately appreciated as I really am quite stuck.

Unfortunately there is no current support for synchronous webservice calls, you cannot return a value from your method directly, the results are only available thru your callback.