Given the following code below. How might I display another web page or content within the placeholder via a server onClick event in C# code; similar to now defunct iframe. I perfer to handle all events via sever side code (C#, asp.net 4.0)
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder"
runat="Server">
<asp:PlaceHolder runat="server" ID="PlaceHolder1">
</asp:PlaceHolder>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="NavigationContentPlaceHolder" Runat="Server">
<uc1:NavigationUserControl runat="server" ID="NavigationUserControl" />
</asp:Content>
Thank you in advance!
You can use WebClient to receive a page from a URI. (It can also send content to a URI too.)
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click"
Text="Load Page"/>
<asp:PlaceHolder runat="server" ID="PlaceHolder1">
</asp:PlaceHolder>
protected void Button1_Click(object sender, EventArgs e)
{
var uri = new Uri("http://www.stackoverflow.com");
var result = new WebClient().DownloadString(uri);
PlaceHolder1.Controls.Add(new LiteralControl(result));
}
Related
I'm working on an ASP.NET site and I'm using HttpGetRequest & HttpGetResponse to place the contents of https://news.google.com/news into a literal control on one of my pages.
The problem is that the stream is covering the entire page. I can't manage to control the size with a div or container. So the stream from the literal is covering a label that is above it (in its own div) and a menu that's inherited from SiteMaster.Master.
How can I keep the menu and label above the literal control?
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
HttpWebRequest googRequest = (HttpWebRequest)HttpWebRequest.Create("https://news.google.com/news");
HttpWebResponse resp = (HttpWebResponse)googRequest.GetResponse();
string googNews = new System.IO.StreamReader(resp.GetResponseStream()).ReadToEnd();
Literal1.Text = googNews;
Label1.Text = DateTime.Now.ToLongTimeString();
}
ASP:
<%# Page Title="" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="Literally.aspx.cs" Inherits="DNut.Literally" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<p>
<asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="Larger" Font-Italic="true"></asp:Label>
</p>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</asp:Content>
The content, based on how you are receiving it, will likely include an html tag, head tag and body tag. This isn't valid markup to inject into the middle of an existing html document. The typical way to include a whole page like this in another html document is via an iframe tag.
Iframe's are frowned on a bit in the web dev world and not used alot but in this case it's its a good fit for what you are trying to do. It's also helps protect your site against Cross-site scripting (XSS) attacks (verses trying to inject markup into a div via literal tag.
With an iframe tag you set the 'src' attribute to the url you want to display and you won't need the literal or the related c# code to set it.
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<iframe src='https://news.google.com/news'></iframe>
</asp:Content>
You can style up the iframe to have a desirable height and width.
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" ClientIDMode="AutoID" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">
<%
foreach (var item in AllSales)
{
//Here i have just set a breakpoint to see if it loops the AllSales list when I press the update button
}
%>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The script manager code is in the masterpage :
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Problem here is that everytime i click the Update button it loads the page again and loops the "AllSales" list , i want to only update a section and not have to do unnecessary loops.
Here is the fun part : If i remove the masterpage, it works ! But with the masterpage, it dont , why?!
You need to use a server control to initiate the update rather than a normal html input button. Try using:
<asp:Button ID="ActivitySubmit" Text="Submit" runat="server" />
The reason for such behavior is that you use plain html submit button instead of ASP.NET server button. Thus page submitted to server without involving ASP.NET Ajax functionality. Replace ActivitySubmit button with asp:Button control
Set the UpdateMode mode property to Conditional. The default value for UpdateMode is Always, If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page, reference
Edit: You are using input type="submit" which probably causing the post back replace it with asp:Button to get the ajax call to work.
I have tested the same code with only change in the button type, and it refreshes only the update panel content. The problem is your site is targeting .NET 3.0 but you need to target at least to 3.5. I have tested on 3.0, it does not work but on 3.5 and 4.0, it works fine. So the easier and safer solution is to target 3.5 onwards. But If you want to use .NET 3.0, I will try to find a workaround. Please let me know.
I tried to recreate your problem but it is working fine in my case. Try creating a new .aspx file and paste the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Outside UpdatePanel</p>
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="SubmitButton" Text="Go" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
This was tested in VS2010 using .NET 4.0 in an Empty Web Application. Does this work for you as well? I tried the same example using a Master page with a content placeholder. This yields the same (properly working) results. From this I gather there is something else going on on your page that we're missing. Is there?
[Edit]
I made another simple example, this time with a Master page and a Content page.
Master page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<p>Master Page: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Again this works without a problem in the same environment as the other example. It looks like you'll have to tell me about the .NET framework you're targeting and what else you've got set up in your master page and aspx page, because:
In .Net 3.5 and up this will work. In lower versions however, it won't. I'm afraid I've been unable to figure out how to fix it in lower versions though. :(
In your Page tag just add
ClientIDMode="AutoID"
I think you can try and put the form inside the updatepanel, or you can try and change children as triggers property of the updatepanel like this :
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="False">
and then add the button as trigger like this
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(SubmitButton);
try addind this part to your UpdatePanel:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Submit" />
</Triggers>
I´m trying to use UpdateProgress with Triggers (see the code bellow) but when a button assigned as an asyncPostBackTrigger is clicked, the UpdateProgress doesn´t work.
If I remove the AssociatedUpdatePanelID property, the UpdateProgress control works. But I want to configure independent UpdateProgress so, I need to specify the
AssociatedUpdatePanelID property of UpdateProgress control.
Is this behaviour as it is supposed to be?
NOTE: I do not want to intercept the Sys.WebForms.PageRequestManager
instance and manipulate the asyncronous request to manually display
and hide the UpdateProgress element. Is there a way to do that?
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AjaxExtensionsTest._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>
<h2>
Ajax Extensions Test
</h2>
<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="content">
<asp:TextBox ID="txtDataHora" runat="server"></asp:TextBox>
<asp:UpdateProgress ID="progress1" AssociatedUpdatePanelID="up1" DynamicLayout="true" DisplayAfter="0" runat="server">
<ProgressTemplate>
<div>
<img alt="progress" src="loading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSubmit" Text="Get Current Date/Time" runat="server" onclick="btnSubmit_Click" />
<p>
</p>
</asp:Content>
<script runat="server" language="csharp">
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(btnSubmit);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtDataHora.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
System.Threading.Thread.Sleep(2000);
}
</script>
If you need to use AsyncPostBackTrigger and AssociatedUpdatePanelID then your only option is to handle events on the Sys.WebForms.PageRequestManager instance and manually display and hide the UpdateProgress element. There is code out there that runs on the server and injects the necessary JavaScript that could be encapsulated into a control to make doing this quite tidy.
I know this must sound really basic but I'm really stumped here. What I'm trying to do is to show a Hyperlink once a process has completed. And this process is the AsyncFileUpload. In the ASPX page, I want to create an but have it hidden on the initial page load. If I set the Style="display: none;" seems to work but after the file upload, nothing I do, will make the control visible again. When the file is uploaded, it calls a function called FileUploadComplete. It's in here that no matter what I do, the Hyperlink won't display.
Any help is greatly appreciated :)
Thank you,
dave
Here is the ASPX Code (with recently added javascript)
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="OptionsPlaceHolder" runat="server">
<script language="javascript" type="text/javascript">
function ShowLink() {
$("#openFile").show();
}
</script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
<asp:UpdatePanel ID="updImportFile" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="pageHeader">
<asp:Literal runat="server" ID="pageTitle" Text="<%$ Resources:Resources, ImportFile %>" />
</div>
<ajaxToolkit:AsyncFileUpload ID="FileUpload1" runat="server" Width="600px"
UploaderStyle="Traditional" OnUploadedComplete="FileUploadComplete" ThrobberID="throbber"
CompleteBackColor="#E9F2FD" OnClientUploadComplete="ShowLink" />
<asp:Image runat="server" ID="throbber" ImageUrl="images/loading.gif" />
<br />
<asp:Hyperlink runat="server" ID="openFile" NavigateUrl="~/OpenFile.aspx" Text="Open"
style="display:none;"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
And here is the code behind:
protected void FileUploadComplete(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string importName = Server.MapPath(#"Uploads\") + FileUpload1.FileName;
FileUpload1.SaveAs(importName);
// Import the JSA
JSA jsa = new JSA();
jsa.Import(importName);
// Show the Hyperlink
ShowLink();
}
}
private void ShowLink()
{
openFile.Attributes["Style"] = string.Empty;
}
I didn't include the master page code. It has the ToolkitScriptManager in it.
Are you trying to show on client-side or server-side? Is the link a client-side, or server-side object? Javascript would be the standard way.
If the control is a client-side object:
document.getElementById("hyperlink_name").style.display = "block";
Or if it is a server-side object:
document.getElementById("<%= hyperlink_name.ClientID %>").style.display = "block";
I would recommend getting jQuery and using the following though:
$('#hyperlink_name').show();
Or you can use an ASP.Net Link Button and do it server-side:
linkButton.Visible = true;
It would be more helpful if you would post some of the code you have tried already so that we can get a better idea of where you are.
{first answer deleted}
[EDIT :I didn't catch that you are using AsyncFileUpload when I first read the question]
Using AsyncFileUpload inside an update panel the server is being accessed via a partial postback, as a result other controls (the hyperlink) cannot be affected on the server. This will require that you make use of javascript (or preferably jquery) to make the change on the client.
You can do it on OnClientUploadComplete function, but you have to reference the hyperlink like this: <%= hyperLink.ClientID %>.style.display = 'block';
Another more asp.net way is to use update panels. Put the hyperlink into an UpdatePanel and set a trigger on the UpdatePanel when the file is uploaded. then change the visibility on the server at UploadedComplete event.
Hi all I have an update panel that works lovely in my test solution but when I put it into the main project it does not work correctly.
I've made it very simple, but still no joy, it consists of:
the file upload control
a link button
the link button has an onclick method that takes the file and creates a byte array. For some reason the contentLength is -2 every time. It does not matter what type of file I am using. Every time!
This is very frustrating considering it works fine in my test solution.
Is there anything I am missing or should be looking at?
Thanks :)
EDIT:
I am using VS2008
CODE:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:LinkButton ID="btnUpload" runat="server" ValidationGroup="uploadform" CssClass="uploadbutton" OnClick="btnUpload_Click">Upload</asp:LinkButton>
</form>
</body>
</html>
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
var intDoccumentLength = FileUpload1.PostedFile.ContentLength;
// will crash here as content length is -2 for some reason~???
byte[] newDocument = new byte[intDoccumentLength];
}
Your test code must be doing complete postback which is not the case for Updatepanel part as part of main project. With updatepanel, file may not have been uploaded at point of time when you are checking its contentlenght; this is not true for complete postback where file always gets uploaded first. In this case, it will always through you an error. This can only be done using an ActiveX control of some kind.
This article may give you nice hint and direction: asp.net FileUpload event after choice is made and before submit for upload
The file up loader is not working well in the update panel so if you are using the update panel then you have to use the triggers in the update panel and you have to give the name of the control or button on which you are clicking to upload the file
<%# Page Language="C#" MasterPageFile="~/FullViewMasterPage.master" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" Title="Untitled Page" %>
<%# Register TagPrefix="yaf" Namespace="YAF" Assembly="YAF" %>
<%# Register TagPrefix="yc" Namespace="YAF.Controls" Assembly="YAF" %>
<asp:Content ID="Content1" ContentPlaceHolderID="FullViewContentPlaceHolder" Runat="Server">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload id="fileUpload" runat="server" ></asp:FileUpload>
<asp:Button ID="Upload" runat="server" OnClick="Upload_Click" Text="Upload The Image" /><br />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Upload" EventName="Upload_Click" />
</Triggers>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime3" runat="server" /><br />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</asp:Content>
<code>
As FileUpload.PostedFile.InputStream.Length returns the same as FileUpload.PostedFile.ContentLength, did you check that your file size is lower than maxRequestLength (4MB by default) or specified in web.config :
<system.web>
<httpRuntime maxRequestLength="8192"/>
</system.web>
See : http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx