ASP.NETプロジェクト申告システム-5.3~5.4(FileUploadとGridViewの使い方)

31861 ワード

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

            }

        }

    }