在网格视图中清除/重置文件上载控件

时间:2012-07-09 19:37:05

标签: javascript asp.net

我在asp.net网格视图中有多个文件上传控件,我想在选择错误文件时清除/重置文件上传控件。

fileupload1 btnReset1 fileupload2 btnReset2 ...

当我点击btnReset2然后只清除fileupload2的值

我如何使用java脚本执行此操作。

提前谢谢大家

我的代码如下。

JS代码: -

<script type="text/javascript">
        function hi(ob) {
            debugger;
            var grid = document.getElementById("<%= GridView1.ClientID %>");
            var inputs = grid.getElementsByTagName("input");
            var fileUpload;
            var strRowNo = ob.id.toString().split("_")[3];//get row number
            alert(strRowNo);
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].type == "file") {
                    fileUpload = inputs[i];
                    if (i == strRowNo) {
                        fileUpload.value = "";
                    }
                }
            }
        }
    </script>

//Aspx Code:-
  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="A" HeaderText="A" />
                    <asp:BoundField DataField="B" HeaderText="B" />
                    <asp:BoundField DataField="C" HeaderText="C" />
                    <asp:TemplateField HeaderText="File">
                        <ItemTemplate>
                            <div id="fuDiv" runat="server">
                                <asp:FileUpload ID="fupFile" runat="server" />
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Reset">
                        <ItemTemplate>
                            <asp:Image ID="ibtnReset" runat="server" onClick="javascript:hi(this);" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="A" HeaderText="A" />
                <asp:BoundField DataField="B" HeaderText="B" />
                <asp:BoundField DataField="C" HeaderText="C" />

                <asp:TemplateField HeaderText="File">
                    <ItemTemplate>
                        <div id="fuDiv" runat="server">
                        <asp:UpdatePanel ID="update_fupFile" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:FileUpload ID="fupFile" runat="server" />
                        </ContentTemplate>
                        <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="ibtnReset" EventName="Click" />
                        </Triggers>
                            </asp:UpdatePanel>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Reset">
                    <ItemTemplate>
                     <asp:UpdatePanel ID="update_fupFile" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                        <asp:Image ID="ibtnReset" runat="server" onClick="javascript:hi(this);" />
                        </ContentTemplate>
                       </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>