vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, May 31, 2012

How to use fileupload control inside update panel in gridview or listview?


Here i discuss the issues i come across recently, if you are using fileupload control without update panel then its working
Fine, but if you use update panel then fileupload control has loss the selected file when asynchronous postback.
Solution for this problem has we have to trigger the control when asynchronous postback occurs.
These are the following solution to overcome the issues.
File upload control without using gridview
<table width="50%" cellpadding="2" cellspacing="0">
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload_Click" /><br />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
</Triggers>

</
asp:UpdatePanel>
</td>
</tr>
</table>


Using gridview
When without using gridview we directly give the button control id to the trigger.
Suppose if you are using gridview, simple solution is then give the grid view control id as postbacktrigger, then it will find the button control..
<Triggers>
                        <asp:PostBackTrigger ControlID="GridView1" />
                </Triggers>

Or
You need to add trigger dynamically in gridview.After binding the data you have to add below code..


GridView1.DataSource = lst;
           GridView1.DataBind();         
 PostBackTrigger posttrg = new PostBackTrigger();
           posttrg.ControlID = this.GridView1.FooterRow.FindControl("btnUpload").UniqueID;
           this.UpdatePanel1.Triggers.Add(posttrg);

Or
Write the code in prerender event
protected override void OnPreRender(EventArgs e)
         {
             PostBackTrigger posttrg = new PostBackTrigger();
             posttrg.ControlID = this.GridView1.FooterRow.FindControl("btnUpload").UniqueID;
             this.UpdatePanel1.Triggers.Add(posttrg);
             base.OnPreRender(e);
         }

Using Listview Control:
  posttrg.ControlID = ListView1.InsertItem.FindControl("btnUpdate").UniqueID
        posttrg.ControlID = ListView1.EditItem.FindControl("btnUpdate").UniqueID
        Me.UpdatePanel1.Triggers.Add(posttrg)

I hope it will be useful for someone ……Happy coding and keep reading

0 comments:

Post a Comment