Monday, 19 August 2013

Asp Updatepanel not working as expected

Asp Updatepanel not working as expected

here is what I am up with.. I am uploading images through FileUpload
Control.. But when I give trigger to updatepanel The Fileupload control
shows that it is empty.. I dont know y..
n here is what I need..
I need to updaate my updatepanel only when I click upload button and other
then that I don wana update its contents.. What changes I need to do in my
coding
Here is the aspx code..
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true"/>
<div>
<asp:Panel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<fieldset style="width:50%; margin-left:300px">
<legend>Upload Files</legend>
<asp:FileUpload runat="server" ID="UploadImages"
style="background-color:white; position:relative;
font-family:'Palatino Linotype'; font-size:medium"
Width="500px" AllowMultiple="true"/>
<asp:Button runat="server" ID="uploadedFile"
style="position:relative; font-family:'Palatino
Linotype'; font-size:medium; width: 112px; height: 29px;"
Text="Upload" OnClick="uploadFile_Click" />
</fieldset>
</asp:Panel>
<div id="back" runat="server" class="transbox"
style="width:100%;height:100%;left:0px;top:0px;position:absolute"
Visible="false">
<asp:UpdatePanel ID="updtpanel" runat="server"
UpdateMode="Conditional"
style="width:100%;height:100%;left:0px;top:0px;position:absolute">
<ContentTemplate>
<asp:Button ID="btnsave" runat="server"
UseSubmitBehavior="true" Text="Find Images"
OnClick="btnsave_Click" Font-Bold="true"
style="position:absolute"
BackColor="Yellow"></asp:Button>--%>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="uploadedFile"
EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
</div>
</div>
</form>
and here is the .cs Code for FileUpload..
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
string fileExt =
Path.GetExtension(UploadImages.FileName).ToLower();
if (fileExt == ".jpeg" || fileExt == ".png" || fileExt ==
".jpg" || fileExt == ".bmp")
{
foreach (HttpPostedFile uploadedFile in
UploadImages.PostedFiles)
{
count1 += 1;
}
foreach(HttpPostedFile uploadedFile in
UploadImages.PostedFiles)
{
count += 1;
filepath = Server.MapPath("~/Images/" +
uploadedFile.FileName);
uploadedFile.SaveAs(filepath);
newpath = "../Images/" + uploadedFile.FileName;
try
{
createImgPanel();
Image nimg = Page.FindControl("img" + count) as
Image;
nimg.ImageUrl = newpath.ToString();
Page.ClientScript.RegisterStartupScript(GetType(),
"msgbox", "alert('Files Uploaded!!');", true);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(),
"msgbox", "alert('Please Select only Image Files!!');",
true);
}
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "msgbox",
"alert('Please Select a File First!!');", true);
}
}
If I comment the Triggers in updatepanel uploadFile.HasImages will work..
But if I uncomment it It will directly land on Please Select a file first
error msg.. What mistake am committing in the code..

No comments:

Post a Comment