How to refresh browse from thread ? C# - c#

I have an application that populates a grid with information from a SELECT. After all the filling, I gather a playlist (songs) and reproduce in the same sequence. After this whole process, I need to run it again. I've used the TimerPicker LOAD_PAGE to give a 1 in 1 minute, but can not run from time to time. I need to force a page_load at the end of execution of the process.
protected void Page_Load(object sender, EventArgs e)
{
LoadGrid();
Thread thrPlayMusic = new Thread(ThreadPlayMusic);
thrPlayMusic.Start();
}
public void ThreadPlayMusic()
{
music.PlaySound();
}
My application is ASP.NET.
Could someone help me?

You can use Response.Redirect(Request.RawUrl) or Server.Transfer() to "refresh" the page.
So for your case, use join:
protected void Page_Load(object sender, EventArgs e)
{
thrPlayMusic.Start();
thrPlayMusic.Join();
Response.Redirect(Request.RawUrl)
}

Related

is there a way to somehow block certain pages (aspx) based on a condition MVC?

I have a customized authentication on the system, what I would like is for certain pages (ex. EditingProfile.aspx) to not be accessible through URL navigation.
if a user is authorized: buttons (ex. btnEditingProfile) is enabled and would redirect to EditingProfile.aspx.
is a user isn't authorized: buttons (btnEditingProfile) would be disable. I'm looking for a way to prevent users from accessing EditingProfile.aspx through URL navigation.
The system I'm working on is old so I need a safe way to secure it that wouldn't interfere with other aspects of the system.
MainMenu.aspx
protected void Page_Load(object sender, EventArgs e)
{
//CODE
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
//MORE CODE
}
protected void btnEditProfile_Click(object sender, EventArgs e)
{
queryStrings=SOMEVALUE();
string QueryString = QueryStringEncrypter.GetEncryptedQueryString(queryStrings);
Response.Redirect("ProfileDetails.aspx?" + QueryString, false);
}
In my opinion, each user should has an login session to know their permission.
https://www.codeproject.com/Articles/21474/Easy-way-to-create-secure-ASP-NET-login-using-Sess
Following Tim Nguyen's suggestion I did the following:
in MainMenue.aspx I added
protected void Page_Load(object sender, EventArgs e)
{
//code
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
Session["url"] = Request.UrlReferrer.AbsoluteUri.ToString();
//more code
}
in EditingProfile.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["url"] != null)
{
Response.Redirect(Session["url"].ToString());
}
//code
}
nothing else was changed. The code is running the way I want it to so I thought id update this question in case someone need it.

How to Delay Calling a function After full page loaded on aspx with c#

Below is my simple source code, I'm just trying to call that function after page load, but what is happening right now is: it calls that function before page load. Page loads for 5 seconds and, after that, it displays label execution.
protected void Page_Load(object sender, EventArgs e)
{
display();
}
void display()
{
Thread.Sleep(5000);
Label3.Text = "done";
}
You should read this document about ASP.NET Page Life Cycle. It actually says what you are trying to do is not possible.
Every code you write in the server side is going to run before the browser renders the page. That means you can not call a function after the browser renders the page, unless you use other approach.
The simplest way you can achieve this is using Javascript and an Ajax call or by using a Timer, which opens a new thread different from the main execution thread. Although, I do not recommend open new threads, because you will lose the control over the execution flow and in a webpage you could end having hundreds of open threads.
Below Code is Working
protected void Page_Load(object sender, EventArgs e)
{
Timer1.Interval = 2000;
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label3.Text = "done";
}

Bring custom control to front

I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?

Refreshing DataGridView after Backgroundworker?

I have a DataGridView in a form that uses a Backgroundworker to load the data and a pleasewait form to keep the UI responsive and show a gif so the user can see the item is still working.
After it is complete I need to somehow refresh the DataGridView with the new data. If i manually click on the headers to sort my ASC or DESC then it shows the up to date data. What or how is the best way to get this grid to refresh please?
public void btnSearch_Click(object sender, EventArgs e)
{
pleaseWait.Show();
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.TestTableTableAdapter.Fill(this.TestTableData.TestTableTable, txtHotName.Text, ((System.DateTime)(System.Convert.ChangeType(txtDepartFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtDepartTo.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookFrom.Text, typeof(System.DateTime)))), ((System.DateTime)(System.Convert.ChangeType(txtBookTo.Text, typeof(System.DateTime)))));
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
}
The above does what it needs to which is great. I just need to some how refresh without having to make the end user re-order a column to actually show refreshed data.
Following on from jdweng's comment of ;
The re-paint method doesn't automatically get called when adding data
to a DGV. So the trick is to set the datagridview1.DataSource = null,
and then back to the actual data source.
This fixed the option for me. My gif still spins and the UI updates as my criteria has changed.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pleaseWait.Hide();
this.TestTableTableDataGridView.DataSource = null;
this.TestTableTableDataGridView.DataSource = TestTableTableBindingSource;
}

ASP.NET Stopwatch Remaining at 0

So, as part of an assignment I am trying to roughly calculate the time taken for a transaction to be reviewed and completed in an ASP.NET Web Forms project using CSharp.
In my ASPX page, I am showing the details of the objects in the cart.
And there is a button which confirms the checkout and completes it.
What I need to calculate is the time since the page loaded, till the checkout was confirmed.
Here there are two events: page_load and checkoutbutton_click
So in the code behind file I am
public partial class CheckoutReview : System.Web.UI.Page
{
public Stopwatch sw = new Stopwatch();
protected void Page_Load(object sender, EventArgs e)
{
sw.Start();
//... code
}
protected void checkoutbutton_click(object sender, EventArgs e)
{
//...code
sw.Stop();
// in the database i am then storing the elapsedMilliSeconds of the stopwatch
}
The problem however is that with this code the elapsed time is remained 0 all the time.
If I put the same stopwatch however in the checkoutbutton_click() the stopwatch works fine.
Can someone kindly explain what I am doing wrong here?
Each time the page is posted back to the server, a new instance of the class CheckoutReview is being created... so what you're seeing is the time between the creation of the class on the post-back and the event handler.
You have to remember that each request to the server (whether it's the original page request, or a post-back) is an individual call to the server. Things like Session and ViewState exist to allow you use data between those requests.
I would recommend you store the current time in the ViewState of the page on the initial load, and then check the TimeSpan difference in the event handler...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Only store on first visit to the page
ViewState["pageLoadTime"] = DateTime.Now;
}
}
protected void checkoutbutton_click(object sender, EventArgs e)
{
TimeSpan diff = DateTime.Now - (DateTime)ViewState["pageLoadTime"];
}

Categories

Resources