Hi All, I'm a newbie to ASP.NET. I get the following error for the whole week: Object reference not set to an instance of an object. I've access my controls in my loginview but still get this. Below is my code. I get it at line: 31 or "SubmitButton.Attributes("onclick") = "return SubmitbButton_click()""
Any help will be much appreciated.
<%@ Page Title="Gallery" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false"%>
<%@ Register Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" TagPrefix="CuteWebUI" %>
<%@ Register Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" TagPrefix="CuteWebUI" %>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.Collections"%>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.Security"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.HtmlControls"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts"%>
<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace="System.Drawing.Drawing2D"%>
<%@ Import Namespace="System.Drawing.Imaging"%>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ListFolders()
End If
End Sub
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
Dim SubmitButton As Button = Gallog.FindControl("SubmitButton")
SubmitButton.Attributes("onclick") = "return SubmitbButton_click()"
End Sub
Private uploadcount As Integer = 0
Private Sub SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Uploader_FileUploaded(ByVal sender As Object, ByVal args As UploaderEventArgs)
uploadcount += 1
Dim albit As TextBox = Gallog.FindControl("albTit")
Dim albtit As String = albit.Text
Dim uploader As Uploader = DirectCast(sender, Uploader)
If Not Directory.Exists(MapPath("Gallery\albums\" + albtit + "\")) Then
Directory.CreateDirectory(MapPath("Gallery\albums\" + albtit + "\"))
Directory.CreateDirectory(MapPath("Gallery\albums\" + albtit + "\" + "thumbnails\"))
End If
Dim directory__1 As String = Server.MapPath("Gallery\albums\" + albtit + "\")
Dim directory__2 As String = Server.MapPath("Gallery\albums\" + albtit + "\" + "thumbnails\")
Dim nu As Double = uploadcount
Dim num As String = CStr(nu)
Dim originalBMP As System.Drawing.Bitmap
Using stream As Stream = args.OpenStream()
originalBMP = New System.Drawing.Bitmap(stream)
End Using
Dim origWidth As Integer = originalBMP.Width
Dim origHeight As Integer = originalBMP.Height
Dim newWidth As Integer
Dim newHeight As Integer
Dim tnWidth, tnHeight As Integer
Dim tintWidth, tintHeight As Integer
Dim intWidth, intHeight As Integer
Dim ccw, cch As Double
Dim subtoc1, subtoc2 As Integer
tintHeight = 100
intWidth = 700
Dim multtnWiHe As Integer = origWidth * tintHeight
Dim multWiHe As Integer = origHeight * intWidth
tintWidth = multtnWiHe / origHeight
intHeight = multWiHe / origWidth
If origWidth <= 100 Or origHeight <= 100 Then
tnWidth = origWidth
tnHeight = origHeight
Else
tnWidth = tintWidth
tnHeight = tintHeight
End If
If origWidth <= 700 Or origHeight <= 600 Then
newWidth = origWidth
newHeight = origHeight
Else
newWidth = intWidth
newHeight = intHeight
End If
subtoc1 = tnWidth - 140
subtoc2 = tnHeight - 100
ccw = subtoc1 / 2
cch = subtoc2 / 2
Dim tnBMP As New Bitmap(originalBMP, tnWidth, tnHeight)
Dim newBMP As New Bitmap(originalBMP, newWidth, newHeight)
Dim oGrapics As Graphics = Graphics.FromImage(tnBMP)
Dim oGraphics As Graphics = Graphics.FromImage(newBMP)
oGraphics.SmoothingMode = SmoothingMode.AntiAlias
oGraphics.InterpolationMode = InterpolationMode.Low
oGraphics.DrawImage(originalBMP, 0, 0, tnWidth, tnHeight)
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight)
Dim thum As Bitmap = CropBitmap(tnBMP, ccw, cch, 140, 100)
thum.Save(directory__2 + args.FileName)
newBMP.Save(directory__1 + args.FileName)
trans()
Dim Label1 As Label = Gallog.FindControl("Label1")
Label1.Text = "<span style = 'color: green;'>You have successfully added " + num + " file(s)</span>"
End Sub
Private _uploadDir As DirectoryInfo
Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
Dim SubmitButton As Button = Gallog.FindControl("SubmitButton")
Dim Uploader1 As UploadAttachments = Gallog.FindControl("Uploader1")
SubmitButton.Attributes("itemcount") = Uploader1.Items.Count.ToString()
MyBase.OnPreRender(e)
End Sub
Public Overloads Shared Function CropBitmap(ByVal srcBitmap As Bitmap, _
ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
Dim bmp As New Bitmap(cropWidth, cropHeight)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(srcBitmap, New Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, _
GraphicsUnit.Pixel)
g.Dispose()
Return bmp
End Function
Private Sub ListFolders()
Dim f As New DirectoryInfo(Server.MapPath("~/Gallery/albums/"))
Dim list As New ArrayList()
Dim dirs() As DirectoryInfo = f.GetDirectories()
For Each d As DirectoryInfo In dirs
list.Add(d)
Next
Repeater1.DataSource = list
Repeater1.DataBind()
End Sub
Public Sub trans()
Dim alTit As TextBox = Gallog.FindControl("alTit")
Dim alti As String = alTit.Text
Dim sourcePath As String = Server.MapPath("~/Gallery/PageHub/")
Dim targetPath As String = Server.MapPath("~/Gallery/albums/" + alti + "/")
Dim fileName As String = String.Empty
Dim destFile As String = String.Empty
If System.IO.Directory.Exists(sourcePath) Then
Dim files As String() = System.IO.Directory.GetFiles(sourcePath)
For Each s As String In files
fileName = System.IO.Path.GetFileName(s)
destFile = System.IO.Path.Combine(targetPath, fileName)
System.IO.File.Copy(s, destFile, True)
Next
Else
Dim Label1 As Label = Gallog.FindControl("Label1")
Label1.Text = "Source path does not exist!"
End If
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<div id="container">
<h2><span style="color: Black; font-weight: normal;">Gallery</span></h2>
<div class="effect6 box6">
<div class="box_content6"></div>
</div>
<br />
<asp:LoginView ID="Gallog" runat="server" EnableViewState="false">
<AnonymousTemplate>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:HyperLink ID="HyperLink1" runat="server">Upload Pictures</asp:HyperLink>
<asp:Panel ID="pnladd" runat="server">
<div class="can-layout-wrapper">
<div class="can-removed-layout" style="width: 976px;">
<div class="can-content-layout-row">
<div class="can-layout-cell" style="width: 465px;">
<br />
<p>Add photo to an album<br />(Select the album you want to add photos to):</p>
<br />
</div>
<div class="can-layout-cell" style="width: 465px;">
<br />
<p>Create an Album</p>
<br />
<p><asp:Label ID="Label2" runat="server" Text="Album's title:"></asp:Label> <asp:TextBox ID="alTit" runat="server" Width="225px" style="padding: 2px; border-radius: 4px; box-shadow:0 0 2px 1px rgba(0, 0, 0, 0.35);" Height="14"></asp:TextBox></p>
<br />
<p>
<CuteWebUI:UploadAttachments runat="server" ManualStartUpload="true" ID="Uploader1"
InsertText="Browse Files (Max 7M)" önFileUploaded="Uploader_FileUploaded"
ShowCheckBoxes="False" ValidateOption-AllowedFileExtensions="jpg, jpeg, png"
RemoveButtonBehavior="None" ShowRemoveButtons="False"
TableHeaderTemplate="<td nowrap='nowrap'></td><td>Uploaded Files</td>">
<ValidateOption MaxSizeKB="7024" AllowedFileExtensions="jpg,jpeg,png" />
</CuteWebUI:UploadAttachments>
</p>
<br />
<p>
<asp:Button runat="server" ID="SubmitButton" Text="Upload" OnClick="SubmitButton_Click"/>
</p>
<br />
<p>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>
<script type="text/javascript">
function submitbutton_click() {
var submitbutton = document.getElementById('<%=Gallog.FindControl("SubmitButton").ClientID %>');
var uploadobj = document.getElementById('<%=Gallog.FindControl("Uploader1").ClientID %>');
var tit = document.getElementById('<%=Gallog.FindControl("alTit").ClientID %>').value;
if (!window.filesuploaded) {
if (uploadobj.getqueuecount() > 0 && tit != "") {
uploadobj.startupload();
}
else {
var uploadedcount = parseInt(submitbutton.getAttribute("itemcount")) || 0;
if (uploadedcount > 0 ) {
return true;
}
alert("Please browse and select files and enter album's title before upload");
}
return false;
}
window.filesuploaded = false;
return true;
}
function CuteWebUI_AjaxUploader_OnPostback() {
window.filesuploaded = true;
var submitbutton = document.getElementById('<%=Gallog.FindControl("SubmitButton").ClientID %>');
submitbutton.click();
return false;
}
</script>
</div>
</div>
</div>
</div>
</asp:Panel>
</LoggedInTemplate>
</asp:LoginView>
<div class="can-layout-wrapper">
<div class="can-content-layout">
<div class="can-content-layout-row">
<p>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<a href="<%#"~/Gallery/albums/" + Eval("Name") + "/" %>" title="<%# Eval("Name") %>">
<asp:Label ID="lblfolderName" runat="server" Text='<%# Eval("Name") %>'></asp:Label></a>
</ItemTemplate>
</asp:Repeater>
</p>
</div>
</div>
</div>
</div>
</asp:Content>