Reputation: 31
I have a web application(not windows form) where user selects items from listbox(selection box) and press on "submit" button. Upon submit, the application forms the SQL query and connects to database-fillup Datatable - convert to html code and prints the table on the page. What I want to do is show an image(some sort of indicator to the user that in the background it's doing query and building table etc.) just when they press submit button and when table is ready to be displayed on page, hide the image. So far, this is what I did.
BackgroundWorker bw = new BackgroundWorker();
protected void Page_Load(object sender, EventArgs e)
{
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
public void SQL_Query() {...database connection and fill up data table }
protected void Submit_Button(object sender, EventArgs e)
{
//LoadingImage.Visible = true; //this does not work
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
SQL_Query();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.tbProgress.Text = "Canceled!";
}
else if (!(e.Error == null))
{
this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
//when the query is completed, customize the data and print it on page as label
LoadingImage.Visible = false;
if (SQLQuery_Table.Rows.Count >= 1)
{
CustomizedTable(SQLQuery_Table);
}
string str = Display_Table();
label.Text = str
}
}
Now, the problem with code is that LoadingImage.Visible = true;
does not make image appear when button is pressed. According to my reading of other similar posts about backgroundworker, it says that UI can not work in the same thread. I dont want to make any progress bar so not interested in doing Reportprogress. Simply, make image appear on submit button press and disappear in bw_RunWorkerCompleted
. How can I make the image appear when user presses the submit button? I want to keep it simple so if I can avoid creating another backgroundworker just for Image that would be great. thanks!
Upvotes: 0
Views: 419
Reputation: 203827
BackgroundWorker
is pretty much useless in an ASP application. When you finish kicking off the asynchronous operation you send the response back to the client. You can no longer affect the response once you have sent it back to the client. You aren't able to change the UI based on the results of the asynchronous operation in the manor that you are describing.
The only way to do this is for a new request to be made from the client that can ask for updates to the operation, thus allowing the server to send new information to the client.
Upvotes: 1
Reputation: 6521
You can use a .gif loading image like so...
<asp:UpdateProgress id="updateProgress" runat="server">
<ProgressTemplate>
<div style="position: fixed; text-align: center; height: 100%; width: 100%; top:0; right: 0; left: 0; z-index: 9999999; background-color: #000000; opacity: 0.7;">
<asp:Image ID="imgUpdateProgress" runat="server" ImageUrl="~/images/ajax-loader.gif" AlternateText="Loading ..." ToolTip="Loading ..." style="padding: 10px;position:fixed;top:45%;left:50%;" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
Upvotes: 0