Archive for December, 2005

One of the email i got after the crosspage posting is :-
I was asked  how can i use crosspage page posting with masterpages and get the values of the previous page.
So I thought let me give 2 simple scenario of getting the same back first with non master pages and with master pages.
Here you go:-
 
here is the code ith simple page without master:-

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<head runat=”server”>

<title>Untitled Page</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>

</div>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” PostBackUrl=”~/Default2.aspx” />

</form>

</body>

 

and here is the Default2 code in pageload:-

 

protected

void Page_Load(object sender, EventArgs e)

{

if (IsCrossPagePostBack)

{

TextBox1.Text = ((TextBox)PreviousPage.FindControl(“TextBox1”)).Text;

}

}

 

In MasterPage Scenario:-

<%@ Page Language=”C#” AutoEventWireup=”true” MasterPageFile=”~/MasterPage.master” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” PostBackUrl=”~/Default2.aspx” style=”left: -296px; position: relative; top: 81px” />

</asp:Content>

Here is the default2  load :-in default2.cs

 

protected

void Page_Load(object sender, EventArgs e)

{

if (IsCrossPagePostBack)

{

TextBox1.Text = ((TextBox)(((ContentPlaceHolder)PreviousPage.Master.FindControl(“ContentPlaceHolder1”)).FindControl(“TextBox1”))).Text;

}

 

 

}

Note :- I have not provided the code for default2.aspx

For details checkout here in msdn2:-

http://msdn2.microsoft.com/library/ms178139(en-us,vs.80).aspx

Advertisements

As you Know the Asp.Net1.1 model was built in with Self posting that is Autopostback of forms and was a single form model although there are hooks like Paul Wilson’s multiform(i ll rather say it hooks ..hope Paul; u don’t mind .Developers from ASP Background always found this a s a limitation as they tend s to post (Submit )to a different Page .Microsoft provides few Changes in this arena to provide the facility of Crosspage posting and the Wizard control to handle such scenario an demands.

 

So how Cross page posting works: – here is the desc from the Quick start site

 

Cross page posting sets action of the form in Page 1
When the Button is clicked, the post goes from Page 1 to Page 2
Page 2 retains viewstate from Page 1

 When PreviousPage accessed, viewstate is re-populated to an instance of Page 1

 Page 1 then executes up to its LoadComplete phase (excludes rendering)

 At this point, Page 2 may access the values of controls in Page 1

 

Now for differentiating the post back and crosspage post back you can check  like (if (!Page.IsCrossPagePostBack)

{
handle a normal request here
}

Also Note that you can Postback to different Application

as well although the previouspage would be NULL here

See for the info to the quickstart site.

 

However still you can’t directly access the previous page control and you need to do like

(Textbox)Page.PreviousPage.FindControl to get the val.

 

Saying all these more question arises what happen when i need to submit (postback) to same page from multiple sources..Now how can i handle the same????

 

I am not going to provide the in-depth behavior here as Fritz and Scott Allen already had written the workarounds…

Read the same here 2 wonderful posts on the subject:-

 

Multi-source cross page posting

Design Considerations for Cross Page Post Backs in ASP.NET 2.0 

 

 

However still you can’t directly access the previous page control and you need to do like

(TextBox)Page.PreviousPage.FindControl to get the val.

 

 

ALSO you might think how you ll get the values in master pages scenario.Let say you submitted to a differnet page and now you wants to get the previous page control values.In Master pages scenario you need to first fidn the container an dthen use the findcontrol to get the value of the previous page.hope this helps

 

IN ASP.NET 2.0 you will find a new folder called APP_CODE .The Purpose of this folder is to share resources thru out the app…That is it can contain source code for utility classes and business objects (.cs..etc)which you want to compile as part of your application.In dynamic compilation upon the first request the codes in this folder are compiled and those get recompiled if any code changes happen. Further you can get strongly typed by placing arbitary type.

 

As I told the code in this folder is accessible thru out to Ur application it gives a certain advantage for code managing.

 

The above features is a part of the New Compilation model in asp.net 2.0 .Compilation of ASP.NET 2.0 no longer produce a Single Assembly (As many Devs provided feedback that a huge assembly is taking quite lot of time for a huge application.).Thus in 2.0 we have now multiple assemblies may be based on the directory structure except the root (Yet needed to be sure ).That is 1 assembly for the Root and subsequent assemblies for the directory struct (one for each).

 

However There are few points needed to taken care before using the App_Code which otherwise create many doubts on your mind (at least I was thinking it’s a missing feature, bydesign or I am wrong for a whileJ).

 

The issue is if you are putting a class (say a1.cs) outside the App_Code and if you are trying to access the same in codebehind file (say webform1.cs) it ll produce the Compilation error as it no longer recognize ur class in CodeBehind.The same is the story the reverse side that is if you are trying to access the CodeBehind or UserControl class inside the Class (say a1.cs ) in App_Code the same ll be the result Compilation Error.

I found that Classes in the Different directory other then App_Code are producing this behavior.

Thus Beware of this if you are migrating any 1.1 apps to 2.0 byhand make sure to place the file appropriately that is place the .cs (utility classes  )in App_code and look at the workaround below:-

 

So now the question arises what is the workaround and why this happens

 

The workaround is simple that is add a reference <%@ Reference … … %> to the reqd classes.However if you don’t have a provision to reference then what you ll do???One solution for the same would be to create a base class put it in App_Code and inherits from it.

 Coming to the design the best Guess for this behavior is seems by design and since all Directory compiled to individual assembly and all have a reference bydefault to the app_code Again providing a Reference from App_Code Classes might causes a circular references.

 

The Questions here are there are problem here when I am developing controls and it can put a certain limitation on me when  i need to ref a class in anotrher directory  .Further I guess if I am putting a class outside the app_code and not in any directory(in root only) then it should be part of the Root assembly but since i am not able access the same class in any codebehind which is in the RootDirectory I need to more digged into the compilation model.

 Update

I am editing this entry as i found quite a few hit on this entry.Thus just a point to mention is that although the above behavior is true we can now have the old behvaior with the new Webproject model shipped in to handle the compilation in alittle different way.For the new compilation check the Fritz onion’s article in msdn and for the new Webproject model check my post here

 

After the 10 must tools every developer should have its now turn for the  10 must addins everydeveloper should have.

 
 
  • NUnit to write unit tests
  • NDoc to create code documentation
  • NAnt to build your solutions
  • CodeSmith to generate code
  • FxCop to police your code
  • Snippet Compiler to compile small bits of code
  • Two different switcher tools, the ASP.NET Version Switcher and the Visual Studio .NET Project Converter
  • Regulator to build regular expressions
  • .NET Reflector to examine assemblies

Addins:- http://msdn.microsoft.com/msdnmag/issues/05/12/VisualStudioAddins/default.aspx

TestDriven.NET
GhostDoc
Paster
CodeKeep
PInvoke.NET
VSWindowManager PowerToy
WSContractFirst
VSMouseBindings
CopySourceAsHTML
Cache Visualizer