c# - Asp.NET Updatepanel not working as expected -
here with.. i uploading images through fileupload control.. when give trigger updatepanel fileupload control shows empty.. don't know why..
and here need..
i need update updatepanel when click upload button , other don't want update contents.. changes need in coding
here aspx code..
<form id="form1" runat="server"> <asp:scriptmanager id="scriptmanager1" runat="server" enablepartialrendering="true"/> <div> <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> <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 .cs code 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) 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 image files!!');", true); } } else { page.clientscript.registerstartupscript(gettype(), "msgbox", "alert('please select file first!!');", true); } }
if comment triggers in updatepanel uploadfile.hasimages
work.. if uncomment it directly land on please select file first error msg.. mistake committing in code..
your updatepanel
triggering postback right after uploadfile
control clicked happens file chosen postedback first request. time submit second request clicking on button, uploadfile
control empty again. uploadfile
control (or input:file
element, matter) won't retain selected value after postback. see design.
the easiest thing avoid initial postback , validation entirely on client side. in other words, remove triggers
section updatepanel since discovered works. obviously, still validation on server side when "upload files" button clicked.
there other alternatives bottom line cannot full postback (this updapanels behind scenes) when have uploadfile
control in page because cause files selected submitted. prove saying, put break point on page_load
, add watch request.files
when updatepanel triggers update.
Comments
Post a Comment