File Upload in Gridview with Ajax Modalpopup

Me_Himanshu
Posted by in ASP.NET AJAX category on for Beginner level | Points: 250 | Views : 59200 red flag
Rating: 4 out of 5  
 3 vote(s)

In this article,I will use fileupload control in ajax modal popup to upload file in gridview and the update it.


 Download source code for File Upload in Gridview with Ajax Modalpopup

Introduction

This article explains using file upload control in Ajax modalpopup extender to upload files in gridview

fileupload in grid with modal popup

Using the code

Take a gridview and bind it with your table and display it on the page.additionally add a link  upload file -

upload link in grid
In .aspx source code

<head id="Head1" runat="server">

<title>File Upload in Gridview with Ajax Modalpopup</title>

<style type="text/css">

.modalBackground

{

background-color: #C0C0C0;

filter: alpha(opacity=80);

opacity: 0.8;

z-index: 10000;

}

</style>

</head>

<body>

<form id="form1" runat="server">

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

</asp:ToolkitScriptManager>

<div>

<table width="100%" align="center">

<tr>

<td>

<asp:GridView ID="GridView1" DataKeyNames="ID" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="lnkupload" runat="server" Text="upload resume" OnClick="lnkupclick"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField HeaderText="Job ID" DataField="JobID" />

<asp:BoundField HeaderText="Name" DataField="Name" />

</Columns>

</asp:GridView>

<asp:Button ID="btnupe" runat="server" Text="Button" style="display:none;"/>

<asp:ModalPopupExtender ID="ModalPopupExtender1" BackgroundCssClass="modalBackground" runat="server" CancelControlID="btncancel1" TargetControlID="btnupe" PopupControlID="Panel1" >

</asp:ModalPopupExtender>

<asp:Panel ID="Panel1" runat="server" GroupingText="upload resume" BackColor="Orange" style="height:100px;">

<asp:HiddenField ID="HiddenField1" runat="server" Value="" />

<asp:FileUpload ID="flpup" runat="server" />

<asp:Button ID="btnfileupload" runat="server" Text="Upload resume" OnClick="resumeupload" />

<br />

<asp:Button ID="btncancel1" runat="server" Text="cancel" />

</asp:Panel>

</td>

</tr>

</table>

</div>

</form>

</body>

//Now in .cs file first bind your gridview with the table and display in the page

//then on link upload click write this code//

   

protected void lnkupclick(object sender, EventArgs e)

{

LinkButton lnk = sender as LinkButton;

GridViewRow gvrow = lnk.NamingContainer as GridViewRow;

int id = Int32.Parse(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());

HiddenField1.Value = id.ToString();

ModalPopupExtender1.Show();

}

//The above code will display modal popup control with file upload control in it.

//now on resume upload button click

protected void resumeupload(Object sender, EventArgs e)

{

int id = Int32.Parse(HiddenField1.Value);

if (flpup.HasFile)

{

string filename = Path.GetFileName(flpup.PostedFile.FileName);

flpup.SaveAs(Server.MapPath("~/files/" + filename));//create a folder named 'files' in the solution explorer,the files will be uploaded to this folder //

con.Open();

{

//your update command e.g :"update tablename set resumepath="'+filename+"' where id="+id;

}

}

}

Conclusion

By this,we can  use modal popup extender to upload files in gridview.
Page copy protected against web site content infringement by Copyscape

About the Author

Me_Himanshu
Full Name: Himanshu Pandey
Member Level: Starter
Member Status: Member
Member Since: 6/6/2012 2:28:47 AM
Country: India
Himanshu Pandey
not yet
I am a avid user if this website since i started my career ,always fantasize to post some useful articles into it.I am having 2.5 years of experience in Asp.Net,C#,Sql server,JavaScript and just started m working on WCF and Linq.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)