HtmlInputFile.OnPreRender(EventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 HtmlInputFile 控制項的 PreRender 事件。
protected:
override void OnPreRender(EventArgs ^ e);
protected public:
override void OnPreRender(EventArgs ^ e);
protected override void OnPreRender (EventArgs e);
protected internal override void OnPreRender (EventArgs e);
override this.OnPreRender : EventArgs -> unit
Protected Overrides Sub OnPreRender (e As EventArgs)
Protected Friend Overrides Sub OnPreRender (e As EventArgs)
參數
範例
下列程式碼範例示範如何覆寫 OnPreRender 方法, Title
以便一律將屬性新增至每個自訂 HtmlInputFile 控制項。 若要讓此範例正常運作,您必須在電腦的磁片磁碟機 C 上建立名為 Temp
的目錄。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Button1_Click(object Source, EventArgs e)
{
// Make sure a file was submitted.
if (HtmlInputText1.Value == "")
{
Span1.InnerHtml = "Error: You must enter a file name.";
}
// Save the file.
else if (HtmlInputFile1.PostedFile != null)
{
try
{
HtmlInputFile1.PostedFile.SaveAs("c:\\temp\\" + HtmlInputText1.Value);
Span1.InnerHtml = "File uploaded successfully to: <b>c:\\temp\\" +
HtmlInputText1.Value + "</b> on the Web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving <b>c:\\temp\\" +
HtmlInputText1.Value + "</b><br />" + exc.ToString() + ".";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom HtmlInputFile OnPreRender Example</title>
</head>
<body>
<form id="form1" enctype="multipart/form-data"
runat="server">
<h3>Custom HtmlInputFile OnPreRender Example</h3>
Select File to Upload:
<aspSample:CustomHtmlInputFileOnPreRender
id="HtmlInputFile1"
type="file"
runat="server"
name="HtmlInputFile1">
<p>
Save as file name (no path):
<input id="HtmlInputText1"
type="text"
runat="server"
name="Text1" />
</p>
<p>
<span id="Span1"
style="font: 8pt verdana;"
runat="server" />
</p>
<p>
<input type="button"
id="Button1"
value="Upload"
onserverclick="Button1_Click"
runat="server"
name="Button1" />
</p>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Button1_Click(ByVal source As Object, ByVal e As EventArgs)
' Make sure the file was submitted.
If HtmlInputText1.Value = "" Then
Span1.InnerHtml = "Error: You must enter a file name."
' Save the file
ElseIf Not (HtmlInputFile1.PostedFile Is Nothing) Then
Try
HtmlInputFile1.PostedFile.SaveAs("c:\\temp\\" & HtmlInputText1.Value)
Span1.InnerHtml = "File uploaded successfully to: <b>c:\temp\" _
& HtmlInputText1.Value & "</b> on the Web server."
Catch exc As Exception
Span1.InnerHtml = "Error saving <b>c:\temp\" _
& HtmlInputText1.Value & "</b><br />" _
& exc.ToString() & "."
End Try
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom HtmlInputFile OnPreRender Example</title>
</head>
<body>
<form id="form1" enctype="multipart/form-data"
runat="server">
<h3>Custom HtmlInputFile OnPreRender Example</h3>
Select File to Upload:
<aspSample:CustomHtmlInputFileOnPreRender
id="HtmlInputFile1"
type="file"
runat="server"
name="HtmlInputFile1">
<p>
Save as file name (no path):
<input id="HtmlInputText1"
type="text"
runat="server"
name="Text1" />
</p>
<p>
<span id="Span1"
style="font: 8pt verdana;"
runat="server" />
</p>
<p>
<input type="button"
id="Button1"
value="Upload"
onserverclick="Button1_Click"
runat="server"
name="Button1" />
</p>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomHtmlInputFileOnPreRender : System.Web.UI.HtmlControls.HtmlInputFile
{
protected override void OnPreRender(System.EventArgs e)
{
// Call the base OnPreRender method.
base.OnPreRender(e);
// Add a Title attribute to the HtmlInputFile control.
this.Attributes.Add("title", "Click the Browse button to select a file to upload.");
}
}
}
Imports System.Web
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomHtmlInputFileOnPreRender
Inherits System.Web.UI.HtmlControls.HtmlInputFile
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
' Call the base OnPreRender method.
MyBase.OnPreRender(e)
' Add a Title attribute to the HtmlInputFile control.
Me.Attributes.Add("title", "Click the Browse button to select a file to upload.")
End Sub
End Class
End Namespace
備註
如果未指定網頁控制項的 HtmlForm 編碼方式,則 Enctype 屬性會設定為 「multipart/form-data」。