Saturday, March 24, 2012

Uploading a file in a updatepanel

Basically, I am trying to do an upload inside the updatepanel. (Ajax, C#, Visual Studio 2005)

I don't think it's possible to do it without full page post back.. (If I am wrong, please let me know how!!)

So, I decided to put a not-visible button in master.master page. The code follows below.

<%@dotnet.itags.org. Master Language="C#"
AutoEventWireup="true"
CodeFile=" Master.master.cs"
Inherits="Master" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<!--General Stylesheet-->
<link rel="stylesheet" href="http://links.10026.com/?link=css/gen_style.css" type="text/css" />
</head
<body style="border:0px; margin:0px;"
<form id="submitTicketForm" method="post" runat="server" enctype="multipart/form-data">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="MainLoginPanel" runat="server">
<ContentTemplate
<!--Top Logo-->
<div class="headerTop">
<div id="header">
<div class="logo"></div>
<div class="login">
<asp:Panel runat="server" ID="LogOutPanel">
<asp:Button OnClick="LogOut_Click" runat="server" ID="LogOut" Text="Click here to logout" Width="142" CssClass="myBtn" />
</asp:Panel>
</div>
</div>
</div
<!--Tab_Panel-->
<asp:Panel runat="server" ID="Tab_Panel">
<div class="tabTop">
...
</div>
</asp:Panel>
<div class="contentplaceholder">
<asp:contentplaceholder id="mainContent" runat="Server"></asp:contentplaceholder>
</div>
<asp:Button id="UploadButton" Visible="false" runat="server" Text="Submit" CssClass="myBtn" OnClick="sendData" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="UploadButton" />
</Triggers>
</asp:UpdatePanel
<!--Page Footer-->
<div class="footer">Footer</div>
</form>
</body>
</html
Then the below is the code for NewTicket.aspx. If I press the btn_sendData button, I need to trigger the UploadButton button in the master's page.

<%@dotnet.itags.org. Page Language="C#"
MasterPageFile="~/Master.master"
AutoEventWireup="true"
CodeFile="NewTicket.aspx.cs"
Inherits="NewTicket"
Title="SPN Ticketing System - Submit a New Ticket" %
<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit"
TagPrefix="ajaxToolkit" %
<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" Runat="Server">
<asp:FileUpload ID="fileChoose" runat="server" />
<asp:Button id="btn_sendData" runat="server" Text="Submit" CssClass="myBtn" OnClick="TriggerMasterUploadBtn" />
</asp:Content
Now, the below is the partial code from NewTicket.aspx.cs.

protected void TriggerMasterUploadBtn(object sender, EventArgs e)
{

Control btnControl = Page.Master.FindControl ( "UploadButton" );
Button btn = (Button)btnControl;
//I have the control but I don't know what to do!
}

I've been pulling my hair for hours for this thing.
Your help is GREATELY appreciated!
Thank you again.

It is not possible to do a file upload without doing a full post back. If you have the fileupload inside the update panel you will need to add a trigger to the button that will take care of the uploading. There have been many posts on this forum about this, and some have some good work arounds.

http://forums.asp.net/thread/1444713.aspx

There is alink on one of the threads that is to an MSDN page that listed all the controls that currently do not work with update panel but I can't seem to find it. If anyone does, please post it here.

No comments:

Post a Comment