如何使用“新”用于创建对象实例的关键字。 [英] how to use the "new" keywords to create an object instance.
本文介绍了如何使用“新”用于创建对象实例的关键字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我是ASP.NET的新手。我整周都得到以下错误:对象引用未设置为对象的实例。我在登录视图中访问了我的控件但仍然可以访问它。以下是我的代码。我得到了它:31或SubmitButton.Attributes(onclick)=return SubmitbButton_click()
任何帮助将不胜感激。
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)
'InsertMsg("File uploaded! " & args.FileName & ", " & args.FileSize & " bytes.")
' Check if the directory we want the image uploaded to actually exists or not
If Not Directory.Exists(MapPath("Gallery\albums\" + albtit + "\")) Then
' If it doesn't then we just create it before going any further
Directory.CreateDirectory(MapPath("Gallery\albums\" + albtit + "\"))
Directory.CreateDirectory(MapPath("Gallery\albums\" + albtit + "\" + "thumbnails\"))
End If
' Specify the upload directory
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)
'Copys the uploaded file to a new location.
'args.CopyTo(path);
Dim originalBMP As System.Drawing.Bitmap
Using stream As Stream = args.OpenStream()
originalBMP = New System.Drawing.Bitmap(stream)
End Using
' Calculate the new image dimensions
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 '*** Fix Thumbnail Width ***'
intWidth = 700 '*** Fix Width ***'
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
' Calculating Horizontal and vertical centering of cropped image
subtoc1 = tnWidth - 140
subtoc2 = tnHeight - 100
ccw = subtoc1 / 2
cch = subtoc2 / 2
' Create a new bitmap which will hold the previous resized bitmap
Dim tnBMP As New Bitmap(originalBMP, tnWidth, tnHeight)
Dim newBMP As New Bitmap(originalBMP, newWidth, newHeight)
' Create a graphic based on the new bitmap
Dim oGrapics As Graphics = Graphics.FromImage(tnBMP)
Dim oGraphics As Graphics = Graphics.FromImage(newBMP)
' Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias
oGraphics.InterpolationMode = InterpolationMode.Low
' Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, tnWidth, tnHeight)
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight)
' Crop thumbnail
Dim thum As Bitmap = CropBitmap(tnBMP, ccw, cch, 140, 100)
' Save the new graphic file to the server
thum.Save(directory__2 + args.FileName)
newBMP.Save(directory__1 + args.FileName)
trans()
'You can also open the uploaded file's data stream.
'System.IO.Stream data = args.OpenStream();
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
' Create the new bitmap and associated graphics object
Dim bmp As New Bitmap(cropWidth, cropHeight)
Dim g As Graphics = Graphics.FromImage(bmp)
' Draw the specified section of the source bitmap to the new one
g.DrawImage(srcBitmap, New Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, _
GraphicsUnit.Pixel)
' Clean up
g.Dispose()
' Return the bitmap
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
' To copy all the files in one directory to another directory.
' Get the files in the source folder. (To recursively iterate through
' all subfolders under the current directory, see
' "How to: Iterate Through a Directory Tree.")
' Note: Check for target path was performed previously
' in this code example.
If System.IO.Directory.Exists(sourcePath) Then
Dim files As String() = System.IO.Directory.GetFiles(sourcePath)
' Copy the files and overwrite destination files if they already exist.
For Each s As String In files
' Use static Path methods to extract only the file name from the path.
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">
<!--<link rel="stylesheet" href="Styles/demo.css" type="text/css" />-->
</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="halftab_shadow"></div>-->
<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 class="slider_shadow"></div>-->
</div>
</asp:Content>
推荐答案
Please try creating the javascript,in script try to call the code behind method
SubmitButton.Attributes.Add(\"onclick\", \"javascript:SubmitbButton_click()\")
Please try creating the javascript,in script try to call the code behind method
SubmitButton.Attributes.Add("onclick", "javascript:SubmitbButton_click()")
这篇关于如何使用“新”用于创建对象实例的关键字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文