asp.net c中gridview中的Fileuploader

时间:2017-08-20 09:09:45

标签: c# asp.net gridview file-upload

default.aspx

<asp:GridView ID="GridView1" Visible="false" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" runat="server" HorizontalAlign="Center" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Q_d" ForeColor="#333333" CellSpacing="0" CellPadding="4" Width="100%" GridLines="None">

    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center">

            <HeaderTemplate>

                <asp:CheckBox runat="server" ID="lqcheckall" AutoPostBack="True" OnCheckedChanged="lqcheckall_CheckedChanged" />

                <asp:ImageButton ImageUrl="~/icons/remove-icon-png-25.png" OnClick="lqdelete_Click" Width="20px" Height="25px" runat="server" ID="lqdelete" />

            </HeaderTemplate>
            <ItemTemplate>


                <asp:CheckBox runat="server" ID="lqcheck" />

                <asp:Button ID="lqbtn_Edit" runat="server" Text="Edit" CommandName="Edit" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="lqbtn_Update" runat="server" Text="Update" CommandName="update" />
                <asp:Button ID="lqbtn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" />
            </EditItemTemplate>

            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>



        <asp:TemplateField HeaderText="Question" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lqlbl_Name" runat="server" Text='<%#Eval("question") %>'></asp:Label>
                <asp:Image ID="lqimg" runat="server" ImageUrl='<%#Eval("q_image") %>' Width="80px" Height="50px" AlternateText=" " ImageAlign="Middle" />

            </ItemTemplate>
            <EditItemTemplate>

                <asp:TextBox ID="lqtxt_Name" CssClass="input" runat="server" Text='<%#Eval("question") %>'></asp:TextBox>


                <div class="custom_file_upload">

                    <div class="file_upload">
                        <asp:FileUpload accept=".jpeg,.png,.jpg" ID="f0" AllowMultiple="false" runat="server" ViewStateMode="Inherit" EnableViewState="True" />
                    </div>
                </div>
                <asp:TextBox runat="server" ID="lqtq" Visible="false" Text='<%#Eval("q_image") %>'></asp:TextBox>
            </EditItemTemplate>

            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>

            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Correct" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label ID="lqlbl_City" runat="server" Text='<%#Eval("correct") %>'></asp:Label>
                <asp:Image ID="lqcimg" runat="server" ImageUrl='<%#Eval("c_img") %>' Width="80px" Height="50px" AlternateText=" " ImageAlign="Middle" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="lqtxt_City" CssClass="input" runat="server" Text='<%#Eval("correct") %>'></asp:TextBox>

                <div class="custom_file_upload">

                    <div class="file_upload">
                        <asp:FileUpload accept=".jpeg,.png,.jpg" ID="f1" AllowMultiple="false" runat="server" ViewStateMode="Inherit" AccessKey="f" EnableViewState="True" />

                    </div>
                </div>

                <asp:TextBox runat="server" ID="lqtc" Visible="false" Text='<%#Eval("c_img") %>'></asp:TextBox>

            </EditItemTemplate>

            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>

            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Q_id" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lq_id" runat="server" Text='<%#Eval("Q_d") %>'></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>

    </Columns>
    <HeaderStyle BackColor="DarkCyan" ForeColor="#ffffff" />
    <PagerSettings Mode="NumericFirstLast" />
    <RowStyle BackColor="#EEEEEE" />

</asp:GridView>

这是我的代码但是当我把它放在更新面板中时,fileupload变空了。有没有办法在没有更新面板的情况下在autopost上停止页面刷新,因为fileupload与更新面板不兼容?我在互联网上搜索但对找到的解决方案不满意。

1 个答案:

答案 0 :(得分:0)

<强>代码隐藏:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
{
   if (e.Row.RowType == DataControlRowType.DataRow
             && e.Row.RowState == DataControlRowState.Edit)
   {
        // finds all file upload controls inside gridview
        FileUpload f0 = e.Row.FindControl("f0") as FileUpload;
        FileUpload f1 = e.Row.FindControl("f1") as FileUpload;

        // registering the controls to post back
        ScriptManager.GetCurrent(this).RegisterPostBackControl(f0);
        ScriptManager.GetCurrent(this).RegisterPostBackControl(f1);  
   }
}

注意:不要忘记在GrindView OnRowDataBound中添加<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >个事件。

编辑:

如果您想将其注册为AsyncPostBack,请尝试以下操作:

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(f0);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(f1);