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

2012年3月26日月曜日

Retrieve viewstate during a GET request

I have a page with controls that specify a filter on a set ofresults. I have used ajax and the update panel to show updated resultsthrough asynchronous request when the filter gets updated. When theuser selects an item in the set they navigate to a different page...when they then hit the back button they return to the filter page butall the controls are all re-initialized.

What I've been trying isgenerating an id for the page on the initial request (as is done andnormally placed in a hidden field for my custom PageStatePersister thatkeeps the viewstate in a database) but then redirecting the user to aurl with the id appended. So when you later hit the back button youhave the id in the URL.


Next LoadViewState is NOT calledunless the request is the result of a postback. Well I've been tryingto initialize the ViewState in the Page_Load by callingLoadPageStateFromPersistenceMedium() and that does result in callingthrough my custom PageStatePersister... But the ViewState dictionary ofthe page does not get updated... what am I missing?

Or is there another way to accomplish not losing the filter values?

Ok...this is going to be hard for me to explain..but I shall try. And i will suggest two solutions. First of all remember client side styles are not persisted unless asked, and viewstate track changes to state only when tracking is enabled. So all your controls are reinitialised because tracking gets enabled after oninit() and then viewstate just deserializes the stored data.

So unless you set trackviewState(), viewstate is not tracking anything. (Once on, cannot be turned off) Once tracking is on..viewstates items can be marked dirty(IsItemdirty()). You can also set an item to be dirty. Basically tracking allows viewstate to monitor which of its elements have changed since TrackViewState() has been called. Statebag ignores the items that are not marked dirty when it comes to saving view state SaveViewState() and hence items not marked dirty are not serialized.

So one of the solution is to markitems dirty which you want to be persisted before that postback. The other is dont do it by ajax but by actual user controls.

Just a question for you to ponder, if you have two identical pages with a textbox and a button with on onclicking code.

as <as:textbox runat="server" text="abc"><asp:button runat="server" text="submit">

with other pages only having change in its text property as text="the quick brown fox jumps over the little lazy dog" Do the two pages have same viewstate?

Read this article if you want to understand viewstate.

http://msdn2.microsoft.com/en-us/library/ms972976.aspx


I appreciate your feedback but I'm not sure how it applies... I'm not having problems writing viewstate... I'm having problems loading the viewstate during a GET request. I'll try articulating my problem differently:

1) I've persisted the viewstate on the server side.
2) The viewstate is identified by a hidden field and so is in the form data during a postback (this already works)
3) On the first GET of a page I generate the identifier and redirect the request to a page with the viewstate identifier appended to the URL
4) By default Viewstate is NOT loaded during a GET request only POSTBACK
5) On the first GET of the URL+ViewstateID, the viewstate does not exist and the page runs through normal initialization
6) On subsequent requests to URL+ViewstateID does exist and I want to load this Viewstate from storage (see 4)
7) I've tried Page.LoadPageStateFromPersistenceMedium() which does call my pagestatepersister and retrieves the viewstate... now how do I apply this to the page instance...? can I ...?


before you move on to the next page, you can either save your viewstate as I said in my previous post and write it to a session and later on retreive the session, and put it back in viewstate.

Although this can be accomplished by plain sessions.

2012年3月24日土曜日

Return of false for onkeypress event no longer blocks input

I'm not sure if this is related to the Ajax framework but the problem surfaced after I updated my Ajax Extensions and Ajax Futures and I'm searching for any possible cause.

Throughout the website I use a clientside call to do things such as limit the entry into a textbox for a Zip Code to numbers only. I do it like this: onkeypress="return validNumeric(event);" This has always worked just fine. But no longer. Letters, special characters and such can now be keyed into the textbox. I know the onkeypress event is being fired as I've traced it though and added alert message to see that false is being returned by the function validNumeric. If I change to the event onkeydown then the entry of letters/special characters is blocked again. Just onkeypress is failing.

The affected textboxes are those located in a Web Form within a Master Page - the Web Form is also wrapped by an Update Panel. If I move the textbox into the Master Page the onkeypress event works correctly. I've also created a simple website with a Web Form within a Master Page wrapped by an Update Panel and the onkeypress event works fine in all locations. It is only in my existing site.

I've included the code below for the sampme site to show the format of the production site.

MASTER PAGE:

%@dotnet.itags.org. Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function validNumeric(e)
{
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode >= 48 && unicode <= 57) return true;
if (unicode == 8) return true;
if (unicode == 9) return true;
if (unicode == 13) return true;
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="upContent" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

WEBFORM:

<%@dotnet.itags.org. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="Enter a Number"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return validNumeric(event);"></asp:TextBox>
</asp:Content>

After further testing this is not an issue with the Ajax Framework but related to how ASP.NET works. The situation arises when the ASP Textbox is located with an ASP Panel which has the DefaultButton property set. A return of false from the onkeypress event does not block the input in this case. If I remove the DefaultButton property from the Panel then the event works as required. I'll repost in the appropriate place for help with this issue. Below is a simple webpage that shows this issue. With the DefaultButton property set alpha input is not block, but without the DefaultButton property then the alpha input is blocked. Maybe it has something to do with how asp.net is handling the enter key being pressed to click the default button.

%@. Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">

function validNumeric(e)
{
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode >= 48 && unicode <= 57) return true;
if (unicode == 8) return true;
if (unicode == 9) return true;
if (unicode == 13) return true;
return false;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:Label ID="Label1" runat="server" Text="Enter a number:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"
onkeypress="return validNumeric(event);">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
</div>
</form>
</body>
</html>


I did find out how to solve my problem. By changing the onkeypress event from:

onkeypress="return validNumeric(event);" to

onkeypress="event.returnValue=validNumeric(event);"

I now get the form to limit input in the zipcode textbox to numbers only and also keep the DefaultButton property on the Panel it is contained in.