ASP.net Passing Links from Page to Page - c#

I am trying to display a page into an IFrame.
The IFrame is displayed into a fancyBox overlay popup.
I have a list with the http links (gets compiled at runtime and it constantly changes).
Using a global variable I can access the list with the links.
But the http link in the list must match the link I have clicked.
If I can even get the link which I have clicked it will also be enough (the link brings up a fancyBox popup so it doesn't actually bring up a new page so to speak)
How to do that?

You have to write some tricky code to achieve this, main goal is to edit the dynamically added page content by adding wrapper tag (with onclick event) around all the links, writing javascript to be called using that wrapper to findout which link has been clicked,
You can try this by doing following steps
1) Get the content of IFrame , using the following JQuery code you can get the content of IFrame
var $currentIFrame = $('#myIFrame');
var content = $currentIFrame.contents();
2) Now manupulate these content by finding all the links inside that page and wrapping them with a tag that should have onclick event e.g. span , you have to write some javascript function to fire on a link if user clicks it.
see the following link for how to manipulate content
Get all links inside iframe and add blank target attribute

Related

Add hyperlink and trigger click event

I know there are a lot of these questions out there. However, the problem I am running into is that I am building out the javascript and/or jquery (tried both) from a C# page that creates a hyperlink, uses ScriptManager to register a startup script and triggers the click event.
The reason I want to do this instead of redirecting the window is sometimes I need to be able to change the target of the link to a blank window. That is where I'm running into the issue. The link is being created without issue, and added to the page and the click event is being fired. But when the target is _blank it is actually opening in a popup window instead of a new tab within the browser. The popup window is the same as a window.open().
There are other static links on my page with target=_blank and they open in a new tab without issue. Just the link that is being created behind the scenes and triggered with javascript open this way. Any ideas?
Pure Javascript Approach:
string js = #"var link = document.createElement('a');
link.href='{0}';
link.target='{1}';
document.body.appendChild(link);
link.click();
link.remove();";
string link = String.Format(js, url, target);
JQuery Approach:
string js = #"$('document').ready(function(){{
$('<a id=""tmpLink"" href=""{0}"" target=""{1}""></a>').appendTo('body');
$('a#tmpLink').trigger('click');
}});";

Including a Handler Response inside a Web Form

I am generating HTML in the Page Load method in more than 1 page.
All those generated HTML are the same across all pages.
I found that it's a pain to have the same codes in every page because once I need to change something, I need to change them in all pages.
Example:
I have a div in each of those page:
<div id="Questions" runat="server"></div>
In the Page Load method of each page, I generate the same HTML.
Questions.InnerHTML = "<span>...etc...</span>";
So I decided to make a page that generates those contents, then load this page inside the div of the other pages, means, if I ever need to change, I only change this page.
I created a Handler, Questions.ashx. This handler generates that HTML and sends back a response.
Now to include it, I know I can use JQUERY's .load() function, but I would like to generate those HTML from server side.
What I've tried:
Questions.InnerHTML = LoadControl("~/Handlers/Questions.ashx").ToString();
But I received this error:
Type 'Questions' does not inherit from 'System.Web.UI.UserControl'.
"LoadControl" is for "User Controls", not HTTP Handlers..
You will probably be better off creating a User Control, which is an .ascx file. This can contain HTML, ASPX controls and code behind, and can be referenced by any ASPX page.
More Info here:
http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx

Allow .ASPX page to display before .ASCX control finishes loading?

I have an ASP.NET page with one control (.ascx) on it. The page (.aspx) onload assigns some text to a couple labels and passes a product ID to the .ascx control. The .ascx control, onload, takes that product ID from the .aspx page and hits the database several times, does several calculations, etc - basically takes a long time to load.
So when I'm clicking a link to this .aspx page, it is taking 7-10 seconds for the page to load. I've narrowed it down to the calculations on the .ascx control being the culprit and I've optimized the code as much as I can ... but it's still taking too long.
Is there a way to load the .aspx page BEFORE the control loads? (Maybe display a "Loading..." animation? Like used in an UpdateProgress?)
You could do this with an UpdatePanel. It will take a little trickery, but try something like this:
1) Put the UserControl in an UpdatePanel.
2) Put a public property on your usercontrol like IsEnabled that it will use to conditionally do nothing or render a "please wait." Set it false from your main page.
3) Add some code in OnInit to your main page:
if (MyScriptManager.IsInAsyncPostback) {
MyUserControl.IsEnabled=true;
}
4) Add a client script along these lines:
finished=false;
Sys.WebForms.PageRequestManager.pageLoaded(function(sender,args) {
if (!finished) {
finished=true;
__doPostBack('','');
// you can include the uniqueID of your updatepanel as the first arg
// otherwise it will refresh all update panels
}
});
or with jquery..
finished=false;
$(document).ready(function() {
if (!finished) {...
}
});
What this should do is cause an async postback to be initiated immediately after the page is done loading, which will in turn cause the update panel to be refreshed. Since you set it to be enabled when it's in an async postback, it will render itself the 2nd time.
The only possible way to achieve this is by setting it up as a separate HTTP resource. At the moment .NET is integrating the control into the page so that it is waiting unti it has everything it needs to respond.
You could do this a multitude of different ways:
Web Service that gets called via javascript
Seperate page which contains the control (and is hosted within an iFrame to appear to be on the same page)
The best way to do this would be to use an iFrame (or something similar) which will instruct the browser to request the control after the main page has been sent).
Personally, I would never use an iFrame to load content on a page - that's more like a hack than anything and plus, iframe == "bad".
But they are right, you won't be able to do anything like what you're looking for.
If the user control DOES NOT have any web controls that cause a postback (or have any form controls that you need to access during a postback), then I would use AJAX to request the data on the server after the page has already loaded and use javascript to display the content on the page.

Masterpage + updatepanel on child page

So I have a masterpage with a login that is in an update panel. I have a child page that has a literal control that should update when the login updates. What it doesn't do is reload the method I use to generate the content for that literal when it posts back. I tried to call the method on the child page from the master page once you click log in, but I get an error that the literal control cannot be found (because it exists on the child page not the master page). How would I reference that control in the masterpage to pass it to my method?
The article below shows how the control tree works with MasterPages and how to reference different controls at different levels of the control tree.
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
So the scenario is that you have an update panel on the child page that when triggered doesn't update/refresh your lets say, label which is in your header on your master page.
What you do is, in the code behind of your master page create a function that changes the value of the label.
Include an update panel on the master page for the label, which is triggered by the textchanged event etc.
Now, in your child page code behind or your let say, button click event, call upon the function that exists in the master page and send the needed value in the parenthesis.
C#:
((MyMaster)this.Page.Master).ShowMessage(text);
VB.NET:
DirectCast(Me.Page.Master, MyMaster).ShowMessage(text)
This should update the label with the correct value whilst triggering the update panel on the master page and thus refreshing your label as well.
I'm about to try this now for myself, wish me luck. :D

Popups with complex functionality using jQuery

I am using jQuery to simulate a popup, where the user will select a series of filters, which I hope to use to rebind a ListView in the original window.
The "popup" is opened via an ajax request and the content is actually a diferent aspx file (the rendered output is injected into a div that acts as the popup).
I have another ListView in this popup, and it has pagination.
My problem is that since the popup is in reality html content inside a div in the same page, when I try to paginate, the whole page postbacks and is replaced with the aspx that has the filters.
How can I fix this?
I tried using an update panel to contain the ListView but it didn't work.
$("div.yourthingie").hide();
Will hide the part you want to show :) Instead of generating the popup on the fly, leave a small part already made, and hide it in the begining, when you need to show, unhide and add the information you need to.
Hope it helps
Either get rid of the HTML "crust" and just produce the <div> with its contents, or use an IFRAME.
First, let's think through what is happening. When you submit the original page, you are taking a "normal" Request/Response trip to get the code. On the page is a JQuery AJAX bit that fires off what is essentially a modal dialog. The desired effect is the user plays with the new page until they have figured out their filters and submits back. The problem is this "modal page" loses information when someone paginates.
The solution to this is fairly simple, in theory. You have to store the "filters" in the popped up page so they can be resent, along with pagination information. OR you have to cache the result set while the user paginates.
What I would do to solve this is create a static page that has the "filters" in place and work out the AJAX kinks separate from having the page post back to a parent page. Once you have all of the AJAX bits working properly, I would then link it into the popup routine and make sure the pagination is still non-problematic. THe final problem is creating a JavaScript routine that sends back to the parent page and allows the parent page to send its JQuery bits back to the server.
I am not sure about the HTML DIV part of the equation and I think you can solve the problem without this solution. In fact, I believe you can make the "modal popup" page without invoking AJAX, if it is possible to either a) submit the filters to apply via the querystring or b) fake a form submit to the second page. The query string is an easier option, but it exposes some info. Faking a form submit is not that difficult, overall, but could be problematic with a popup.
I am just firing off some ideas, but I hope it spurs something for you.

Categories