HtmlInputFile.OnPreRender Method
.NET Framework 3.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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
<p>
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
Community Additions
ADD
Show: