如何在Gridview中使用文件上传控件 [英] how to use file upload control in Gridview

查看:85
本文介绍了如何在Gridview中使用文件上传控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在gridview中上传文件。但当我使用代码上传时,它无法找到文件上传控件。

错误:

编译器错误消息:CS0103:名称'flupslip'不存在于当前背景

aspx文件:

 <   asp:GridView     ID   =  grdempdetails    runat   =  server    AutoGenerateColumns   =  False  

< span class =code-attribute> CellPadding = 4 PageSize = 20 ShowFooter = true >
< AlternatingRowStyle BackCol或 = blue / >
< >
< asp:TemplateField HeaderText < span class =code-keyword> = Sr.No >
< ItemTemplate >
<% #Container.DataItemIndex + 1 %>
< / Item模板 >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 员工ID >
< ItemTemplate >
< asp:标签 ID = lblEmpID runat = 服务器 文本 =' <% #Bind( MA_App_Code%> ' > < / asp:标签 >
< / ItemTemplate >
< FooterTemplate >
< asp:TextBox ID = txtEmpID runat = server > < / asp:TextBox >
< / FooterTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 员工姓名 >
< ItemTemplate < span class =code-keyword>>
< asp:Label ID = lblEmpName runat = server Text =' <% #Bind(< span class =code-string> 名称%> ' > < / asp:标签 >
< / ItemTemplate >
< < span class =code-leadattribute> FooterTemplate >
< asp:TextBox ID = txtname runat = 服务器 > ; < / asp:TextBox > ;
< / FooterTemplate >
< / asp:TemplateField >

< ; asp:TemplateField HeaderText = CheckBox >
< ItemTemplate >
< asp:CheckBox ID = chkbox runat = server / >
< ; / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 上传 > ;
< ItemTemplate >
< asp:FileUpload ID = flupslip runat = server / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:Gri dView >





并上传按钮代码



  public   string  uploadslip()
{
string filename = ;
bool fileOK = false ;
string fileExtension = null ;
if (flupslip.Hasfile)
{
fileExtension = System.IO.Path.GetExtension(flupslip.FileName).ToLower( );
string [] allowedExtensions = { 。 jpg .jpeg 。png 。gif 。bmp 。pdf 。docx };
for int i = 0 ; i < allowedExtensions.Length; i ++)
{
if (fileExtension == allowedExtensions [i])
{
fileOK = true ;
}
}
}
如果(fileOK)
{
尝试
{
common cm = new common();
int no = cm.DocName();
string Newsfile = flupslip.FileName;
string [] str = Newsfile.Split(' )。
string s = str [ 0 ]。ToString();
string ext = str [ 1 ]。ToString();
filename = txtEmpID.Text + _ + drdmonth.SelectedValue.ToString()。 Trim()+ fileExtension;

FileInfo files1 = new FileInfo(Server.MapPath( .. \\Images\\Emp Docs \\)+ filename);
if (files1.Exists)
{
string delfile;
string [] files = System.IO.Directory.GetFiles(Server.MapPath( .. \\Images\\Emp Docs \\));
foreach 字符串文件 in 文件)
{
delfile = Server.MapPath( .. \\Images \\Emp Docs \\ + filename);
if (file == delfile)
{
System.IO.File.Delete(file);
}
}
}
flupslip.PostedFile.SaveAs(Server.MapPath( .. \\Images\\Emp Docs \\ + filename));
}
catch (例外情况)
{
}
最后
{
}
return filename;
}
其他
{
.ClientScript .RegisterStartupScript(GetType(), AlertCode alert('无效的文件格式\\ n支持的格式:jpg,jpeg,png,gif,pdf,bmp'););
return ;
}
}

解决方案

您的文件上传控件位于gridview内部。如果你直接调用fileupload没有任何意义,首先你需要调用gridview控件然后根据gridview id你需要调用该文件上传控件。



参考以下链接可能对您有所帮助。 如何在网格视图中管理文件上传控件 [ ^ ]

Hi, i am trying to upload file in gridview. but when i use code for upload then it unable to find file upload control.
Error:
Compiler Error Message: CS0103: The name 'flupslip' does not exist in the current context
aspx file:

<asp:GridView ID="grdempdetails" runat="server" AutoGenerateColumns="False"

            CellPadding="4" PageSize="20" ShowFooter="true">
        <AlternatingRowStyle BackColor="blue" />
        <Columns>
        <asp:TemplateField HeaderText="Sr.No">
        <ItemTemplate>
         <%#Container.DataItemIndex+1%>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee ID">
        <ItemTemplate>
            <asp:Label ID="lblEmpID" runat="server" Text='<%#Bind("MA_App_Code") %>'></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
        <asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
        </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee Name">
        <ItemTemplate>
            <asp:Label ID="lblEmpName" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
        </FooterTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="CheckBox">
        <ItemTemplate>
            <asp:CheckBox ID="chkbox" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Upload">
        <ItemTemplate>
            <asp:FileUpload ID="flupslip" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>



And upload button code

public string uploadslip()
    {
        string filename = "";
        bool fileOK = false;
        string fileExtension = null;
        if (flupslip.Hasfile)
        {
            fileExtension = System.IO.Path.GetExtension(flupslip.FileName).ToLower();
            string[] allowedExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".pdf", ".docx" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                common cm = new common();
                int no = cm.DocName();
                string Newsfile = flupslip.FileName;
                string[] str = Newsfile.Split('.');
                string s = str[0].ToString();
                string ext = str[1].ToString();
                filename = txtEmpID.Text + "_" + drdmonth.SelectedValue.ToString().Trim()  + fileExtension;

                FileInfo files1 = new FileInfo(Server.MapPath("..\\Images\\Emp Docs\\") + filename);
                if (files1.Exists)
                {
                    string delfile;
                    string[] files = System.IO.Directory.GetFiles(Server.MapPath("..\\Images\\Emp Docs\\"));
                    foreach (string file in files)
                    {
                        delfile = Server.MapPath("..\\Images\\Emp Docs\\" + filename);
                        if (file == delfile)
                        {
                            System.IO.File.Delete(file);
                        }
                    }
                }
                flupslip.PostedFile.SaveAs(Server.MapPath("..\\Images\\Emp Docs\\" + filename));
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
            return filename;
        }
        else
        {
            this.ClientScript.RegisterStartupScript(GetType(), "AlertCode", "alert('Invalid file format\\n Supported Formats : jpg, jpeg, png, gif,pdf, bmp');", true);
            return "";
        }
    }

解决方案

Your fileupload control is located in inside gridview. If you are directly calling fileupload there is no meaning, first off all you need to call gridview control then based on gridview Id you need to call that fileupload control.

Refer below link this might be helpful to you. how to manage file upload control inside grid view[^]


这篇关于如何在Gridview中使用文件上传控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆