Search code examples
asp.netajaxgridviewupdatepanel

I am unable to use FileUpload which is in Gridview in the UpdatePanel, have also used trigger, how can i solve this?


I want to use UpdatePanel for my GridView, but the FileUpload in GridView doesn't work even if I added trigger... Because it cant find FileUpload Button, whats the solution?

<asp:TemplateField HeaderText="Upload Kundli">
  <ItemTemplate>
    <asp:FileUpload ID="FileUpload1" runat="server" /><br />
    <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center" />
  <HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>

</Columns>

</asp:GridView>

</ContentTemplate>
<Triggers>
  <asp:PostBackTrigger ControlID="btnupload" />
</Triggers>
</asp:UpdatePanel>

Solution

  • Here is a way I do it via code behind, its just dummy mockup to give you an idea:

    ASPX Code:

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">        
        </asp:ScriptManager>  
    
    <asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Label runat="server" ID="Label1" Text=""></asp:Label>    
    <asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
        <asp:TemplateField HeaderText="Upload Kundli">
                                    <ItemTemplate>
                                        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                                        <asp:Button ID="btnupload" runat="server" Text="Upload"       OnClick="btnupload_Click" />
                                    </ItemTemplate>
                                     <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
    
              </Columns>
    
        </asp:GridView>
    
        </ContentTemplate>
    
            </asp:UpdatePanel>
            </form>
    

    Code Behind (ASPX.CS):

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    List<int> gridItems = new List<int>();
                    gridItems.Add(1);
                    gridItems.Add(2);
                    GridView1.DataSource = gridItems;
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
            {
                //May not need this if. So check depending on what and how you are binding.
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Button UploadButon = (Button) e.Row.Cells[0].FindControl("btnupload");
                    if(UploadButon != null)
                    {
                        if (this.ScriptManager1 != null)
                        {
                            this.ScriptManager1.
                                RegisterAsyncPostBackControl(UploadButon);
                        }
                    }
    
                }
    
            }
            protected void btnupload_Click(object sender, EventArgs e)
            {
                Label1.Text = Label1.Text + "a";
            }