Fileuploader in gridview in asp.net c# -
default.aspx
<asp:gridview id="gridview1" visible="false" onpageindexchanging="gridview1_pageindexchanging" onrowupdating="gridview1_rowupdating" onrowediting="gridview1_rowediting" onrowcancelingedit="gridview1_rowcancelingedit" runat="server" horizontalalign="center" allowpaging="true" autogeneratecolumns="false" datakeynames="q_d" forecolor="#333333" cellspacing="0" cellpadding="4" width="100%" gridlines="none"> <columns> <asp:templatefield itemstyle-horizontalalign="center"> <headertemplate> <asp:checkbox runat="server" id="lqcheckall" autopostback="true" oncheckedchanged="lqcheckall_checkedchanged" /> <asp:imagebutton imageurl="~/icons/remove-icon-png-25.png" onclick="lqdelete_click" width="20px" height="25px" runat="server" id="lqdelete" /> </headertemplate> <itemtemplate> <asp:checkbox runat="server" id="lqcheck" /> <asp:button id="lqbtn_edit" runat="server" text="edit" commandname="edit" /> </itemtemplate> <edititemtemplate> <asp:button id="lqbtn_update" runat="server" text="update" commandname="update" /> <asp:button id="lqbtn_cancel" runat="server" text="cancel" commandname="cancel" /> </edititemtemplate> <itemstyle horizontalalign="center"></itemstyle> </asp:templatefield> <asp:templatefield headertext="question" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lqlbl_name" runat="server" text='<%#eval("question") %>'></asp:label> <asp:image id="lqimg" runat="server" imageurl='<%#eval("q_image") %>' width="80px" height="50px" alternatetext=" " imagealign="middle" /> </itemtemplate> <edititemtemplate> <asp:textbox id="lqtxt_name" cssclass="input" runat="server" text='<%#eval("question") %>'></asp:textbox> <div class="custom_file_upload"> <div class="file_upload"> <asp:fileupload accept=".jpeg,.png,.jpg" id="f0" allowmultiple="false" runat="server" viewstatemode="inherit" enableviewstate="true" /> </div> </div> <asp:textbox runat="server" id="lqtq" visible="false" text='<%#eval("q_image") %>'></asp:textbox> </edititemtemplate> <headerstyle horizontalalign="center"></headerstyle> <itemstyle horizontalalign="center"></itemstyle> </asp:templatefield> <asp:templatefield headertext="correct" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center"> <itemtemplate> <asp:label id="lqlbl_city" runat="server" text='<%#eval("correct") %>'></asp:label> <asp:image id="lqcimg" runat="server" imageurl='<%#eval("c_img") %>' width="80px" height="50px" alternatetext=" " imagealign="middle" /> </itemtemplate> <edititemtemplate> <asp:textbox id="lqtxt_city" cssclass="input" runat="server" text='<%#eval("correct") %>'></asp:textbox> <div class="custom_file_upload"> <div class="file_upload"> <asp:fileupload accept=".jpeg,.png,.jpg" id="f1" allowmultiple="false" runat="server" viewstatemode="inherit" accesskey="f" enableviewstate="true" /> </div> </div> <asp:textbox runat="server" id="lqtc" visible="false" text='<%#eval("c_img") %>'></asp:textbox> </edititemtemplate> <headerstyle horizontalalign="center"></headerstyle> <itemstyle horizontalalign="center"></itemstyle> </asp:templatefield> <asp:templatefield headertext="q_id" visible="false"> <itemtemplate> <asp:label id="lq_id" runat="server" text='<%#eval("q_d") %>'></asp:label> </itemtemplate> </asp:templatefield> </columns> <headerstyle backcolor="darkcyan" forecolor="#ffffff" /> <pagersettings mode="numericfirstlast" /> <rowstyle backcolor="#eeeeee" /> </asp:gridview>
this code when put in update panel fileupload got empty. there way stop page refresh on autopost without update panel because fileupload not compatible update panel? searched on internet not satisfied found solutions.
code-behind:
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow && e.row.rowstate == datacontrolrowstate.edit) { // finds file upload controls inside gridview fileupload f0 = e.row.findcontrol("f0") fileupload; fileupload f1 = e.row.findcontrol("f1") fileupload; // registering controls post scriptmanager.getcurrent(this).registerpostbackcontrol(f0); scriptmanager.getcurrent(this).registerpostbackcontrol(f1); } }
note: don't forget add onrowdatabound
event in grindview <asp:gridview id="gridview1" runat="server" onrowdatabound="gridview1_rowdatabound" >
.
edit:
if want register asyncpostback
try this:
scriptmanager.getcurrent(this).registerasyncpostbackcontrol(f0); scriptmanager.getcurrent(this).registerasyncpostbackcontrol(f1);
Comments
Post a Comment