HtmlInputFile.OnPreRender Method
.NET Framework 2.0
Raises the PreRender event for the HtmlInputFile control.
Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
The following code example demonstrates how to override the OnPreRender method so that a Title attribute is always added to each custom HtmlInputFile control. For this example to work properly, you need to create a directory called Temp on your computer's drive C.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL.Controls" Assembly="Samples.AspNet.JSL" %>
<%@ Page Language="VJ#" AutoEventWireup="True" %>
<HTML>
<HEAD>
<title>Custom HtmlInputFile - OnPreRender - VJ# Example</title>
<script runat=server>
void Button1_Click(Object source, EventArgs e)
{
if (HtmlInputText1.get_Value().Equals("")) {
Span1.set_InnerHtml("Error: you must enter a file name");
}
else if (HtmlInputFile1.get_PostedFile() != null) {
String tempPath = Environment.GetFolderPath(Environment.SpecialFolder.
InternetCache) + "\\";
try {
HtmlInputFile1.get_PostedFile().SaveAs(tempPath + HtmlInputText1.
get_Value());
Span1.set_InnerHtml("File uploaded successfully to:<br><b>"
+ tempPath + HtmlInputText1.get_Value()
+ "</b><br>on the Web server");
}
catch (Exception ex) {
Span1.set_InnerHtml("Error saving file:<br><b>"
+ tempPath + HtmlInputText1.get_Value() + "</b><br>"
+ ex.ToString());
}
}
} //Button1_Click
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<h3>Custom HtmlInputFile - OnPreRender - VJ# Example</h3>
Select File to Upload:
<aspSample:CustomHtmlInputFileOnPreRender
id="HtmlInputFile1"
type="file"
runat="server"
name="HtmlInputFile1">
</p>
<p>
Save as filename (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>
package Samples.AspNet.JSL.Controls;
public class CustomHtmlInputFileOnPreRender
extends System.Web.UI.HtmlControls.HtmlInputFile
{
protected void OnPreRender(System.EventArgs e)
{
// Call the base OnPreRender method.
super.OnPreRender(e);
// Add a Title attribute to HtmlInputFile.
this.get_Attributes().Add("title",
"Click the Browse button to select a file to upload.");
} //OnPreRender
} //CustomHtmlInputFileOnPreRender
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Community Additions
ADD
Show: