how to close current webform after display messagebox - c#

I use the code below to open a message box:
protected void btnUpdate_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Messagebox", "alert('Record Updated successfully.Note: This tab is now getting close');", true);
}
How to close the current page after a click on the OK button of this message box?

in web forms you can't close the page, what you will have to do is redirect the page to a different page, and if you add the redirect after the alert it should happen when the alert closes
if you don't know how to redirect a page then this may help
aspx page to redirect to a new page
however webforms is something i'm still learning myself

Open the current page as popup using window.open() then write a Javascript function in the page such as
<script>
function confirmAndClose()
{
var conf = confirm("Confirm to close");
if (conf == true) {
window.close();
}
}
</script>
then change you server side code to
ScriptManager.RegisterClientScriptBlock(this, "script","confirmAndClose()");

Try this,
string message = "alert('Your Approval Has Been Saved Success');self.close();";
ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", message, true);

Related

Once the popup closed, main page should be refreshed to load changed data

private void AddOrderItems(object sender, EventArgs e)
{
string url = "Orderdetails.aspx";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}
I am using above code to open a popup window.
I have a page order.aspx from which I am opening another aspx page (Orderdetails.aspx).
Orderdetails.aspx page opens as a popup.
once user close the window(Orderdetails.aspx) , Orders.aspx page should post back and load newly added items.
How to catch the close event of the popup window?
Put this script block in your child window:(Orderdetails.aspx)
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>

How to open page in new tab using the response. redirect at asp.net

I want to open a new tab or a new page by using Response.Redirect in a button click handler. I'm using a query string to pass some values. How can I open he page in a new tab?
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Redirect("NewQuote.aspx?val=" + this.txtQuotationNo.Text);//displaying gridview in other page to print what needed
}
Try this. This works sure for me...
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Write("<script>window.open ('NewQuote.aspx?val=" + txtQuotationNo.Text+"','_blank');</script>");
}
Simple solution is here.
Edit your html button element and add attribute OnClientClick="target ='_blank';".
<asp:Button ID="myButton" runat="server" CssClass="btn1"
OnClick="btnSave_Click" OnClientClick="target ='_blank';" />
Then in btnSave_Click
protected void btnSave_Click(object sender, EventArgs e) {
Response.Redirect(url);
}
You can change your design element as below : OnClientClick
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick ="document.forms[0].target = '_blank';" />
Javascript code :
Response.Write("<script>");
Response.Write("window.open('NewQuote.aspx' ,'_blank')");
Response.Write("</script>");
I encountered this same problem today.
Response.write didn't work for me, to solve it I used instead System.Web.UI.ScriptManager.RegisterClientScriptBlock.
This is how your btnSave click event should be:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
}
NOTE: I'm using ScriptManager.RegisterClientScriptBlock because the button is inside an UpdatePanel, if that's not your case you should try:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openModal", "window.open('NewQuote.aspx?val= " + this.txtQuotationNo.Text + "' ,'_blank');", true);
}
if you are using http
then use below code
Response.Write("<script>window.open ('URL','_blank');</script>");
this code cannot be used for https
for https do below
javascript in page
function shwwindow(myurl) {
window.open(myurl, '_blank');
}
in c# code behind
string URL = ResolveClientUrl("~") + "**internal page path**";
ScriptManager.RegisterStartupScript(this, this.GetType(), "show window",
"shwwindow('"+URL+"');", true);
this code cannot bybass the browser popup blocker. user must allow it to run.
for ie it opens in new window or new tab up to ie version
in firefox and chrome it opens new tab
enjoy it!!
A redirect is always in the same page as where you came from, you can't open a new window from a redirect call.
I would suggest to inject some javascript code in the client to open the new page on reload, or change to a control that can open to a new page, like a LinkButton with the right Target attribute.
You can use ScriptManager to do the needed:
private void Redirect_New_Tab(string url_To_Open)
{
string modified_URL = "window.open('" + url_To_Open + "', '_blank');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", modified_URL , true);
}
Worked for me when I left the Double Quotes off of target ='_blank';
Response.write didn't work for me, so I used instead System.Web.UI.ScriptManager.RegisterClientScriptBlock.
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
I spent quite some time on this, and while it does work to set the target on the form to _blank it will mean that every other control on that page will attempt to open in a new window afterwards. And you can't manipulate the form back server side, since the response will never reach the orginal tab.
So I made it work with a combination of setting the target to _blank when the button is clicked, and then shortly after reset the target back.
I put in this Javascript function on the page:
<script type="text/javascript">
function openInNewTab() {
document.forms[0].target = "_blank";
setTimeout(function () {
document.forms[0].removeAttribute('target'); // After 500 ms revert back to normal mode so all other controls are not affected.
}, 500);
}
And on the control that needs to open in a new tab just add:
OnClientClick="openInNewTab();"

Right way to detect browser and stop user from using page?

Ok, I want to allow user to be able to use the page only when browser is Chrome or IE8-9.
I doing so from code behind as you can see. And also trying to set viability of main div to none.
protected void Page_Load(object sender, EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if (browser.Type.ToString().ToLower().StartsWith("ie"))
{
if (Convert.ToDecimal(browser.Version) < 8 || Convert.ToDecimal(browser.Version) > 9)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>alert('Your browser is not supported. Please use Chrome or IE7,IE8,IE9');</script>", false);
pagecontainer.Attributes["style"] = "visible:none";
return;
}
}
else if (!browser.Type.ToString().ToLower().StartsWith("chrome"))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "pop", "<script>alert('Your browser is not supported. Please use Chrome or IE7,IE8,IE9');}</script>", false);
pagecontainer.Attributes["style"] = "visible:none";
return;
}
//blah blah
}
<div id="pagecontainer" runat="server" >Page Content Goes Here</div>
But it does not seem to be hiding the div.
I know I can always redirect user to a warning page or something but lets say I do not want to do so. What are my alternatives?
remove this line:
pagecontainer.Attributes["style"] = "visible:none";
and use this one instead:
pagecontainer.Attributes.Add("style", "display:none;");

Popup window after submit

Just to ask a straight question, when I complete adding some data in my page and click submit button, how can I make a popup window saying that the information has been successfully added in database instead of make a new page? Is there any ways that I can do? Any website to be referred? Thanks
You may create a re-usable function for it .
public void Show(string msg)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "msg", "alert('" + msg + "');", true);
}
}
and in submit button call like this.
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Your Code for submit
Show("Save Success");
}
You have to just put below code:
Response.Write("<script>alert('information has been successfully added')
</script>");
You could set a label text with your message and make it visible when you want to show the message.
lblMessage.Text = "Data updated successfully";
lblMessage.Visible = true;
To make it prominent, you could use jquery dialogs, and style it appropriately with CSS.
ScriptManager.RegisterStartupScript(this, this.GetType(), "Notification", "alert('Done');", true);
Using ASP.NET you would be still undergoing a full page life cycle if you are submitting your form with a regular submit behavior. This means that the page would first need to reload and then to fire your alert. If you want the page to not reload but instead just show a result alert, you would need to perform your action with AJAX, POSTing the form to a service method which updates the database you have. This would not reload your page and would just show an alert which your ajax call is complete. jQuery $.post()
On the click of your button after checking for valid data you can do something like this
ClientScriptManager script = Page.ClientScript
if (!script.IsStartupScriptRegistered(GetType(), "Show Popup"))
{
script.RegisterStartupScript(GetType(), "Show Popup", "ShowPopup();", true);
}
You can call javascript function from code behind using ClientScriptManager.RegisterStartupScript Method
Here is you Popup
HTML:
<div id="Popup"></div>
CSS:
#Popup
{
height:200px;
width:300px;
position:fixed;
z-index:102;
left:50%;
top:50%;
margin-top:-130px;
margin-left:-180px;
font-weight:bold;
font-size:10pt;
padding:20px;
background-color:#fff;
border:10px solid #9cc3f7;
border-radius:20px;
-webkit-border-radius:20px;
-moz-border-radius:20px;
text-align:center;
display:none;
}​
Jquery function:
function ShowPopup()
{
$('#Popup').show("slow");
}​
See Sample
//Global Declaration
public static void Message(String message, Control cntrl)
{
ScriptManager.RegisterStartupScript(cntrl, cntrl.GetType(), "alert", "alert('" + message + "');", true);
}
//Call any where, where you want to display message
Message("Any message here", this);

How to open new browser window on button click event?

How to open new browser window on button click event in C# ASP.NET?
Please share any example.
I am doing following code. Please let me know where I am going wrong.
btn_Click()
{
if(condition==true)
{
this.Page.ClientScript.RegisterStartupScript(
this.GetType(),
"page_index_script2",
"openNewWindow();",
true
);
}
}
And the JavaScript function is
function openNewWindow()
{
alert('HI');
window.open('http://www.stackoverflow.com');
}
When I run the code from javascript function Alert works but new window is not getting opened.
You can use some code like this, you can adjust a height and width as per your need
protected void button_Click(object sender, EventArgs e)
{
// open a pop up window at the center of the page.
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'your_page.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
}
It can be done all on the client-side using the OnClientClick[MSDN] event handler and window.open[MDN]:
<asp:Button
runat="server"
OnClientClick="window.open('http://www.stackoverflow.com'); return false;">
Open a new window!
</asp:Button>
Response.Write('... javascript that opens a window...')
http://www.aspspider.com/qa/Question2714.aspx
Or write to the response stream:
Response.Write("<script>");
Response.Write("window.open('page.html','_blank')");
Response.Write("</script>");

Categories