asp.net open new tab and postback event - asp.net

My asp.net hat is a bit dusty, so hopefully this will be a simple question.
C# asp.net 4.5
I need a control on my form that will:
open a page in a new tab (not window because its not showing up on mobile browsers)
do a postback to a server-side method. I need the server-side callback because I have to modify some databound data.
I can't seem to get both conditions at the same time.
If I use an asp:Button, I can get the server-side event, but can't get the new page to open in a tab, rather than a window.
If I use an asp:HyperLink, it will open in a new tab, but I can't get a server-side event.
If I use an asp:LinkButton, it will open in the current tab. If I set the target="_blank", then I get an error "'webform_postbackoptions' is undefined"
Questions:
Is there a control available that will do both situations correctly?
If not, for controls with no URL, what code can I use from the server-side to open a new Tab rather than a Window? (I know its usually up to the browser to decide what to open, but somehow the hyperlink control opens properly, so there must be a way!)
OR for controls with no OnClick events, how can I call my server-side function? (from javascript in a client-side event maybe?)
note, any solution that changes the form's target to "_Blank" ruins the functionality of the rest of the page.
Code:
protected void btn_Docsign_Click(object sender, EventArgs e)
{
//Do stuff here
//open window - this always opens a window, not a tab
Page page = (Page)HttpContext.Current.Handler;
String DocusignLink = "myurl.com"
if (page != null)
{
string script = String.Format(#"window.open(""{0}"", ""{1}"", ""{2}"");", DocusignLink, "_Blank", "toolbar=1,menubar=1,resizable=1,scrollbars=1,top=0,left=0");
ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
}
//Response.Redirect(DocusignLink); //opens in current tab
}

Related

Post back and browser back button issue in asp.net

I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column.
Issues
My code is like
if (!Page.IsPostBack)
{
//CODE
}
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE.
How can I execute the CODE , on browser back button click ?
It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
Avoid using button to change page. Generate an hyperlink with the target url.
Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.
Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}

updating Textbox.text in serverside code asp.net

I have a textbox in my default.aspx page that I want to update it's text property in an event handler but the change does not take place after executing the code .
void _gsc_Task_Completed(object sender, TaskEventArgs e)
{
TextBox1.Text = "New Value";
}
How ever , if I do this on the click event of a simple button the textbox.Text value
changes.
what is the difference between a custom event handler like _gsc_Task_Completed and a simple button click event ?
Text box is a server side control. when you change the text this work(change text) is client side so the event not firing. but when i click on a button the page is going to server and back so the event is firing.
You need to consider the page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
Either your event handler is being called to early (and clobbered by whatever is brought back from the client) or it is being called after the control was already rendered.
If I were you I'd put a breakpoint there as well as normal handlers for page onInit, onLoad, etc. See what order that handler is called (if at all) and what is below it on the callstack.

How does one call client side and server side code for each selected node in treeview?

For each node in treeview, its NavigateUrl is set to call Client Side function which loads new page (page 2).
While doing this, SelectedNodeChanged event for treeview doesn't fire (page 1).
[Server Side]
node.NavigateUrl = "javascript:RefreshWorkspaceHome();";
RefreshWorkspaceHome(): either load new page or call doPostBack to execute some server side code for page 2.
Problem now that SelectedNodeChanged event doesn't fire for page 1.
Any idea?
If a node has a navigate URL, then when a user clicks on it, the web browser will navigate to the specified URL, thus bypassing the post back that would have happened instead. It does this because the node is simply rendered as a hyper link in the HTML (an "a tag"). What you could do instead is remove the NavigateUrl property from the nodes and do the redirect to the new page on the server. Here's an example of what your page 1's code-behind could look like:
// This is the event handler for the TreeView's SelectedNodeChanged event
protected void onSelectedNodeChanged(object sender, EventArgs e)
{
// Do server-side processing first
// ...
// Now do the redirect to page 2
Response.Redirect("page2.aspx");
}

onRowCommand gets fired again on refreshing the page

In my web page i have a gridview which contains link to download file and link to open another web site in separate page.When i click on link gridview onRowCommand gets executed and website opens in separate window.After this when i refresh the web page, gridview onRowCommand get executed again and opens web site in separate window again which i dont want.I want that when i click on link to navigate to another web site then only it should go.I mean on page refresh, gridview onRowCommand should not get executed.
I am using Page.RegisterStartupScript to open another web site in separate window.
ASP.net 2.0
thanks
-------------------------------------------code----------------
protected void grdDisplayView_onRowCommand(object sender, GridViewCommandEventArgs e)
{
string path = e.CommandArgument.ToString();
if (path.Contains("http"))
{
StringBuilder sbString = new StringBuilder("<script language='javascript'>");
sbString.Append("window.open('");
sbString.Append("http://www.yahoo.com','', 'status=no,width=600,height=500,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,titlebar=yes,top=198,left=305');");
sbString.Append("</script>");
if ((!Page.ClientScript.IsStartupScriptRegistered("clientScriptExportView")))
{
Page.RegisterStartupScript("clientScriptExportView",sbString.ToString());
}
}
else
{
//download file which is locally
path = Server.MapPath(path);
System.IO.FileInfo file = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "Application/octet-stream";
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
}
}
If the user clicks the refresh button, any information sent in the previous request will be sent again. That is to say, if the user clicks a button, and if he presses the refresh button, the button click action will be re-sent to the server.
I believe there is no easy way to distinguish between the original button click, and the button click sent via the page refresh.
This is a classic problem, and can be solved (in a not so easy way) using the Post/Redirect/Get pattern. See here (wiki) / here.
I am not sure if this will solve your problem, but it will hopefully set you out on the right track ! Hope it helps !
See this SO post that describes how to implement the pattern in ASP.NET Web Forms.
maybe this is not what you want but i should use:
<a href="new page" target="_blank" >text</a>
in the gridview itemtemplate
this makes sure that there is no code behind executed :)
but i don't know if that's what you want
you can create the link in the ondataitembound event
if you need the code behind you can also use the dataitembound event to add the javascript to for instance an imagebutton to launch a new page and have the code behind of the onclick event:
imagebutton.Attributes.Add("onclick", "window.open('otherpage.aspx?courseid=" + id+ "')")

Can you perform an action based on a seperate window in asp.net?

I have a page of search results, and clicking one opens a new window where they edit the information about it. Once they finish, they currently have to hit "search" again to refresh the results to ensure it saved correctly (it refreshes the search results and reflects the new status).
Is there any way to trigger the page to automatically refresh (ie. automatically click the search button) based on the popup window closing or through some other means?
I echo the sentiments to do this without popup windows, but if you really need to...
I would do this with an update panel wrapped around the results and some javascript added to your save button on the popup page.
On your popup page, I guess you have a button to save your work, something like:
<asp:button id="btn_save" runat="server" OnClick="Save" Text="save" />
on the codebehind:
protected void Save(object sender, EventArgs e)
{
// save code ...
// bla bla
// Add this
ClientScript.RegisterStartupScript(this.GetType(), "save", "<script language='javascript'>updateResults();</script>
}
On the results page add this
<script language="javascript">
function updateResults()
{
__doPostBack('updatePanel_results','');
}
</script>
and wrap your results control in an update panel (in this example called updatePanel_results). In your page_load, you'll probably need to code to call your refresh resutls method.
I'd really avoid having the application flow spanning two windows if possible. Why not impliment some kind of modal dialogue when the user clicks an edit link?
Check out jQuery's modal form implimentation to see what I mean:
http://jqueryui.com/demos/dialog/#modal-form
Opening window does not look like the ideal solution here.
In the new window you have access to parent window in javascript. You can use it to fire refresh/or some other function in mail window.
Is there any way to trigger the page
to automatically refresh (ie.
automatically click the search button)
based on the popup window closing or
through some other means?
You can register a JavaScript event handler for the OnUnload event. However, in my experience it's not reliable (browser-to-browser inconsistencies, etc).
Have you considered using Ajax to keep the search results on the same page as the original window? Or maybe a small Silverlight control?

Resources