server.transfer in asp.net? - asp.net

When i write server.transfer("defaul2.aspx");
in url default2.aspx is not displaying instead of it is showing defaul.aspx page.
when i write response.redirect("default2.aspx");
in url default2.aspx page is showing what is there any different.

That's because Server.Transfer() and Response.Redirect() don't work the same way.
Server.Transfer() doesn't end the current request, it only instructs ASP.NET to stop rendering the current page and start rendering the new page instead. The client is none the wiser, from its point of view the server is still responding to the initial request, so the URL displayed in the address bar does not change.
Response.Redirect() ends the current request and sends a 302 response code to the client. The client then issues another HTTP request to the redirected URL and processes the response. Since the client knows that the URL has changed, it displays the redirected URL in its address bar.

Response.Redirect method:
it helps in navigating to another page from code. This is like clicking a hyperlink.To navigate from one page to another using button click or linkbutton control or from server side code use Response Object's Redirect method.
using Response.rediect() method you can not get information from source page on target page.
its source code is like
VB.Net Code
Private Sub Button1_Click(ByVal Sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
'Display target page.
Response.Redirect("Target.aspx")
End Sub
C# Code
private void Button1_Click(Object sender, System.EventArgs e)
{
//Display Target Page.
Response.Redirect("Target.aspx");
}
Using Server.Execute Method
use Server.Execute Method to process a Target web form without leaving the Source page. This technique let you embed the result from a target page to a region on Source page. Like Server.Transfer, It also required EnableViewStateMac attribute to page directive set to false.
Suppose I have to pages one is Calculate.aspx having two textboxes and a button control. On button Click event. I performed Server.Execute to Result.aspx another aspx page.
Source Code:
protected void btn_Click(object sender, EventArgs e)
{
Server.Execute("Result.aspx");
}
write below code in Result.aspx pageload event.
protected void Page_Load(object sender, EventArgs e)
{
NameValueCollection colform = new NameValueCollection();
colform = Request.Form;
Response.Write("<h2>Additon Result:</h2>" + (Convert.ToInt32(colform["TextBox1"]) + Convert.ToInt32(colform["TextBox2"])));
}

Related

How can I access my Mater Page's radtabstrip?

I have a master page with a radtabstrip I use as my main navigation. In a content page I'd like to be able to disable/enable a tab from that strip. I've tried using findControl but I think it's returning null. Here's my code:
RadTabStrip menuStrip = (RadTabStrip)Master.FindControl("tabMain");
menuStrip.FindTabByText("Lookup Table").Enabled = true; //null reference error occurs
Fairly simple, but I'm guessing referencing an object like a radTreeStrip isn't the same as a label or textbox..any ideas?
EDIT:
I actually got it to work by making a function on my master page and calling it from the content page. However, what I REALLY need is to access the master page from a page that is not implementing the master page. Would that be possible? Or is there a way to include the master page but not show it's contents? –
You must first select the Page.Master and convert it to a MasterPage; next you load the control using FindControl, then convert it to a RadTabStrip, like so:
VB.NET
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim menuStrip As RadTabStrip = CType(CType(Page.Master, MasterPage).FindControl("tabMain"), RadTabStrip)
End Sub
C#
Proected void Page_Load(Object sender, System.EventArgs e)
{
RadTabStrip menuStrip = (RadTabStrip)((MasterPage)Page.Master).FindControl("tabMain")
}
EDIT:
If you want the same RadWindow to be used across multiple pages, make sure you make a different aspx page and add its URL inside the NavigateUrl of the RadWindow.
Below is a link from Telerik that displays info about the RadWindow and its NavigateUrl:
http://demos.telerik.com/aspnet-ajax/window/examples/contenttemplatevsnavigateurl/defaultcs.aspx

how to do notification message in ASP.NET Web Forms

I have in Site.Master:
<% if(Session["msg"]!=null) Response.Write(Session["msg"].ToString()); %>
I have also on submit form method:
protected void Send_Click(object sender, EventArgs e)
{
Session["msg"] = "Thx for email.";
Response.Redirect("~/Default.aspx");
}
But now when I refresh page or go to another page I still see "Thx for email." but user should see it only once.
You can clear out the Session["msg"] on Page_load (outside of the if(!isPostback))
Or you can create a label on the master page, access that through the child pages to put the message in there, and clear that one on Page load, this gets you away from using Session. Using a Label you can also set the cssClass, allowing bolding, color changes (red for errors, green for success, etc).
If you just want a plan message you could aways go with a literal control, less over head.
This is because Session variable having their value throghout the session.
Session["msg"]
will always have same value on all the pages in a session.
If you want that value should only be used for the page where you redirect then you can use querystring.
protected void Send_Click(object sender, EventArgs e)
{
Session["msg"] = "Thx for email.";
Response.Redirect("~/Default.aspx?msg='true'");
}
then on SiteMaster
<% if(Request.QueryString["msg"]!=null) Response.Write(Session["msg"].ToString()); %>
You have to set Session["msg"] = null after you show the message. Session lives in server at defualt of 20 min. If you do not set it null it will appeear
Try setting the Session["msg"] to null once it is printed on the page.

Wrong page after Login

If I click on a link(Without logging into the site) I am navigated to this page:
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fContacts.aspx
When I login to the site I want to be redirected to my Home page rather than Test Contacts.aspx page.
How can I resolve this?
If i do this then I'm not logged in.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
Response.Redirect("Home");
}
Just change link url you clicking on to:
http://localhost:59196/Login.aspx?ReturnUrl=Index.aspx
I think you clicked on Test Contact.aspx page with out logging in and hence you have been redirected to the Login page with a query string as ReturnURl = test contact.aspx, which might be used to redirect from the login page.
So instead of using return url query string you can redirect directly to your page page from the log in button click in the login page.
Hope it works.
I mean in
Loginbutton_click()
{
//login validation code here
//after successful validation
Response.redirect("Yyourhomepageurlhere");
}
You want to redirect to
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fIndex.aspx
rather than
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fContacts.aspx
In other words the link that you click on should point to the first link above rather than the second.
The question that remains is how are the links generated? Are they hard coded or are you using MVC and generating the link somehow? Is the link even in your control to generate?
It would appear your code looks at the ReturnUrl query string parameter and on successful login redirects to it.
Did you come to the Login page from the Contacts page? If so then the ReturnUrl might have been generated dynamically depending on where you came from. You may require this to be fixed to being the Index page (I assume this is what you have called it). Be careful though, other developers may want to have this link be dynamic.
I must changed the event.
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Response.Redirect("Home");
}

ASP.Net Page Not Posting Back - Reloading Instead

I have a simple ASP.Net login page. When the login button is clicked, the page should post back and the even should be handled by my server-side event handler. Instead, the page simply reloads. Page.IsPostBack is false.
I've put breakpoints in the Page_Load/Init (where applicable) handlers of the Master page, the ASPX page and the UserControl (ascx). When I hit the Login button, instead of getting a post back and having my event handler called, I simply get the page load as if it was a fresh request.
But that's not the end of it! The login page takes a single query string parameter: Login.aspx?id=123456. The above failure occurs when using this parameter. However, if I enable URL Rewriting in order to make the query Login/123456, the error does not occur; I get a post back and my event handler is called in this instance.
So why am I not getting the expected behaviour from my page? What about the rewritten URL is making the problem go away?
Login Button is declared in LoginUserControl.ascx:
<asp:Button ID="SubmitLinkButton" runat="server" Text='Log In' OnClick="SubmitLinkButton_Click"></asp:Button>
And the handler in the code behind:
protected void SubmitLinkButton_Click(object sender, EventArgs e)
{
Authenticate();
}
SubmitLinkButton_Click is never called. :(
Edit (more code):
//Page_Init on the Master page
protected void Page_Init(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
try
{
if (SessionFacade.User != null)
{
loginlabel.Text = "Logged in |";
LoginLink.Visible = true;
}
}
catch
{
FormsAuthentication.SignOut();
CacheFacade.RemoveSessionValues();
Session.Abandon();
Session.RemoveAll();
HttpContext.Current.Response.Redirect("~/Login.aspx");
}
}
else
{
loginlabel.Text = "";
LoginLink.Visible = false;
}
}
Page_Load on the ASPX page:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack.Equals(false))
{
/* Some business stuff that boils down to this: */
Session["company"] = Request["company"];
}
}
Page_Load on the Login Control:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//honestly, there's no code here
}
}
P.S: I need to keep the non-url-rewrite way of accessing the login page, because many users are still navigating to that URL.
P.P.S: Even if URL rewriting isn't enabled, the error still occurs.
Whenever I've seen this problem in the past it has usually been attributed to URL re-writing. I would usually reach for Fiddler to trace the HTTP activity. My hunch tells me after you click the button and see the POST request it will quickly be followed up by a 302 redirect to the login page.
You note that "The above failure occurs when using this parameter" of the login.aspx page. Are you certain there is no url-rewriter configuration that for example, may strip out any querystring values and do a redirect?
Try to check your caching policy. Possibly your request is cached
I cleared the history on my IE browser and the problem went away.
Did you try to use a different browser? Once I had a similar problem, and it solved by re-installing Firefox!

ASP.net control Postback Problem (Can't read the value the user entered!)

I've written a custom widget to allow a user to select a location from a database of locations. This is my first ASP.net custom control. It seemed like everything was working fine, but now there's a problem.
My control implements the RaisePostBackEvent function as follows:
public void RaisePostBackEvent(string eventArgument)
{
SelectedLocationId = eventArgument.Split('|')[0];
SelectedLocationDescription = eventArgument.Split('|')[1];
}
I wrote a test page and included the following in my ASP code:
<%= locationSelector.SelectedLocationId %>
That worked fine.
However, in my web application, the following code does not work:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
Response.Write(locationSelector.SelectedLocationId);
// SelectedLocationId is null here!!!
}
When I run this code in the debugger, I see that my Page Load event fires before the Post Back event! Therefore, the data is not yet read from the postback. I know that using the MS provided text field control, the text is available at Page Load, so I think I must be doing something wrong.
How can I read the location that the user selected when the Page Load event fires? To clarify, I'm refering to the Page Load of a web application page.
You're setting SelectedLocationId on a postback event and at the same time you are trying to retrieve its value on the first load. SelectedLocationId will be null all right.
Try:
protected void Page_Load(object sender, EventArgs e)
{
if (locationSelector != null)
Response.Write(locationSelector.SelectedLocationId);
}

Resources