 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 26th, 2008, 04:37 PM
|
|
Registered User
|
|
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
PostBackUrl + OnClick
In wrox book "Professional ASP.NET 2.0" on page 73 it's mentioned that if am using the postbackurl for a button I can still create onclick event for the button to be executed on the source page before posting back to the target page. But this seems not to be working. I need to run some code in the click event of the button before it moves to the target page, how can I do that?
This is the exact paragraph in the book
"Notice that, when you are crossposting
from one page to another, you arenât restricted to working only with the postback on the second
page. In fact, you can still create methods on Page1.aspx that work with the postback before moving
onto Page2.aspx. To do this, you simply add an OnClick event for the button in Page1.aspx and a
method. You also assign a value for the PostBackUrl property. You can then work with the postback on
Page1.aspx and then again on Page2.aspx."
|
|

January 28th, 2008, 08:17 AM
|
|
Registered User
|
|
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi JessyEzzy,
Even I have the same problem. If you got to know the solution, pl let me know.
[email protected]
|
|

January 28th, 2008, 11:58 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I checked the book text and something is striking me as odd.
If you change the postback URL for the form (through the button control) the "action" attribute of the form is eventually modified. This means the form is posted to a different page. If this happens, there is no possible way you can handle a server click event on both pages. Now perhaps there is something special going on with ASP.NET that I don't understand or have experienced yet. Maybe the runtime now recognizes a cross page postback and does some extra processing.
However, this doesn't mean that you can not handle a CLIENT-side click event. Is it possible this problem is a confusion about what/where events are concerned? If you set a OnClientClick attribute for a button, this results in javascript that can certainly execute before the page is posted to the server. This can happen regardless of where the page is posting to.
-Peter
|
|

February 3rd, 2008, 07:50 AM
|
|
Registered User
|
|
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I know about the OnClientClick attribute but I don't want to add javascript, I want to add server side.
Thanks
|
|

February 3rd, 2008, 10:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Have you set the PreviousPageType directive in the destination page?
Also, are you accessing a property or other element on the previous page? I can get Button1_Click on the *previous* page to fire when I have something like this in page 2:
Label1.Text = this.PreviousPage.TheText;
where TheText is a read-only property that retrieves the text from a TextBox on the previous page. As soon as I comment it out, it doesn't work anymore. I think this is because the PreviousPage property needs to be accessed before its underlying page's life cycle is executed. If you look into the PreviousPage property of the Page class, you see something like this:
Code:
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Page PreviousPage
{
get
{
if ((this._previousPage == null) && (this._previousPagePath != null))
{
if (!Util.IsUserAllowedToPath(this.Context, this._previousPagePath))
{
throw new InvalidOperationException(SR.GetString(
"Previous_Page_Not_Authorized"));
}
ITypedWebObjectFactory vPathBuildResult = (ITypedWebObjectFactory)
BuildManager.GetVPathBuildResult(this.Context, this._previousPagePath);
if (typeof(Page).IsAssignableFrom(vPathBuildResult.InstantiatedType))
{
this._previousPage = (Page) vPathBuildResult.CreateInstance();
this._previousPage._isCrossPagePostBack = true;
this.Server.Execute(this._previousPage, TextWriter.Null, true, false);
}
}
return this._previousPage;
}
}
You can see Server.Execute is used to execute the previous page and have it go through its own page life cycle, causing the Button to be clicked. Where would we be without Reflector....
Finally, make sure that the destination page is in your application. As Peter pointed out, with CrossPagePostBacks the target of the form is changed so the initial request of the postback is handled by the destination page. So, only a destination page in your own site is able to create an instance of your previous page so the relevant code gets executed.
Be aware that because of this model, other code, like Page_Load is triggered as well.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
|

February 9th, 2008, 04:20 PM
|
|
Registered User
|
|
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No I didn't set PreviousPageType directive in the destination page beacuse the button at which I have set the PostBackUrl property is in a master page and the PreviousPageType directive can only be set to aspx pages.
Thanks
|
|

February 9th, 2008, 04:24 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Right, then based on what do you expect this to work?
Since the master page can be used by any page, there's no way to tell what page the previous page is, right? So you can never tell what data this previous page can expose.
As my explanation shows, PreviousPage is not executed unless you access it.
Or am I overlooking something?
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|
|

February 14th, 2008, 09:58 AM
|
|
Registered User
|
|
Join Date: Jan 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No, you're absolutly right.
Thanks
|
|

September 21st, 2008, 10:13 AM
|
|
Registered User
|
|
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have a similair problem but it can be solved that easily. The problem is that I have to post to another page which is NOT my same website and therefore I have no control over that.
I want to post some data to another website using POST but before I post them, I want to save them on my database. Furthermore when I post to the other website, the URL of that website has to remain the same meaning that I cannot use Server.Transfer to do the job since Server.Transfer will not chnage the browser URL to the URL were the page has been posted.
Anyone knows how I can post back using client-side script (using OnClientClick event on the link button)? Maybe I manage to call both a server side method whilst the page is being redirected to the form action.
Or else I've heard about URL rewriting but I guess one can't just rewrite a whole URL making me think I'm on another site. :<
I'm confused...
|
|

September 21st, 2008, 09:13 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I would suggest starting a new thread if this is indeed a new topic. This thread was started 8 months ago.
You can not do a Server.Transfer to a URL outside of your own ASP.NET application. Transfer actually transfers ASP.NET page execution from one page to another inside your application. I suspect that you can't even do this to a page in another application on the same server let alone to a totally different site.
What you should be able to do is have your initial page do a regular postback, process the data as you need to (i.e. save to a database), then change the target of the webform and post it to the new destination system. You should be able to use the standard web form and change the form's action. Then you simply need to trigger some client side javascript to auto submit the form. You can use the methods of the ClientScriptManager (Page.ClientScript) to get what you need:
http://msdn.microsoft.com/en-us/libr...r_methods.aspx
The way I have done things like this is to create a regular button on the page which you can set as not visible. Then you use GetPostBackEventReference to get the client side code that fires that button's click event. Then you use RegisterStartupScript to drop that script snippet onto the page such that it will fire immediately on page load. This technique works well for auto postbacks. I've done it in ASP.NET 1.1, but with the new event validation behavior you may run into problems, I haven't do this in 2.0 yet.
An alternate that might be easier would be to manually construct the exact form structure that needs to be posted to the destination. Set it's action to the destination URL. Then use the RegisterStartupScript method to drop a form submit javascript snippet to the page to auto post the form.
-Peter
compiledthoughts.com
|
|
 |