Retrieving image uploaded from ASP.NET page - asp.net

I've got an ASP.NET upload form on one page, where the user can upload an image. heres the code for that one:
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
<html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
If imageupload1.HasFile Then
imageupload1.SaveAs(Server.MapPath(".") + "/uploadedimages/" & imageupload1.FileName)
Label1.Text = "Received " & imageupload1.FileName & " Content Type " & imageupload1.PostedFile.ContentType & " Length " & imageupload1.PostedFile.ContentLength
Else
Label1.Text = "No uploaded file"
End If
end sub
</script>
</head>
<body>
<form id="imguplad" runat=server>
<asp:FileUpLoad id="imageupload1" AlternateText="You cannot upload files" runat="server" />
<asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server" />
<asp:Label id="Label1" runat="server" />
<input type="button" value="Click here when image is uploaded"
onClick="location.href='imageloadtest.aspx';">
</form>
</body>
</html>
Then I am trying to retrieve it on another page, using the following code:
<%# Page Language="C#" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.IO" %>
...blah
<body>
<img id="image" src="Server.MapPath(".")+"/uploadedimages/"+Request.Form["imageupload1.FileName"]";">
</body>
...blah
Can't seem to get it to show up? I must have some path slightly wrong.
EDIT: By the way, the image uploads perfectly, it just won't show up on the next page.

Try with
<img id="image" src="<%=Server.MapPath("~")%>/uploadedimages/<%=Request.Form["imageupload1.FileName"]%>">
You also may have to set manually the image name in session because you won't have access to imageupload1.FileName in another page, where the control imageupload1 doesn't exist.
So in your click event add Session["ImagePath"] = imageupload1.FileName
And the img tag replace Request.Form["imageupload1.FileName"] by Session["ImagePath"]

Related

Want to call .ascx file from master page upon button click in asp.net web form application

I have created .Master, .aspx and .ascx pages. I want to call .ascx page upon click on button in .master page. If the button is not clicked then .ascx should not show up.
Currently, Onload of page, .ascx page is calling because i have used <uc1:Account runat="server" ID="Account" />. But i want after click of button not on page load.
Any help is highly appreciated. Thank you in advance.
My master page looks like this:
<%# Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Staff.master.cs" Inherits="Admin_Staff" %>
<%# Register Src="~/Controls/Account.ascx" TagPrefix="uc1" TagName="Account" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h4>Account</h4>
<span class="input-group-btn">
<input type="text" class=" search-query form-control" placeholder="Search" />
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</span>
<div class="col-lg-9">
<uc1:Account runat="server" ID="Account" />
</div>
</asp:Content>
My User Control looks like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Acc.ascx.cs" Inherits="Admin_Controls_Account" %>
<asp:panel id="pnlAcc" runat="server">
<section id="AccForm">
<asp:PlaceHolder runat="server" ID="PlaceHolder1" Visible="false">
</asp:PlaceHolder>
<div class="form-group">
<asp:Label runat="server" CssClass="col-md-2 control-label">Country: </asp:Label>
<div class="col-md-10">
<asp:Label runat="server" CssClass="col-md control-label" >New Zealand</asp:Label>
</div>
</div>
</section>
</asp:panel>
My .aspx page
<%# Page Title="" Language="C#" MasterPageFile="~/Admin/Staff.master" AutoEventWireup="true" CodeFile="Staff.aspx.cs" Inherits="Admin_Staff" %>
<asp:Content ID="Content1" ContentPlaceHolderID="StaffContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent1" Runat="Server">
</asp:Content>
You can set the Visibility of the Control to false and change it on Button Click
<uc1:Account runat="server" ID="Account" Visible="false" />
And then on button click
protected void Button1_Click(object sender, EventArgs e)
{
Account.Visible = true;
}
Or you can add the Controls dynamically
protected void Button1_Click(object sender, EventArgs e)
{
Admin_Controls_Account account = (Admin_Controls_Account)LoadControl("~/Controls/Account.ascx");
PlaceHolder1.Controls.Add(account);
}
Note that for the last option you will have to reload the control each time there is a PostBack so you will have to store the Visibility yourself and recreate the control each time the page is loaded.
Simple way is to make the control invisible on the master page.
<uc1:Account runat="server" ID="Account" Visible="False" />
Make it visible on button click.

Content getting outside ContentPlace - ASP.NET

I'm on VS 2008, Windows Xp.
My page:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%= WriteReport()%>
<%= "Hellooo" %>
</asp:Content>
Code-behind:
public string WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
return swriter.ToString();
}
The funny thing is the "Heloo" goes in the contentPlace, but the method's return does not. It's place below the div of the ContentPlace (ends up in the footer div).
Thank you for your help.
EDIT----------------------
Temporary solution
<%= WriteReport()%>
<asp:Table runat="server" ID="testeme">
</asp:Table>
I added a bogus table (id=testeme), it does nothing and has nothing in it. But it works now, what the hell. Only works when below the method.
EDIT2--------------------
I tried to guess the relevant parts from the MasterPage and placed them here.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyAppV2.Views.MasterPage" %>
<div id="general">
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="main">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</div>
</form>
</div>
<div id="footer">
<p>MYFOOTER</p>
</div>
First line of the view using the master page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MP5._Default" MasterPageFile="~/MasterPage.Master" %>
Instead of injecting code into your page using <%= ... %>, you can use an <asp:literal> tag:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:Literal id="MyReport" runat="server" />
</asp:Content>
Then you can write to it from your code-behind using the text property. You can call the WriteReport() method from within Page_Load.
public void WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
MyReport.Text = swriter.ToString();
}
That should solve any oddities of where the code ends up on the page.

Add attachment to email

Can anyone sort out the following code to make the adding an attachment to an email work please.
Thanks
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="ISO-8859-1" Debug="true" %>
<% #Import Namespace="System.Web.Mail" %>
<% #Import Namespace="IO" %>
<script language="vb" runat="server">
Sub btnSendEmail_Click(sender as Object, e as EventArgs)
Dim objMM as New MailMessage()
objMM.To = "my#email.co.uk"
objMM.From = "their#email.co.uk"
objMM.BodyFormat = MailFormat.HTML
objMM.Priority = MailPriority.Normal
objMM.Subject = "Attachment test"
objMM.Body = "There should be an attachment with this email"
objMM.Attachments.Add(new MailAttachment("myimage.jpg"))
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(objMM)
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button runat="server" id="btnSendEmail" Text="Send email" OnClick="btnSendEmail_Click" />
</form>
</body>
</html>
I'm suspicious about
new MailAttachment("myimage.jpg")
I suspect you might want to get the full path e.g.
new MailAttachment(Server.MapPath("Myimage.jpg"))
The file path needs to be a full path but that aside, System.Web.Mail is deprecated/obsolete. You should be using the System.Net.Mail API, see here for examples.

Giving a custom UserControl an ID in rendered HTML

When I use a ASP:Calendar control, and give it an ID:
<asp:Calendar runat="server" ID="MyCal" />
It looks like this in the rendered html:
<table id="NameMangled_MyCal"... />
And I can access the element by ID in javascript like this:
var cal= document.getElementById("<%= MyCal.ClientID%>")
However, When I make a custom user control that has-a calendar:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div>
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>
And then give it an ID when I add it to my page...
<mvs:WeeklyEventsCalendar ID="WeeklyCal" runat="server" />
That ID doesn't show up anywhere in the rendered HTML. all I get is
<div> stuff </div>
When I want
<div id="NameMangled_WeeklyCal"> stuff <div>
What am I doing wrong?
UserControls only render their contents, nothing else. What you could do is
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WeeklyEventsCalendar.ascx.cs"
Inherits="WeeklyEventsCalendar" %>
<div id="<%= this.ControlID %>">
<asp:Calendar runat="server" ID="InnerCal" Width="100%" OnDayRender="RenderCell" OnVisibleMonthChanged="ChangeMonth" Height="480px" />
</div>

How can I enable live preview for FCKeditor in an ASP.Net site?

Over in this question, Scott writes that it is possible to get the current HTML for what's written in the FCKeditor by using FCKeditorAPI.__Instances['instanceNameHere'].GetHTML();
Could someone provide step-by-step instructions on how to accomplish this in an ASP.NET page? All I currently have so far in the .aspx file is this:
<%# Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
<%# Page Title="" Language="C#" ... %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create a new piece</h2>
<form id="form1" runat="server">
<FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server">
</FCKeditorV2:FCKeditor>
<input id="Submit1" type="submit" value="Submit" runat="server" />
</form>
</asp:Content>
In javascript
Well you can do this here:
<script type="text/javascript">
var oEditor = FCKeditorAPI.GetInstance(’IDFromFCKEditor’);
oEditor.Events.AttachEvent( 'OnSelectionChange', function() {
document.getElementById("PreviewDiv").innerHTML = oEditor.GetHTML(true);
}) ;
</script>
Source http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/JavaScript_API

Resources