惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
量子位
博客园 - 司徒正美
V
V2EX
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
N
Netflix TechBlog - Medium
L
LangChain Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 不可以

Framework 各版本比较对 用xperf查看系统启动过程 在无IIS下用SharpDevelop进行Asp.net 开发 从程序集中读取流信息 fiddler 学习笔记 .net 拷贝方法 IPhone 应用程序管理 Subversion配置安装 使用Windows Live Writer写博客 xml操作学习 Oracle学习 windbg学习 在博客园添加Web Live Messenger对话框 java调用C#写的com组件 配置文件加入智能提示(Intellisense)功能 让相同的winform程序只启动一个 用户中心 - 博客园 用户中心 - 博客园 去除左边数字行号工具
可以自由控制fileupload文本框与命令按钮之间的距离
不可以 · 2007-06-22 · via 博客园 - 不可以

目标:可以自由控制fileupload文本框与命令按钮之间的距离
原理:修改生成的html中的input type=file控件的属性,隐藏其中文本框,加上一个文本框,实现变相的上传控件。
其生成的html原代码为:
<input id='uploadText' value=''/>&nbsp;&nbsp;<input onpropertychange="uploadText.value=this.value;" type="file" name="MyUploadFile1" id="MyUploadFile1" style="width:0px;border-width:0" />
其中红色就为其中的关键,相信大家都看的懂吧,我就不再解释了
呵呵(只改了一下显示其实质还是原来的fileupload).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace LK.WebControl
{
    /// <summary>
    /// MyUploadFile 的摘要说明
    /// </summary>
    public class MyUploadFile : FileUpload
    {

        public MyUploadFile()
        {
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:0px;border-width:0");
            writer.AddAttribute("onpropertychange", "uploadText.value=this.value;");
            base.AddAttributesToRender(writer);
        }

        #region 属性
        /// <summary>
        /// 文本框与命令按钮的间距
        /// </summary>
        [BrowsableAttribute(true)]
        [DescriptionAttribute("文本框与命令按钮的间距")]
        [CategoryAttribute("外观")]
        public int CellSpace
        {
            get { return _cellSpace; }
            set { _cellSpace = value; }
        }
        private int _cellSpace = 0;

        [BrowsableAttribute(true)]
        [DescriptionAttribute("文本框的样式")]
        [CategoryAttribute("外观")]
        public string TextCss
        {
            get { return _textCss; }
            set { _textCss = value; }
        }
        private string _textCss;

        [BrowsableAttribute(true)]
        [DescriptionAttribute("文本框的宽度")]
        [CategoryAttribute("外观")]
        public string TextWidth
        {
            get { return _textWidth; }
            set { _textWidth = value; }
        }
        private string _textWidth;

        [BrowsableAttribute(true)]
        [DescriptionAttribute("文本框的高度")]
        [CategoryAttribute("外观")]
        public string TextHeight
        {
            get { return _textHeight; }
            set { _textHeight = value; }
        }
        private string _textHeight;
        #endregion

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (Visible)
            {
                string strStyle = "style='{0}'";
                string styleValue = "";
                writer.Write("<input id='uploadText' value='' ");
                if (!string.IsNullOrEmpty(_textWidth))
                    styleValue+= "width:"+_textWidth+"px;";
                if (!string.IsNullOrEmpty(_textHeight))
                    styleValue +="height:"+_textHeight+ "px;";
                if (!string.IsNullOrEmpty(_textCss))
                    writer.Write(" class='" + _textCss + "' ");
                if (!string.IsNullOrEmpty(styleValue))
                    styleValue = string.Format(strStyle,styleValue);
                writer.Write(styleValue );
                writer.Write(" />");
                for (int i = 0; i < _cellSpace; i++)
                {
                    writer.Write("&nbsp;");
                }
            }
            base.RenderControl(writer);
        }
    }
}