Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Wednesday, March 28, 2012

How do I use JavaScriptSerializer on only certain properties of an object?

I have an object that inherits from UserControl. I want to seralize certain properties in this object using JavaScriptSerializer (or another method that does the same thing). Unfortunatly, if I try to seralize the control itself, I get a crash.

I am forced to make another object that contains all the properties that need to be seralized. The properites in my control then refrence the properties of that object. Though this works, it leaves my control with a bunch of properties which are effectively nothing more than pointers to another object that exists soley to make javascript seralization to work.

Is there way around this? I want to be able to use JSON on a usercontrol and be able to specify which properties I want seralized to JSON.

Try using the ScriptIgnore attribute in the properties you don't want to be serialized.

Hope this helps,

Elias.


I tried to override every virutal property on usercontrol and I still got "Cannot get inner content of because the contents are not literal."Something in my base class (usercontrol) seems to be causing problems

How do I reset a ModalPopup?

How do a reset the contents of a ModalPopup that has been closed (using the .Hide() method or by clicking the cancel button) to it's initial state (at page load)?

I am using a ModalPopup control that contains a UserControl that allows users to search for records in a database. If they find a match, they can click the 'Select' link in the GridView (where the subsequent event handler calls the ModalPopup.Hide() method). They can also click on the Cancel button to close the popup if they don't find what they are looking for.

Everything is working perfectly, except after the ModalPopup has been used once, subsequent use shows the ModalPopup in the exact state it was when it was closed, either by the .Hide() method or the cancel button.

Is there a way around this?

If its a usercontrol in the modalpopup why not create a public reset method in the usercontrol to set all the values back to "" or whatever needs to be done. You could call that right before your .hide(). It sounds like the viewstate of the controls are on. You may be able to kill viewstate on your usercontrol or set initial values, but that could potentially cause problems depending if your user control posts back a bunch of times.

HTH,

AjaxButter


I have started down the road of creating a .ResetForm() method for the user control and will see if that works.

I am hopeful that your suggestion about disabling the ViewState will work, since ideally the ModalPopup should be at it's initial state whenever it's loaded and this would involve the least amount of effort (and code to test). The only issue here might be the use of UpdatePanels for partial-page postbacks (both in the main page on in the popup), so it might not work as hoped.

I guess my best option will be to leave the ModalPopup.TargetID property blank (if it's not required) and handle showing the popup from a button Click() event that resets the UserControl first.

Thanks for the assistance.


Creating a reset event in the UserControl and calling it after certain events works for simple controls (manually created and handled forms). There are still some issues remaining...

Trapping the cancel button click event and performing a reset there causes a full page refresh since it's not wired up through the ModalPopupExtender control (and it can't be part of the UpdatePanel in the popup). Maybe I'm doing something wrong here?

If the ModalPopup contains a UserControl with a FormView (for inserting & updating), the data remains as is after the cancel button is clicked. I tried disabling the ViewState, but since the page has not been refreshed, the FormView stays the same. I also tried creating a _Click() method for the cancel button, but the event doesn't seem to get called for some reason.

Do I have to do some funky client-side scripting to get this to work?


Nevermind... I did some more playing and figured out what I was doing wrong. Staring at it for too long sometimes prevent you from seeing the forest through the trees! Wink

If I moved the cancel button into the UpdatePanel in the popup (and left the ModalPopupExtender.CancelControlID property blank), then this started to work as hoped (without the full page refreshes).

Resetting the FormView inside the UserControl turned out to be as simple as calling for the FormView.ChangeMode() method and setting it to the default mode (insert in this case) and then calling the DataBind() method (may not be needed?).

If anyone else agrees, please mark this post as an answer.

Saturday, March 24, 2012

How clientscript member Authentication last the whole browse session than a page

I use asp.net ajax client method to do member Authentication

Sys.Services.AuthenticationService.login(username.value,password.value, true ,null,null,null,null,"User Context");

It successed in current page. but when I redirecit to another page, the Authentication dissapeard

How clientscript member Authentication last the whole browse session

I found I have wrong setting in my config files

How can i use dataSource control, let it binding to page method but not web service

such as

<dataSource id="listDataSource" autoLoad="true" serviceURL="MyService.asmx" />

it binding to a web service named MyService.asmx,How can i binding it to a page method?

thanks

A data source can only declaratively bind to a DataService.

To bind it to other data, you can obtain the data programmatically (e.g. from a page method), and set the data property of the data source to the returned data.

Alternatively, you could directly bind the content (e.g. the ListView control) directly to the data, without using the data source control.

Thanks,

Wednesday, March 21, 2012

How can I tell when an updatepanel has finished updating?

Hi,

Hopefully this is a simpe question! I have a couple of UpdatePanels on my page, and at the top a "Save all" button that calls the "Update" method on each of these updatepanels which gets them to save their contents. Immediatly after doing the updates, I need to render a graph of the data. The problem I have is that the UpdatePanel "updates" are all asynchronous, so my graph doesn't always pick up the newly saved information - because its still being saved while the graph is created.

What I need to do is either get the UpdatePanels to update synchronously, or have some way to fire server-side code once they have finished.

Is this possible?

Thanks!

Matt

Why not update the graph at the end of your "Save all" event handler, if that's what it is you want to do?

Hi,

You cann't process multi-Asyncpostbacks at the sametime, You can process them one by one when you click "Save all".

Following is a demo:

<%@. 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">
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Label1.Text = "The time is: " + DateTime.Now.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Label2.Text = "The time is: " + DateTime.Now.ToString();
}
protected void Button3_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Label3.Text = "The time is: " + DateTime.Now.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>UpdatePanelTutorialIntro1</title>
<style type="text/css">
#UpdatePanel1 {
width:300px; height:100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="padding-top: 10px">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Processing…
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend>UpdatePanel</legend>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend>UpdatePanel</legend>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<legend>UpdatePanel</legend>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
</div>
</form>

<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
var thebutton;
function BeginRequestHandler(sender, args)
{
$get('UpdateProgress1').style.display = 'block';
thebutton = args.get_postBackElement();
thebutton.disabled = true;
}
function EndRequestHandler(sender, args)
{
$get('UpdateProgress1').style.display = 'none';
thebutton.disabled = false;
var str = thebutton.id
if(document.getElementById("Button" + (parseInt(str.substring(6, str.length)) + 1)))
document.getElementById("Button" + (parseInt(str.substring(6, str.length)) + 1)).click();
}
</script>

</body>
</html>

Best Regards,


Thanks very much. Your example shows me that I can call the buttons "click" manually using the EndRequestHandler, to then go and call the next one. I think I can use that information to achieve what I was after :)


I have a similar situation. You'll have to register two events EndRequest, InitializeRequest and queuing system.

Please refer tohttp://forums.asp.net/p/1167326/1944939.aspx#1944939 for the code example.

Let me know if this helped.

How can I tell the difference between an AJAX and a regular postback in the Page_Load even

Is there anyway to determine if the page is called through an AJAX or a regular postback in the Page_Load method, kinda like you can determine if it is a postback or the initial request via the Page.IsPostBack property?

I hoped the Page.IsAsync could be used for this but it doesn't seem to work like I expected.

I think when an AJAX does a post back you cannot access any controls like Label's, buttons etc...so try accessing a label by going label.text, if it fails catch the Exception...not a good way to do it, but a cheaky option for a quick test.


if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

this can differentiate those methods.


What you're after is ScriptManager.IsInAsyncPostBack.


Sathesh_pandian:

if the button clicked or postback occur, the page.ispostback will be true.

if you are using ajax then the page.ispostback will be false.

Test that.

You'll find that Page.IsPostBack is actually true during partial postbacks.


That's it. ScriptManager.IsInAsyncPostBack does the trick. Thank you.Big Smile


it will not work when we are using it for different page.

calling another page.


That's because that isn't a postback.

How can I pass additional parameters to the web service or how can I call a method in the

I have done a ton of research in this area and there just does not seem to be a way to pass any addiional parameters to the Web Service reference in the autoextneder call. Is there a way that this can be done. In my world, the list of suggestion is dependant on other data on my web form or web user control. I had read some references that you might be able to call a method in the code behind page, however everything i have tried on this has failed. Here is a list of what I have attempted

Tried to add query string arguments to the service path property (error is thrown trying to render the page)

Tried to set the service path to a web page (aspx) and then call the code behind method (this also threw error trying to render the page)

Viewed some alternative solutions from vrious posts...however most we out of date t the current release (RC1).

I see that David Reed has an example at Infiinity Loop, however his server is unavailable at this time.

I would love for the auto extender to be able to natively support calling the code behind method of the page or user control that is using it. It would seem logical that the derived suggestion should be able to present a filtered list based on other data. It would be nice if I could pass it an array of string that represent name value pairs of data that I can then access in the web service and pass to whatever is building the list. Anyway just a suggestion...maybe the functionality is there and I just can not figure it out

Please help

>> I have done a ton of research in this area and there just does not seem to be a way to pass any addiional parameters to the Web Service reference in the autoextneder call. Is there a way that this can be done. In my world, the list of suggestion is dependant on other data on my web form or web user control.

With some digging around in the AutoCompleteExtender you may be able to modify the javascript to do this - I've never looked at it personally but that is where I would start. You would need to hijack that part that reads from the target control and add other data to the call.

>> I had read some references that you might be able to call a method in the code behind page, however everything i have tried on this has failed.

The following RC1 example of using a page method works on my system, but it's not going to solve your problem. The page method has to be static, so you won't be able to access any form data.

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><%@. Register Assembly="BenValidationControls" Namespace="ben" TagPrefix="hauna" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><script runat="server"> [System.Web.Script.Services.ScriptMethod] [System.Web.Services.WebMethod] public static string[] AutoCompleteMethod(String prefixText, int count) { return new string[] { "foo", "baz" }; } </script><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" /> <div> <asp:TextBox ID="TextBox1" runat="server" /> <asp:AutoCompleteExtender ID="AutoCompleteExtender1" ServiceMethod="AutoCompleteMethod" ServicePath="~/default.aspx" TargetControlID="TextBox1" MinimumPrefixLength="1" runat="server" /> </div> </form></body></html>

Thanks for the response. I am looking at modifying from the jvavascript as you have suggested. Here is also what I have found out...

First off the code behind must be in an aspx page and not in a ascx. I am not sure why that should matter but apparently it does. it would be really be nice if I could reference the code behind on either type of file. If you are trying to make a reusable control within your app with the auto complete extender then this is somewhat limiting. Also what I did not know or see document anywhere that this function needed to be static. Once I did that it started working quite well.

Thank you very much on this. This actually helped quite a bit.


You were referring to my derived AutoCompleteExtender that enables callbacks... I had some server issues but its back up and running :) This would solve your problem perfectly.

http://weblogs.asp.net/infinitiesloop/archive/2006/11/15/ASP.NET-Ajax-Beta_3A00_-AutoCompleteBehavior-without-a-Web-Service.aspx