HtmlInputFile.OnPreRender(EventArgs) Yöntem

Tanım

Denetim için olayı tetikler PreRenderHtmlInputFile .

C#
protected override void OnPreRender(EventArgs e);
C#
protected internal override void OnPreRender(EventArgs e);

Parametreler

e
EventArgs

EventArgs Olay verilerini içeren bir.

Örnekler

Aşağıdaki kod örneği, her özel HtmlInputFile denetime her zaman bir Title özniteliğin OnPreRender eklenmesi için yönteminin nasıl geçersiz kılındığını gösterir. Bu örneğin düzgün çalışması için bilgisayarınızın C sürücüsünde adlı Temp bir dizin oluşturmanız gerekir.

ASP.NET (C#)
<%@ 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>
C#
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.");
        }
    }
}

Açıklamalar

Web sayfasının HtmlForm denetimi için kodlama belirtilmezse, Enctype özelliği "multipart/form-data" olarak ayarlanır.

Şunlara uygulanır

Ürün Sürümler
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Ayrıca bkz.