0

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>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
Zoya
  • 405
  • 3
  • 10
  • 21

2 Answers2

3

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";
        }
Pasha Immortals
  • 829
  • 1
  • 7
  • 19
1

FileUpload need a full postback to work.

  • try putting GridView inside UpdatePanel.
  • Set OnRowCommand="GridView1_RowCommand" event of gridview
  • set the commandname to the Button CommandName="upload"

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
       <Triggers>
         <asp:PostBackTrigger ControlID="btnupload" />
       </Triggers>
       <ContentTemplate>
          <asp:GridView ID="GridView1" Runat="server" OnRowCommand="GridView1_RowCommand">
          <Columns>
            <asp:TemplateField HeaderText="Upload Kundli">
             <ItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                <asp:Button ID="btnupload" runat="server" Text="Upload" CommandName="upload" />
             </ItemTemplate>
           </asp:TemplateField>
      </Columns>
    

Now get the Button even via rowcommand event.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "upload")
    {
       //your stuff
    }
}
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56