Calling pageload after some operation - c#

I have an aspx.cs file with the following code
protected void Page_Load(object sender, EventArgs e)
{
//Some code
}
protected void Removeabc(object sender, EventArgs e)
{
//Some code
}
In the last line of Removeabc i want to reload the page and call Page_Load again. Please help me on how to do the same.

To reload the page, use
Response.Redirect(Request.Url.ToString())
It will call the Page_Load on that reload.

You can use
Response.Redirect(Request.RawUrl);
It will redirect you to the same page and call Page_Load().

You should wrap that logic into a third method which can be called from both handlers:
protected void Page_Load(object sender, EventArgs e)
{
//Some code
DoSomeCleverStuff();
}
protected void Removeabc(object sender, EventArgs e)
{
//Some code
DoSomeCleverStuff();
}
private void DoSomeCleverStuff() {
// Clever stuff
}
It is good practice not to put heavy logic/code into event handlers in C#. Extract the core logic out into either another method or class so that the code can be re-used elsewhere throughout the class/application.

You can redirect to this page using Response.Redirect(Request.Url.ToString()) or if you only want the code in Page_Load to execute you can call Page_Load(null,null)

Related

ViewState value not being retained during postback

How can I make this piece of code work? I am dealing with a bigger issue but if I can make this work then I will know what to do.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Value"] = "Button clicked";
}
Page_Load event happens before Button1_Click and hence you wont be able to access a value that is not already set.
You will need to use an event that happens after Button1_Click like Page_PreRender as you have used in the answer.
Please go through this link to understand Page Life Cycle, which is invaluable in Asp.Net Webforms development.
I was able to solve my problem by putting my logic in pageLoad method in the page_PreRender method like this:
protected void Page_PreRender(object sender,EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}

C# how to stop a control's event

I have an .aspx page that has a page_load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.Name != "")
{
....
}
else
{
FormsAuthentication.RedirectToLoginPage();
return;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
}
The issue I am noticing is that when the user clicks the submit button it goes though the Page_Load (goes though the else) and then tries to run though the protected void btnSubmit_Click(object sender, EventArgs e).
Is there a way to make it redirect to the login page and not continue to the next void?
This is NOT an issue. This is the normal ASP.NET Page Life Cycle.
Controls events fire right after the Page_Load event.
Note that the FormsAuthentication.RedirectToLoginPage method does not end the request by calling HttpResponse.End. This means that code that follows the RedirectToLoginPage method call will run.
Assuming you want to stop further processing from within the Page_Load (which I believe is your intention with return):
protected void Page_Load(Object sender, EventArgs e)
{
if (/*I want to kill processing*/)
{
// Method one:
Response.End(); // Though admittedly ugly
// Method two:
this.Context.ApplicationInstance.CompleteRequest();
return; // return as normal (short-circuit)
}
}

onclick second click

New to asp.net, I am having a problem on a website I am creating, I am using a master page to build my pages. I am trying to change the css class of a li tag using the onclick event in linkbuttons:
<asp:LinkButton runat="server" id="AboutButton" OnClick="about_click" PostBackUrl="about.aspx"><span>About</span></asp:LinkButton>
This linkbutton calls a function in the master page's code behind:
protected void about_click(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
about.Attributes.Add`enter code here`("class", "current");
}
}
This only works when the page is loaded and the button is clicked again. Any help would be greatly appreciated.
By adding: if(Page.IsPostBack) you're specifically telling it not to execute that code the first time the page is loaded, but you want it to happen when the page is first loaded, by the sounds of the question.
Why did you add if(Page.IsPostBack). Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
about.Attributes.Add("class", "current"); //initial setting here
}
}
protected void about_click(object sender, EventArgs e)
{
about.Attributes.Add("class", "current");
}

Redirects with Global.asax file in C#

I have added the following code to my Global.asax file:
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
etc.....
But my BeginRequest function just gets ignored. How do I redirect my entire application from http: to https:?
If you're using a master page or a base class, I would put your logic there. Global events shouldn't be relied upon for logic like this.
Put the logic in Page_Load (or earlier in the lifecycle) of the master page or base class like this:
protected void Page_Load(object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
You could do the above at another point in the lifecycle if you wanted too, like PreLoad or PreRender.
Using global events
If you're going to use a global event, I would actually use Application_EndRequest, because it gets called on every request so the application can clean up resources.

What is the best way to distinguish beteen "Refresh Post" or a "Real Post Back"

What is the best way to distinguish beteen "Refresh Post" or a "Real Post Back".
This is what I need to attain
protected void Button1_Click(object sender, EventArgs e)
{
if(PostBack && !Refresh)
{
//Do Something
}
}
I usually do a Response.Redirect to the same page in the postback event.
That way all my Page.IsPostBack are real Postbacks and not Refreshes
You could set a hidden input with a nonce value generated randomly every time the form is loaded (but not on postback), then check if the nonce value got sent twice. If it got sent a second time, it was a refresh.
you could try like
protected void Button1_Click(object sender, EventArgs e)
{
//your code of Click event
//..............
//...............
// and then add this statement at the end
Response.Redirect(Request.RawUrl); // Can you test and let me know your findings
}
Sample working code for the accepted answer
Add this line in designer
<input type="hidden" runat="server" id="Tics1" value="GGG" />
Add following lined in the code behind
public partial class WebForm1 : System.Web.UI.Page
{
long tics = DateTime.Now.Ticks;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Tics1.Value = tics.ToString();
Session["Tics"] = tics;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Tics"] != null && Request["Tics1"] != null)
{
if (Session["Tics"].ToString().Equals((Request["Tics1"].ToString())))
{
Response.Write("Postback");
}
else
{
Response.Write("Refresh");
}
}
this.Tics1.Value = tics.ToString();
Session["Tics"] = tics;
}
}

Categories