Message box popup in the background - c#

String message = "Are you sure the event: \"" + event_text + "\" has been complete for Loan# " + loannumber + "?"; //show popup to confirm the completion of event
var result = System.Windows.Forms.MessageBox.Show(message, "Loan # "+loannumber+" - Processed Event: "+event_text, System.Windows.Forms.MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.No)
{
//do nothing close popup
}
else
{
//do something else
I have this code behind on my ASP.NET page. When I click a button on my webpage, this popup should show up. The problem is that sometimes it shows up in the foreground, but sometimes I have to actually click on the little icon on the doc.
Any ideas?

It is unusual to show a server side dialog as part of a web application. That will block the thread for the web server, and the user (unless they are sitting at the web server console, rather than in their home/office with a web browser) won't see it.
I think you want to do a client side popup - perhaps with a javascript Alert or a HTML dialog box.

Do not use a MessageBox.
If you need to fire a JavaScript alert message from the code-behind, try using the ScriptManager.
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "clientScript", "alert('hi');", true)
The code above uses the page to register the script and fire off the alert message. The final parameter true, indicates the the tags will open and close around the script you provided.

Related

Javascript alert is not working in Update Panel in asp.net

I am working on asp.net web application.
I have one UpdatePanel and there is table inside table which has data in some TextBoxes.
I have one save button too inside this UpdatePanel
So I want to save this data into Database, when I click Save Button.
This is working fine till now.
But I want to show alert message to User that information saved successfully. I'm using javascript for this purpose, But Javascript is not working.
so is this possible to achieve the desired functionality using javascript, if yes then please guide me Or if there is any other alternative method except this please let me know.
Thanks,
Vivek
If you have update panel use ScriptManager.RegisterClientScriptBlock as below
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('Member Registered Sucessfully');", true)
Remember that you can't use Response.Write during an asynchronous postback.
It would be good if you have posted your code here.
Try This Line of Code
Its Definitely work with Update panel.
var message = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Bill No. : " + BillNo + " successfully generated.");
var script = string.Format("alert({0});window.location ='ChhallanPrint.aspx';", message);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", script, true);
Try this for popup sweetalert in asp vb.net
Dim message As String = " swal('Added!','Country Added Successfully.', 'success')"
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "sweetAlert", message, True)
string message = " swal('"+gelen[2]+"!','Sepete Eklendi.', 'success')";
ScriptManager.RegisterClientScriptBlock(this,this.GetType(), "sweetAlert", message, true);

Alert message box

I have been using label text to show different status in an asp.net website.
e.g. "Profile Updates", "Invalid Character" etc, however would prefer to have a pop up message box instead.
This is what I have tried -
string alert = ws.webMethod(TextBox1.Text);
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(" + alert + ")</SCRIPT>");
However when this is fired, the screen in IE just seems to get bigger, and no message box is presented.
How can I achieve this?
I would not advise doing this, it looks like you are creating a modal dialog on page load, meaning that your page cannot continue processing until a user has clicked OK.
That said, however, your problem is probably a lack of quotes around the alerted text:
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + alert + "')</SCRIPT>");
Use ClientScriptManager.RegisterClientScriptBlock instead of Response.Write
string alert = ws.webMethod(TextBox1.Text);
string script = "<SCRIPT LANGUAGE='JavaScript'>alert(" + alert + ")</SCRIPT>"
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptBlockName", script );
I have used this in my project, works fine for me
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "MessageBox", "alert('message')", true);
you can use alert or you can use jquery plugin like this and this.
if use jquery plugin you can custom message box like change backColor or...

Javascript confirm box blocks background

I have a button in a GridView, and when it is clicked it triggers the
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
event. In there I have some logic and at the end I am registering some javascript that generates a pop up. The issue I am having is that for the confirm pop up when it comes up. It shows a blank screen on the background and when I click cancel it shows it back again, otherwise it navigates to another page... But while the box is up the background is white.
I need to make it so that it shows like the "Alert" pop up that still shows the website in the background. Please note I can only make this work with these pop ups as they are used throughout the website ("not confirm ones"). This is the first confirm box I have had to add but there are many Alerts on other pages. So would not like to change them as it will be too much work (150+ pages website).
Thank you
It looks like the page isn't getting a chance to render before the alert box shows up. Hook up the code that displays the JavaScript alert to something like the body.onload event; this will wait for the page to finish its initial load before displaying the alert.
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"key",
#"
function Redirect() {
location.href = 'shoppingCart.aspx';
}
function showAlert() {
if (confirm('***Message truncated***.') == true){
Redirect();
};
}
// If you're using JQuery, you could do something like this, otherwise you
// would need to add the function call to the HTML body tag's onload attribute.
// There are other alternatives, see:
// http://stackoverflow.com/questions/1235985/attach-a-body-onload-event-with-js
$(document).ready(function() {
showAlert();
});
", true);

confirm message box

I have to show a yes no popup messagebox for a function>
This is what i do for an alert popup>
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('File Updated');</script>");
This is what i want to do in the code behind:
if (ID != 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "<script>confirm('are you sure?');</script>");
if (yes)
{
perform function
}
else
{
return;
}
}
The confirm is not working,,, any suggestions on how to do this
thanks
Edit Portion:
Navigate to a page
Add values to a textbox
Click "Save" Button to add value to database
Ask the user if he is sure he want to do it 'are you sure'?,, the confirm pop up
Now the above confirm box will only happen if the ID is != 0 or else there is no need for a popup.
if he says yes then add to database and show alert popup that values have been enterd in the DB.
if NO then just dont add to Db and just return.
so i get the confirm box like this.. but how can i get what is selected
string scriptString = "<script language='JavaScript'> ";
scriptString += "confirm ('Are you sure you want to Close this period.')";
scriptString += "</script>";
Response.Write(scriptString);
Is there a button you are clicking on to trigger the action? If so, you should add the client events to your web control like so:
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/Images/icon_remove.gif"
OnClick="DeleteUrlImageButton_Clicked"
OnClientClick="return confirm('Are you sure you want to delete?');" />
If the user selects yes, the postback happens as usual. If they select no, the even is cancelled and no postback occurs. This, IMO, is the way to handle it because it prevents any extra server activity if they select no.
Add a linkbutton.
In the OnClientClick add
javascript:return confirm('Are you sure')
This will not launch the postback if they click no. It will launch the postback if they click yes.
Then in then code behind (OnClick) of the button do your server side processing:
(Will only be executed if they click yes)
if (ID != 0)
{
Perform function
}
See the problem here is that, without posting back, you can't get the value of the confirm box. JavaScript is run client-side and until a postback occurs (either via ajax or the regular way), it can't "talk" to your C# file.
What you'll have to do is add a confirm box in JavaScript which, if Yes is clicked, will post back to your Asp.net page and run code either through Ajax or (example) form.submit().
It appears that what you're trying to do is (in a simplified scenario):
Have the user navigate to Page.aspx
Check the value of ID (lets assume
it's a querystring parameter)
If the value of ID is non-zero, prompt
the user to confirm
If they confirm do "something"
The mistake you're making is attempting to handle 2, 3 and 4 alltogether in the code-behind. The script that you emit (by calling RegisterStartupScript) doesn't get executed until the entire page has been rendered back to the user, at which point the code for steps 3 and 4 to check the value of ID and do something will already have been "skipped over"
What you need to do is decide how to separate the work between client-site and server-side as what you're attempting to do just won't work. Without knowing how your page(s) work and where the ID value comes from I can't give a speciic example, but, something like:
Have your page check ID to see if it hits your criteria, if it does, emit the javascript, but with some additional javascript that checks the response to the prompt and causes the page to re-submit but with confirmed=yes added on the querystring
Have your page check the querystring parameter "confirmed" to see if it's yes. If it is, THEN do the work
You can't do it this way. RegisterStartupScript just registers the script to be included when the page is finally rendered. After it is rendered (to HTML) then the html is sent to the browser. So when the user finally sees that popup, your code has long since finished.
EDIT:
See the answer by Mike C.: you need to move that confirm to just before the submit.
Page.ClientScript.RegisterStartupScript will register that script on the client. As far as I can see you are executing the if statement on the server. Could you please specify what confirm is not working mean? Is the alert box displaying but no effect should yes/no is pressed? If so, move the if ... else statement on the client. Anyway, I suggest that you replace RegisterStartupScriptBlock with this code:
ClientScript.RegisterClientScriptBlock
(this.GetType(), "confirm", "alert('do')", true);

Issues with displaying messagebox in asp.net

I have the following message box in c# on my asp.net page inside the btnSubmit_Click event.
It tends to popup sometimes and not popup sometimes. Any reasons as to why it is not consistent?
ClientScript.RegisterStartupScript(
GetType(),
"alert",
"alert('An email has been sent to Customer Service');",
true);
I guess that this will depend on the text you are putting inside the alert. In the example you provided the text is hardcoded but I suppose that in your real application this text is dynamic and might contain characters that break javascript such as '. Try using FireBug to see if there are some javascript errors when it doesn't work.
Have You checked, if the alert('An email has been sent to Customer Service'); line is in the HTML Source after you clicked the Button and the message did NOT appear?
If it isn't in the HTML, check:
with the Debugger if your
codeblock is hit
are you maybe redirecting the response?
try these popups instead
type java directly in the visualstudio GUI
On a button go to the "OnClientClick" property (its not into events*) over there type:
return confirm('are you sure?')
it will put a dialog with cancel ok buttons transparent over current page if cancel is pressed no postback will ocure. However if you want only ok button type:
alert ('i told you so')
The events like onclick work server side they execute your code, while OnClientClick runs in the browser side. the come most close to a basic dialog
as this codes is so small it should work unless they have really strange browser clients

Categories

Resources