I have a save button that saves all the information the user enters from a form into the database. Once all that is done, I want a new tab to automatically open.
So the code checks if the save is successful then I want the new tab to open.
if (NewQuoteID != -1)
{
Response.Redirect("Printouts/DSVQuotation.aspx?QuoteID=" + NewQuoteID.ToString(), false);
}
Code for the save button:
<asp:LinkButton runat="server" ID="btnSave" CssClass="btnSaveLarge loading" OnClientClick="return CheckNoOfDetails()" ToolTip="Save Quote" OnClick="btnSave_Click" Text="Save" />
This code opens the link on the same tab. How do I get the link to open on a new tab in the browser? I see a lot of examples are setting the OnClientClick to something like this: OnClientClick="aspnetForm.target ='_blank';"
But I am already calling a method in the OnClientClick
Try this:
OnClientClick="window.open('New.aspx')"
But question is duplicated, here is the full answer
how to open a page in new tab on button click in asp.net?
Try something like this
string script = "<script>window.open('Printouts/DSVQuotation.aspx?QuoteID=" + NewQuoteID.ToString() + "','_blank');</script>"
response.write(script);
if you want open tab after server side save operation is success..
client side :
<script type="text/javascript">
var openNewTab = function (url) {
window.open(url);
}
</script>
server side
if (NewQuoteID != -1)
{
var url = "Printouts/DSVQuotation.aspx?QuoteID=" + NewQuoteID.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "openmypage", string.Format("openNewTab('{0}');", url), true);
}
Related
I have this aspx script for the button for pop-up:
<asp:Button ID="btnNewEntry" Text="Post Code Search" OnClick="btnNewEntry_Click" runat="Server" target="_blank"/>
and behind:
protected void btnNewEntry_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('../search/postcode_search/Default.aspx?code="+ p +"','Post Code Search','width=800,height=300,left=100,top=100,resizable=yes'); popup_handle.focus();", true);
}
But as the button is clicked the pop-up is opened but the parent page is refreshed. Why it's so? any work around?
Page got refreshed when a request to server is made, and server controls like Button has a property AutoPostback = true by default which means whenever they are clicked a trip to server will be made. Set AutoPostback = false for insert button, and this will do the trick for you.
or
Add OnClientClick="return false;" ,
<asp:button ID="btninsert" runat="server" text="Button" OnClientClick="return false;" />
Perhaps a little explanation of what is actually happening with your code will help. We have already discussed the auto postback, so you know that clicking that button will send the event back to the server. The page initializes again and reloads the view state and all of the posted data. After that, the button click event is handled.
At this point, your code writes the window.open script to the page. Keep in mind this is no where specific. This is just script that is added some where on the page and executed. The view state then gets updated and the page is sent back to the client. If the user reloads the page, that script is going to execute again.
Your best bet is going to be converting that to a client side button only. Find a way to get the necessary data back from the server before loading your popup. The easiest way to do that is make an AJAX call and open your pop up on success from your end point.
Try this.Updated one.
<asp:Button ID="btnNewEntry" Text="Post Code Search" OnClick="btnNewEntry_Click" runat="Server" target="_blank"
OnClientClick="javascript:window.open('../search/postcode_search/Default.aspx?code=+ p','Post Code Search','width=800,height=300,left=100,top=100,resizable=yes').focus();return false;"/>
Issue was due to "+ p +".
So as advised, i decided to go to client side. Here is my script:
<button onclick="OpenPopup()" type="button">Post Code Search</button>
and javascript code above it:
<script>
function OpenPopup() {
var getQueryString = function ( field, url ) {
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
};
var str = getQueryString('code');
if (str != null) {
p = str.replace(/%20/g, ' ');
}
else {
p = "";
}
var url = "../search/postcode_search/Default.aspx?code=" + p;
window.open(url, "Post Code Search", "toolbar=no, location=no,status=yes,menubar=no,scrollbars=yes,resizable=no, width=750,height=400,left=430,top=100");
return false;
}
</script>
so basically what it does, it grabs parameter from parent window url and adds to popup window url. My parent window url:
/Customer.aspx?code=V6E%20111&firstname=MyName
I hope it helps for others as well. Thanks for help guys.
The web form has grid view which contains Delete link. On clicking the link, confirmation message should be displayed. I am using function which displays the confirmation dialog box. On clicking OK button, the respective record should be deleted. How can I pass some value on clicking OK button in the dialog box to perform delete operation?
void ConfirmMsg(string cMsg)
{
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
String cstext = "confirm('" + cMsg + "');";
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}
just use this line in place of ur delete button.nothing needed:
<asp:LinkButton ID="lbDelete" runat="server" OnClientClick="return confirm('Are you sure want to delete the Company Information?')" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
use a link button as Delete link and
OnClick = your delete function
OnClientClick = return Confirm("Confirmation message")
if user clicks OK, the function in OnClick will execute. Otherwise it will not
Clicking on ASP.NET button redirects to correct website but on the same tab, not in a new tab what i need to do. What's wrong with the code OnClientClick="aspnetForm.target ='_blank';" below? Why it is not enough alone and what else need to be done?
The following ASP.NET code for the button control is:
<asp:Button ID="btnGenerateReport" runat="server" Text="Generate Report"
OnClick="btnGenerate_Click" OnClientClick="aspnetForm.target ='_blank';" />
I know two methods for redirecting the page to new tab in asp
1) The first method which you are already using and it works also. Make an onclientclick event of Button and on code behind of Button
Click write the following code:-
button.OnClientClick = "aspnetForm.target='_blank'"; Response.Redirect("yourpage.aspx");
2)You can also use javascript
button.Attributes.Add("onclick", "window.open('yourpage.aspx');return false;");
Both the method will redirect your page to new tab on clicking the button.
The error with your code is OnClientClick = "aspnetForm.target='_blank;'" remove the semicolon after '_blank' and it will work
If you are looking out for server side code to open a new window on Button Click, then here's how to do so.
Add the following script to the section of your page
<script language="javascript" type="text/javascript">
function openNewWin(url) {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
}
</script>
Then add a Button Control in the following manner
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"
onclick="btnOpenPop_Click" />
Finally add some code in the code behind file
protected void btnOpenPop_Click(object sender, EventArgs e)
{
string url = "http://www.dotnetcurry.com";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}
You can't use target attribute on button.
You have to use javascript function window.open()
Check this:
Window open() Method
I don't post links lightly, but I found this site explains all and it has solved my problem too.
http://dotnetspidor.blogspot.co.uk/2009/01/open-new-window-in-aspnet-web-page_28.html
I’m trying to use a button click to open a page in a new tab/window. I’ve looked at solutions similar to this, but the answers given there are either to use a link or have it open in the same window. I need to use a button because it needs to generate the link based on criteria data entered in the form (string manipulation). This button is not submitting the form though; it’s just validating some of the data on an external site. I need this in a different window or tab so they can go back and forth between my form and the validation site. This is basically my current Button_Click event:
var Address = AddressTextbox.Text.Trim();
Address = Address.Replace(' ', '+');
var Url = "http://www.example.com/query?text=" + Address + "¶m1=foo¶m2=bar";
Response.Redirect(Url);
This works except that Response.Redirect(Url) only opens in the same window, not a new one.
Just spit out javascript code from the Click event handler on the Button.
var Address = AddressTextbox.Text.Trim();
Address = Address.Replace(' ', '+');
var Url = "http://www.example.com/query?text=" + Address + "¶m1=foo¶m2=bar";
Page.ClientScript.RegisterStartupScript(this.GetType(), "dsadas", "window.open('"+Url+"');", true);
Use the OnClientClick attribute:
<script type="text/javascript">
redirectToPage = function(url){
void(window.open(url, "child_window"));
return false;
}
</script>
<asp:Button ID="Button1" runat="server"
Text="Click Me!"
OnClientClick="return redirectToPage('/somefolder/somepage.aspx');" />
Using the OnClientClick attribute, it's important to remember to return false at the end, otherwise the button click will trigger a postback.
EDIT
If you want to open a new window from the click event handler, you can do something like this:
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "open_window",
string.Format("void(window.open('{0}', 'child_window'));", "/somefolder/somepage.aspx"), true);
}
Admittedly, this is kind of ugly, but if there's certain information that you won't have until the button click logic is executed, this might be your best option. If there's any way you can build the URL before the button is clicked, you can just assign the OnClientClick in code-behind.
i want to do validation asking that, are sure you want to delete..?
<asp:LinkButton ID="lnkDelete" runat="server"
CommandName='<%# DataBinder.Eval(Container.DataItem, "ImageId") %>' OnCommand="Calling_Delete">Delete</asp:LinkButton>
The easiest way to do it is to use Confirm Button extender. Just drag this control next to the linkbutton and set the Confirmbutton externders TargetControlID to the Id of the Linkbutton. Everything else will be taken care of by the control.
More info- http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ConfirmButton/ConfirmButton.aspx
Use OnClientClick property to attach the java-script that will do the prompting. For example,
<asp:LinkButton ID="lnkDelete" runat="server"
CommandName='<%# DataBinder.Eval(Container.DataItem, "ImageId") %>'
OnClientClick="return confirm('Are sure you want to delete..?');"
OnCommand="Calling_Delete">Delete</asp:LinkButton>
This answer has one way to do it, using jQuery and a jQuery UI dialog
One advantage of using a javascript dialog such as that provided by jQuery UI is that the popup dialog is modal only for the web page on which it is displayed. You can still access other tabs within your browser while the popup is displayed.
Other solutions that use the javascript confirm function will force the user to dismiss the confirmation dialog before switching to another browser tab.
if u'd like to use custom confirmation ( like jquery dialog,bootboxjs etc.. ) then you have to generate button's "postback string" or get it somehow. asp.net gives such as postback name after rendering the page; __doPostBack('ctl00$ContentPlaceHolder1$btnDeleteSelected',''). after realizing this i wrote a js function which is generates button's postback str;
function PostBackBtnMake(id) { // id : ContentPlaceHolder1_btnDeleteSelected
var result;
var temp = id.split('_');
result = 'ctl00$' + temp[0] + '$' + temp[1];
return result;
}
then i be able to use in custom confirmation box (in this case i used bootboxjs);
function PostBackBtn(e) {
var _result = false;
bootbox.confirm("Are you sure?", function (result) {
if (result) {
__doPostBack(PostBackBtnMake(e.id), '')
}
});
return _result;
}
it's worked for me, i hope it helps you too.