• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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();
            }
        }
    }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net应用程序错误解决方案发布时间:2022-07-10
下一篇:
ASP.NETMVCFrameworkActionFilters发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap