c# - Format data in the UpdatePanel before save -


i'm using updatepanel in asp.net webforms upload image. want save filename in database, not path. i'm using following code

 <asp:templatefield footertext="image 1" headertext="image 1">     <itemtemplate>        <asp:image id="image11" runat="server" imageurl='<%# bind("pic") %>' height="50px" onerror="this.style.display='none'" />     </itemtemplate>     <edititemtemplate>        <asp:updatepanel id="updatpanelimg1" updatemode="conditional" runat="server">           <triggers>              <asp:postbacktrigger controlid="uploadbutton" />           </triggers>           <contenttemplate>              <asp:fileupload id="fileupload1" runat="server" />              <asp:button runat="server" text="add image" onclick="uploadbuttonclick" />              <asp:image id="image1" runat="server" imageurl='<%# bind("pic") %>' onerror="this.style.display='none'" />            </contenttemplate>         </asp:updatepanel>       </edititemtemplate>     </asp:templatefield> 

and in uploadbuttonclick method, set path image width image1.imageurl = virtualfilepath; virtualfilepath full virtual path file. when data saved, database contains full path file, whant file name.

edit 1: code uploadbuttonclick:

    protected void uploadbuttonclick(object sender, eventargs e)     {         var button = (button)sender;         var fileupload = (fileupload)button.parent.findcontrol("fileupload1");         var imageviewer = (image)button.parent.findcontrol("image1");          if (fileupload.hasfile)         {             string filename = fileupload.filename;             filename = removeinvalidchars(filename);             const string virtualpath = @"/images/news/";             string serverpath = server.mappath(virtualpath);             string serverfilepath = path.combine(serverpath, filename);             string virtualfilepath = path.combine(virtualpath, filename);              fileupload.saveas(serverfilepath);              imageviewer.imageurl = virtualfilepath;         }     } 

edit 2: code save database

<asp:sqldatasource    id="sqldatasourcenews"    runat="server"    connectionstring="<%$ connectionstrings:connectionstring %>"    updatecommand="update [foo] set [pic] = @pic [id] = @id">    <updateparameters>       <asp:parameter name="pic" type="string" />    </updateparameters>    <insertparameters>       <asp:parameter name="pic" type="string" />     </insertparameters>  </asp:sqldatasource> 

you binding imageurl attribute in itemtemplate (in gridview or formview probably?).

<edititemtemplate>   <asp:updatepanel...>     ...     <contenttemplate>       ..       <asp:image id="image1" runat="server" imageurl='<%# bind("pic") %>' />     </contenttemplate>   </asp:updatepanel> </edititemtemplate> 

so when update, value bound <asp:parameter name="pic" /> in sqldatasource control. in code behind file set value complete virtual path (because want image displayed correctly).

var imageviewer = (image)button.parent.findcontrol("image1"); ... imageviewer.imageurl = virtualfilepath; 

so why complete virtual path saved database. fix must bind parameter value hidden control, , use eval image control:

<asp:hidden id="imagefilename" runat="server" value='<%# bind("pic") %>' /> <asp:image ... imageurl='<%# eval("pic", "~/images/news/{0}") %>' 

in code behind, set value hidden control filename:

var imageviewer = (image)button.parent.findcontrol("image1"); var imagefilename = (hidden)button.parent.findcontrol("imagefilename"); ... imageviewer.imageurl = virtualfilepath; imagefilename.imageurl = filename; 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -