ASP.NET项目申报系统-5.3~5.4(FileUpload和GridView用法)

5.3

GridView中,某列的内容超过它的长度后显示省略号(...)而不是把GridView撑大。
关键点是GridView的CssClass和ItemStyle中的CssClass。

页面前台:

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0 style="font-size:small">
    <tr>
<td align="center">
<asp:GridView ID="GridViewDown" runat="server" ShowHeader="false"  ShowFooter="false" AllowPaging="true" PageSize="6"
            AutoGenerateColumns="False" BorderWidth="0" Width="98%" GridLines="None" CssClass="GridViewCSS"
            style="margin-bottom: 0px">
            <PagerSettings Visible="false"/>
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Image ID="Imagelist" runat="server" ImageUrl="~/Images/homepage/list.gif" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField DataTextField="FileName" DataNavigateUrlFields="FileName" DataNavigateUrlFormatString="../Upload/Down/{0}">
        <ItemStyle Width="96%" CssClass="content" />
        </asp:HyperLinkField>
        </Columns>
        </asp:GridView>

</td>
</tr>
</TABLE>

Css文件:

.GridViewCSS
{
    table-layout: fixed;/*重要*/
    text-align:center;
}
.content
{
    text-align:left;
    width:100%;
    /*下面三条重要*/
    overflow:hidden; 
    white-space:nowrap;
    text-overflow:ellipsis; /*显示省略号*/
}

 

5.4

FileUpload的用法:

页面前台:

<asp:HiddenField ID="HiddenFieldfilename" runat="server" />
<table  cellspacing="0" cellpadding="0" class="TableMain">
    
    <tr>
    <td height="24px"class="TableLineItem" colspan="4">
    <span style="color:White;font-size:medium">&nbsp;申报单位信息附件上传</span>
    </td>
    </tr>

<tr>
    <td class="TD1" rowspan="2">企业法人营业执照</td>
    <td>
        <asp:FileUpload ID="FileUpload1" runat="server" Size="50"/>
    </td>
    <td width="30px">
        <asp:Button ID="ButtonUpload1" runat="server" Text="上传" OnClientClick="return beforeUploadUnit(1);"
            onclick="ButtonUpload1_Click"/>
    </td>
    <td width="30px">
    </td>
    </tr>
<tr>
    <td class="TDBottom">
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </td>
    <td width="30px" class="TDBottom">
        <asp:Button ID="ButtonDown1" runat="server" Text="下载" Visible="false" 
            onclick="ButtonDown1_Click"/>
            </td>
            <td width="30px" class="TDBottom">
            <asp:Button ID="ButtonDelete1" runat="server" Text="删除" 
            onclick="ButtonDelete1_Click" Visible="false"/>
        
    </td>
    </tr>

...

</table>

JavaScript代码:

function beforeUploadUnit(obj) {
    var file = document.getElementById("FileUpload" + obj).value;
    var fileArray = file.split('\\');
    var filename = fileArray[fileArray.length - 1];

    //限制上传附件的类型
    var suffix = filename.substring(filename.lastIndexOf(".") + 1);
    if (suffix != "pdf" && suffix != "doc" && suffix != "docx" && suffix != "xls" && suffix != "xlsx" && suffix != "rar" && suffix != "zip") {
        alert("上传文件的格式不合格,请重新上传!");
        return false;
    }
    //限制上传文件名的长度
    if (filename.length > 45) {
        alert("上传文件的名称不能长于45个字符!");
        return false;
    }
    //限制上传文件不能同名
    var nameinfo = document.getElementById("HiddenFieldfilename").value;
    var nameArray = nameinfo.split('|');
    for (var i = 0; i < nameArray.length; i++) {
        if (nameArray[i] == filename) {
            break;
        }
    }
    if (i < nameArray.length) {
        alert("上传文件不能同名,请修改后再上传!");
        return false;
    }

    return true;
}

后台代码:

//Page_Onload里面进行初始化

//修改HiddenFieldfilename的值
    private void removeFileName(string filename)
    {

        int startIndex = HiddenFieldfilename.Value.IndexOf(filename);
        if (startIndex == 0)
        {
            if (filename.Length == HiddenFieldfilename.Value.Length)
            {
                HiddenFieldfilename.Value = "";
            }
            else
            {
                HiddenFieldfilename.Value = HiddenFieldfilename.Value.Remove(startIndex, filename.Length);
            }
        }
        else
        {
            HiddenFieldfilename.Value = HiddenFieldfilename.Value.Remove(startIndex - 1, filename.Length + 1);
        }
    }
    //修改HiddenFieldfilename的值
    private void addFileName(string filename)
    {
        if (HiddenFieldfilename.Value == "")
        {
            HiddenFieldfilename.Value = filename;
        }
        else
        {
            HiddenFieldfilename.Value += "|" + filename;
        }
    }

protected void ButtonDelete1_Click(object sender, EventArgs e)
    {
        if (Label1.Text != "")
        {
            string id = Session["UnitID"].ToString();
            string file = Server.MapPath("~/Upload/UnitAccessory") + "\\" + id + Label1.Text;
            //获取文件属性
            FileAttributes attrs = File.GetAttributes(file);
            // 下面表达式中的 1 是 FileAttributes.ReadOnly 的值  
            // 此表达式是把 ReadOnly 所在的位改成 0
            attrs = (FileAttributes)((int)attrs & ~(1));
            File.SetAttributes(file, attrs);
            File.Delete(file);
            //修改HiddenFieldfilename
            removeFileName(Label1.Text);
            //对数据库中的值进行更新,没有附件的时候,就删除所对应的table
            UnitAccessoryManage accessManage = new UnitAccessoryManage();
            if (HiddenFieldfilename.Value == "")
            {
                accessManage.Delete(id);
            }
            else
            {
                UnitAccessory access = accessManage.GetModel(id);
                access.LegalLicense = null;
                accessManage.Update(access);
            }
            Label1.Text = "";
            ButtonDelete1.Visible = false;
            ButtonDown1.Visible = false;
            FileUpload1.Enabled = true;
            ButtonUpload1.Enabled = true;
        }
    }
    protected void ButtonUpload1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fileFormat = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".")+1);
            //alert中显示aspx中定义的变量
            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert(' "+fileFormat+" ')</script>");
            if (fileFormat != "doc" && fileFormat != "docx" && fileFormat != "xls" && fileFormat != "xlsx" && fileFormat != "pdf" && fileFormat != "rar" && fileFormat != "zip")
            {
                ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('上传文件格式不正确,请重新上传!')</script>");
                return;
            }
            if (FileUpload1.PostedFile.ContentLength> 6 * 1024 * 1024)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传文件大小不能超过4MB,请重新上传!')</script>");
                return;
            }
            string id = Session["UnitID"].ToString();
            string filePath = Server.MapPath("~/Upload/UnitAccessory");
            string filename = FileUpload1.FileName;
            if (filename.Length > 45)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传文件名称不能长于40个字符,请重命名后再上传!')</script>");
                return;
            }
            string file = filePath + "\\" + id + filename;
            if (File.Exists(file))
            {
                FileAttributes attrs = File.GetAttributes(file);
                attrs = (FileAttributes)((int)attrs & ~(1));
                File.SetAttributes(file, attrs);
            }
            UnitAccessoryManage accessManage = new UnitAccessoryManage();
            UnitAccessory access;
            try
            {    
                FileUpload1.SaveAs(file);
                if (accessManage.Exists(id))
                {
                    access = accessManage.GetModel(id);
                    access.LegalLicense = filename;
                    accessManage.Update(access);
                }
                else
                {
                    access = new UnitAccessory();
                    access.UnitID = id;
                    access.LegalLicense = filename;
                    accessManage.Add(access);
                }
                Label1.Text = filename;
                ButtonDelete1.Visible = true;
                ButtonDown1.Visible = true;
                FileUpload1.Enabled = false;
                ButtonUpload1.Enabled = false;
                //修改HiddenFieldfilename
                addFileName(filename);
            }
            catch (Exception ex)
            {
                Label1.Text = "出错:" + ex.Message.ToString();
            }
        }
    }

转载于:https://www.cnblogs.com/particle/archive/2012/05/31/2482346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值