Javascript alert is not working in Update Panel in asp.net - c#

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);

Related

Obtain Confirm Answer upon submission

In my C# code, I have presented a confirm box to the user using the following code:
string confirmMsg = "All the cause data will be permanently removed. Are you sure you want to continue";
ClientScript.RegisterStartupScript(Page.GetType(), "TWCL Issue Mgmt System", "confirm('" + confirmMsg + "');", true);
I am trying to get the answer (YES or No) into a boolean or some other variable so that I can execute other code based on the answer given. So I would like something like this:
bool x = ClientScript.RegisterStartupScript(Page.GetType(), "TWCL Issue Mgmt System", "confirm('" + confirmMsg + "');", true);
if(x)
{
/*Exceute the rest of the code here*/
}
else
{
/*do postback*/
}
Any ideas on how to get the YES/NO answer from the confirm box?
Create a hidden field on the form. Write the answer of the question in the hidden field with some javascript. Read the value of the hidden field in the code behind.
The startup script that you are registering will run when the web page is displayed, before the user has had a chance to interact with it. A confirmation prompt at this time will likely be confusing.
It's more usual to use the OnClientClick property of a button that is doing the postback:
<asp:Button ...
OnClientClick="return confirm('Are you sure?');"
/>
so that the confirmation prompt is only displayed when the user attempts to post.

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...

Message box popup in the background

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.

Implementing javascript confirm box in code behind on drop down box's selected index change event firing

Inside my boxLang_OnSelectedIndexChanged() event I got this :-
if (txtbox1.Text != "" && txtbox2.Text != "")
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
txtbox1.Text = "";
txtbox2.Text = "";
}
Now what is happening is that. When I select a different item from the drop down list this confirm box does show up BUT it first clears the content of the text boxes and then this confirm box appears. What I want is that the textboxes content should be cleared ONLY when OK is clicked.
What am I doing wrong? Please help.Thanks.
edit
#jgauffin
** I typed return where you have. Now what is happening is that its just clearing the textboxes right away..didn't even show the confirm box this time! Whats wrong now?? Please reply..thnx**
edit 2
I tried as follows:-
Inside my BindDropDown() method I added :-
dropdownbox1.Attributes.Add("onChange","DisplayConfirmation()");
then in my aspx page I have added the following script as follows:-
<script type="css/javascript">
function DisplayConfirmation() {
if (confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.')) {
__doPostback('dropdownbox1','');
}
}
</script>
This function is not getting called when drop down's selected index is changed. What's wrong?
edit 3:-
ok I changed the line of code as follows:-
dropdownbox1.Attributes.Add("onchange", "javascript:return DisplayConfirmation()");
Still wont work. I am new to JavaScript and all so having problems. Help will be much appreciated.
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
return is needed to break the process on cancel.
That's not going to work. You're posting back to the server, then registering the javascript confirm code, and then clearing content. The javascript confirm box will only actually appear once the page has reloaded, by which time you've already cleared the textbox values. It's like you're confusing clientside and server side programming. Client side code like javascript will only execute when the server has finished doing it's thing and the page is loaded.
What you need is this answer, probably:
DropdownList autoposback after client confirmation

Possible to run a javascript function after postback using an asp.net button?

I am trying to implement a web page that will on an asp.net submit button click will go to the server to add a row to the SQL database table. After that, on that same page, I want to be able to display the columnID as an alert using javascript (this has to be javascript). Is this possible? Any help would be appreciated.
Another trick picked up from Telerik was to create a asp:Label and place it at the top of the page. From code behind you then set the text to javascript command:
aspLabel.Text = "JavascriptFunction();";
When your page reloads, it will execute the Javascript function. It seems that in your case your just interested in displaying and alert, so registering scripts should not be necessary. In your case you can do the following:
aspLabel.Text = "alert('" + yourValue.ToString() + "');";

Categories

Resources