Whidbey Watcher #3: X-Page postback oddities

Una delle caratteristiche maggiornmente attese della versione 2.0 di ASP .NET è sicuramente il cross-page postback, ossia la possibilità di effettuare il submit di alcuni elementi di una web form ad una "pagina" differente. Il tutto, inoltre, in maniera estremamente facile... Un semplice:

<asp:TextBox ID="searchKeywords" Runat="server">asp:TextBox>
<
asp:Button Runat="server" ID="btnDoSearch" Text="Find" PostBackUrl="~/SearchResult.aspx" />

Permetterà al button btnDoSearch di effettuare submit verso il web form SearchResult.aspx al cui interno possiamo recuperare il valore della casella di testo searchKeywords con un banale:

string keywords = ((TextBox)this.PreviousPage.FindControl("searchKeywords")).Text;

Perchè la proprietà PreviousPage della classe Page è così definita in MSDN:

The Page object representing the page that transferred control to this page

Però (c'è sempre un però...) MSDN dice anche:

The PreviousPage property is null when the user requests that page directly from the server.

Cosa comporta questa precisazione? In poche parole, se ad effettuare il cross-page post è proprio la pagina SearchResult.aspx, la proprietà PreviousPage vale null, e dobbiamo quindi correre ai ripari:

string keywords = string.Empty;
if (this.IsPostBack)
    keywords = ((
TextBox)this.FindControl("searchKeywords")).Text;
else
   
keywords = ((TextBox)this.PreviousPage.FindControl("searchKeywords")).Text;

Pensate che non possa capitare in un caso reale? Ne dubito, e nei workshop Whidbey e Whidbey: Reloaded vi faccio vedere perchè... :-D

posted @ martedì 15 giugno 2004 14:06

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
Please add 6 and 5 and type the answer here:
 

Live Comment Preview:

 
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011