Display message while function execution to UI without postback - asp.net - c#

I am using asp.net.
I have several Insert statement on my .cs file on submit button click. I want that after each insert statement execute, it will show the message on the webpage [UI] [ex: inserted in table 1].
Like:
button_click_event { Insert statement
1 if(done successfully)
display message on UI for statement 1
Insert statement 2 if(done
successfully)
display message on UI for statement 2
Insert statement 3 if(done
successfully)
display message on UI for statement 3
..... and so on... }
just like we got the screen while installing SQL Server, we get successful message after every operation.
I don't want postback to happen for this. How can I do that?

Basically, you can't do this without multiple requests to the server. HTTP is a request/response protocol. While you could potentially just send the "page so far", it would be incomplete HTML and almost certainly render badly.
The best user experience would probably be to use AJAX for this - don't do a full page refresh, just change the appropriate section of the DOM based on the response to a status request.

I have googled and find out a link through that you can achieve results as described above format.
http://msdn.microsoft.com/en-us/magazine/cc163553.aspx
In this link when you download source code please see ContextSens.aspx page, here after click on start task button message will be display as above described format.
Hope it will help for you.

Related

C# refresh page sections

I have a page that is processing data. It goes through a series of 10 steps. I want the page to display a status after each step. ie. after step 1 data processing done print "Step 1 done" then after step 2 data processing done add text "Step 2 done" etc. How can I do this using only C# without hard postbacks? Or do I have to use AJAX/Javascript or page postbacks?
I've been playing around with updatepanels. One around the whole set of steps. Or an updatepanel around each step and then calling button clicks pro grammatically. The only result I can get is for all the text to display at one time at the end of processing.
I've been racking my brain and have search google endlessly. Hopefully someone out there has an idea for me. Thanks!
I'd suggest using ajax -
Server Side:
up a new action method on your server (assuming it's MVC), use this action method to query the state of the task.
public string QueryStatus()
{
return Session["progress"].ToString();
}
When the task progresses to the next step, update a variable to indicate this (in database, or session).
Session["progress"] = "Step Four";
Client Side:
Periodically call the action method and update an element on the page accordingly.
<script>
$.ajax('/Server/QueryStatus').done(function(response)
{
$('#progressElement').innerHTML = response;
})
</script>

How to check the value in textbox in gridview using jquery in asp.net?

I used gridview to display some products and used textbox inside a gridview to enter the quantity. I have written a jquery to display an alert box if the textbox is empty. But while displaying alert box the page reloads. But i want to display the alert box without reloading. Since it was in Master page, i dont know how to do it.
may be this will help you
use return false after your alert syntax like this
alert('Please provide value');
return false;
Having a messagebox popup to inform the user that some field does not have a (correct) value is often perceived as annoying to former user as it completely blocks the browser and forces the user to first click "Ok" before she can do anything else.
I didn't find a good reliable source except for this to back up my bold statement:
Error message This notifies the user that an error has occurred, and it usually prevents them from proceeding further in the form.
Emphasize error messages through color (typically red), familiar
iconography (such as a warning sign), prominence (typically at the top
of the form or beside where the error occurred), large font, or a
combination of these.
Success message Use this to notify users that they have reached a
meaningful milestone in the form. If the form is lengthy, a success
message encourages the user to continue filling it out. Like error
messages, success messages should be prominent. But they should not
hinder the user from continuing.
A better way might be to use a non-blocking message and stopping the submit. jQuery Validation is an excellent framework to validate your user input.

asp.net: how do i prevent users from posting the same data multiple times

i have a little asp.net web application.
it is a front end to a sql-server-2008 database.
after they fill out all the data, they will press submit and the data will be sent to the database.
after this point if the user refreshes the page, the data is posted again!!! how do i disable this from happening?
Send a Location header back to the user that redirects the browser to another page: refresh will then reload that other page rather than resubmit the old form.
This is caused by the last request being a POST request to the page, what do you need to do is a redirect so the last request becomes a GET.
After you have handled the post data you can just do a redirect to the same page with:
Response.Redirect("~/ThePage.aspx");
This will prevent you from presenting a message to the user straight from the code behind, if you want to present a success message using this method you will need to add a querystring or something similar:
Response.Redirect("~/ThePage.aspx?result=success");
And then check on the page bind if the querystring to present a success message is set, such a check could look something like this:
if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString().ToLower() == "success")
{
//Show success message
}
Another solution which probably is superior but might require some more work is to wrap the form in a updatepanel, you can read more about it here: http://ajax.net-tutorials.com/controls/updatepanel-control/
An updatepanel will make the form submit with AJAX instead of full postback.
You need to follow the Post/Redirect/Get pattern which is explained on WikiPedia and alluded to by Femi. In your code after you've done your processing do a Response.Redirect to the desired page.
See this article about the PRG pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
In short, after the user POSTs (submits) data to your server, you issue a Response.Redirect to have the users browser GET a page. This way, if the user presses the reload button, it is the GET request that is repeated.

Dynamically append number to PDF or make submit button change its URL based on that number

I'm serving up PDFs from a SQL db and presenting them in the browser. I'm trying to figure out a way to embed a number in the PDF dynamically so that the recordID for that PDFs SQL record is available to me when the user submits the XML form data. The user hits the submit button on the form and the form submits its XML data to my submission page. If there is some way of changing the submission URL on the fly then I could do a query string to pass my self the recordID. I'm not generating the PDF in code, its being created by hand and then uploaded to my site.
Edit
User is given a link someServer.com/pdfLink.aspx?formID=5 they go there and that pages pulls a PDF from the DB and displays it. This pulls up acrobat in browser full size so my aspx page isn't in control of submitting the completed form, Acrobat is. The user fills out the form and hits the submit button in the form. This submit button was set up at form design time to point to another page someSite.com/pdfSubmit.aspx The submit button posts the XML data to that page and I can process it. I need the recordID in the query string for the someSite.com/pdfSubmit.aspx page. To do this I would need to modify the PDF to either add the recordID and query string to the submit button's submit URL, or embed it in the PDF else ware. The big question is how do I modify the PDF just before I display it via someServer.com/pdfLink.aspx?formID=5 to do either of these two options.
Embedding a number in PDF is not exactly kosher, but there are some things that you can do that will honor the spec.
The current PDF spec says that "The last line of the file shall contain only the end-of-file marker
%%EOF
but there is some wiggle room - the implementation details say that it doesn't technically have to be the last line of the file, but only has to appear in the last 1K and, generally speaking, if you don't muck with things too much, most compliant readers won't even blink. If I had to do this, I would be inclined to add a newline (if there isn't one), then a % (which is a PDF comment), a marker to let me know it's mine, and finally the number. So something like:
// assume we already know it ends with %%EOF
void AppendNumberToPdf(Stream stm, int number, bool addNewline)
{
stm.Seek(0, SeekOrigin.End); // go to EOF
StreamWriter writer = new StreamWriter(stm, new ASCIIEncoding(), 1024);
writer.WriteLine(string.Format("{0}% {1} {2}", (addNewLine ? "\n" : ""), kMyMarkerString, number));
writer.Flush();
}
kMyMarkerString should be something like "MyApplicationDocumentIdentifier:" or some such thing that will make it easy to identify your tracks.
The querystring is read-only so you cannot dynamically change it at runtime. However can you:
Add the recordID to the form at the time the submit page is initially rendered
Can you process the submit form and then do a Response.Redirect or Server.Transfer to the correct page with the recordid parameter added to the querystring
While trying #plinth's suggestion I realized I had to change from XML submission (since his data was on the PDF directly. So I changed the form to submit as XDP which has XML data + embedded PDF. When I did this and viewed the raw XDP that the form submitted I ran across this.
<?xml version="1.0" encoding="UTF-8" ?>
<?xfa generator="XFA2_4" APIVersion="3.0.8262.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2010-05-04T15:15:00Z" uuid="6d0944c8-1573-442c-9c85-11e372bd38c3">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<TextField1>TestMe</TextField1>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="ViewPDF.aspx?formID=10" xmlns="http://ns.adobe.com/xdp/pdf/" />
</xdp:xdp>
Notice the 2nd to last line. It automatically includes the PDF's url which had the formID value that I needed. So all I had to do was get the XDP instead of pure XML post from the form and it gives me everything I needed.

ASP.NET Postback and URLs

I have a page that requires the user to go through several steps, however step is performed on the same ASPX page with different panels being displayed.
However this is a requirement that each step has a different URL, this could be a simple as a query string parameter, for example:
Step 1:
/member/signup.aspx?step=1
Step 2:
/member/signup.aspx?step=2
Step 3:
/member/signup.aspx?step=3
However I don't want to have to redirect the user to the new URL each time they continue to the next step, this would involve a lot of redirecting and also a switch statement on the page load to work out which step the user is on.
It would be better if I could alter the URL that is displayed to the user when the original request is sent back to the user, i.e. the user click "next" on step 1 the page then does some processing and then alters response so that the user then sees the step 2 URL but without any redirection.
Any ideas would be greatly appreciated.
Could you convert your Panels into steps in a Wizard control?
It would be a little more complicated than you probably want, but you could achieve this effect with the PostBackUrl property of the submitting button. I'm assuming each panel has its own "submit" button, and they could all use this property to "advance" the process. The drawback is that in order to get to submitted controls, you'd need to use the Page.PreviousPage property in order to access any controls and their values.
You could programmatically alter the PostbackUrl property of your 'Next' button on each Page_Load, based on the query string value. This is a bit strange though, as you wouldn't be able to use a standard event handler for the button click, and you'd have to use the PreviousPage property of the Page to get the data from the previous tab.
I'd say challenge this requirement. Why would anyone need to jump into the middle step? If it's a case of displaying the progress to the user, do this on the page, not in the URL.
You require that each step has different URL, than Response.Redirect is the only option. As you want to avoid the redirection, you can use IFrame but IFrame URL is not visible to user on his browser. I think redirect option is ugly(for both SERVER and CLIENT) as in this case, you first post on the page and than get that page. The best solution is POST BACK with some varible tracking step.
You could implement a form or url rewriting so that your urls end up being
/member/signup/step1/
/member/signup/step2/
/member/signup/step3/
To do this use the
HttpContext.RewritePath method which means you can rewrite /member/signup/step1/ to /member/signup.aspx?step=1 for example. See an example here.
I would also use the PRG (post request get) pattern so that each next link posts the form data of that step to the session then redirects the user top the correct next url. this will ensure that the user can navigate back and forth through the steps safely and also the url will remain intact in all your posts.
Check out server.transfer

Categories