c# - Error in finding dynamic created control -
i uploading images using fileupload control , after loading file when click upload button file saved in folder in server, before saving database need add image description along image.. dont know getting error. able save file in folder when save database 1 dynamic added control wil found , give object reference not set instance of object though there more 1 controls in particular div. before going code tell controls adding after upload file.. adding 1 image control , textbox each of image files uploaded.. when upload 1 file go foreach loop once again after 1st time.. code might explain more.
so .aspx code:
<form id="contentplaceholder1" runat="server"> <div class="transbox" id="mainbk" runat="server" style="position:absolute; top:0px; left:0px; width: 100%; height: 100%;" > <asp:fileupload runat="server" id="uploadimages" style="background-color:white; position:absolute; font-family:'palatino linotype'; font-size:medium; top: 4px; left: 350px; right: 251px;" width="500px" allowmultiple="true"/> <asp:button runat="server" id="uploadedfile" style="position:absolute; font-family:'palatino linotype'; font-size:medium; top: 4px; left: 870px; width: 112px; height: 29px;" text="upload" onclick="uploadfile_click" /> <asp:panel id="updtpanel" runat="server" cssclass="transbox" style="width:100%;height:100%;left:0px;top:0px;position:absolute" visible="false"> <asp:button id="btnsave" runat="server" usesubmitbehavior="true" text="save" onclick="btnsave_click" font-bold="true" backcolor="yellow"></asp:button> </asp:panel> </div> </form>
and backend code.
sqlcommand com = new sqlcommand(); sqlconnection con = new sqlconnection(); sqldatareader reader; int id = 0; stringbuilder sb = new stringbuilder(); string filepath = ""; string newpath = ""; int tid = 0; int count = 0; int cnt1 = 0; string textid = ""; panel dload; image img; textbox ta; protected void page_load(object sender, eventargs e) { con.connectionstring = configurationmanager.connectionstrings["connectionstring"].connectionstring; con.open(); com = new sqlcommand("select max(slid) slider", con); reader = com.executereader(); if (reader.hasrows) { while (reader.read()) { id = convert.toint32(reader.getint32(0)); } } con.close(); com.dispose(); htmlgenericcontrol dh = new htmlgenericcontrol("div"); dh.attributes.add("class", "head"); dh.innertext = "write description"; updtpanel.controls.add(dh); foreach (httppostedfile upld in uploadimages.postedfiles) { createimgpanel(); } } 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) { count += 1; filepath = server.mappath("~/images/gallery/" + uploadedfile.filename); uploadedfile.saveas(filepath); newpath = "../images/gallery/" + uploadedfile.filename; try { image nimg = dload.findcontrol("img" + count) image; nimg.imageurl = newpath.tostring(); } 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); } } public void createimgpanel() { tid = tid + 1; textid = "txt" + tid; ta = new textbox(); img = new image(); ta.textmode = textboxmode.multiline; dload = new panel(); updtpanel.visible = true; dload.attributes.add("class", "dataload"); //dload.attributes.add("runat", "server"); dload.id = "ind" + tid; img.cssclass = "loadimg"; img.id = "img" + tid; //img.attributes.add("runat", "server"); ta.attributes.add("class", "txtdes"); ta.id = textid; //ta.attributes.add("runat", "server"); dload.controls.add(img); dload.controls.add(ta); updtpanel.controls.add(dload); } protected void btnsave_click(object sender, eventargs e) { if (page.ispostback) { panel pv = (this.form.findcontrol("mainbk").findcontrol("updtpanel")) panel; foreach (control cd in pv.controls) { cnt1 = cnt1 + 1; textbox txt = cd.findcontrol("ind" + cnt1).findcontrol("txt" + cnt1) textbox;****this getting above said error**** image img = cd.findcontrol("ind" + cnt1).findcontrol("img" + cnt1) image; string str = ""; str = txt.text; string iurl = ""; iurl = img.imageurl; id += 1; string insert = "insert slider (slid,slurl,slalt) values (@id,@image_path,@alter)"; sqlcommand cmd = new sqlcommand(insert, con); cmd.parameters.addwithvalue("@image_path", iurl); cmd.parameters.addwithvalue("@id", id); cmd.parameters.addwithvalue("@alter", str); try { con.open(); cmd.executenonquery(); con.close(); } catch (exception e1) { response.write(e1.message); } } updtpanel.visible = false; } }
*i getting error in last part of above code i.e. save button click event. textbox find control getting error. first image saved other images not finding control if present in design.*
i want know whether committing mistakes in save click event or there mistake in whole coding itself
edit
so html source after controls rendered in runtime:
<div id="mainbk" class="transbox" style="position:absolute; top:0px; left:0px; width: 100%; height: 100%;"> <input type="file" multiple="multiple" name="uploadimages" id="uploadimages" style="width:500px;background-color:white; position:absolute; font-family:'palatino linotype'; font-size:medium; top: 4px; left: 350px; right: 251px;"> <input type="submit" name="uploadedfile" value="upload" id="uploadedfile" style="position:absolute; font-family:'palatino linotype'; font-size:medium; top: 4px; left: 870px; width: 112px; height: 29px;"> <div id="updtpanel" class="transbox" style="width:100%;height:100%;left:0px;top:0px;position:absolute"> <input type="submit" name="btnsave" value="save" id="btnsave" style="background-color:yellow;font-weight:bold;"> <div class="head">write description</div><div id="ind1" class="dataload"> <img id="img1" class="loadimg" runat="server" src="../images/gallery/god%201.jpg"> <textarea name="txt1" rows="2" cols="20" id="txt1" class="txtdes"></textarea> </div><div id="ind2" class="dataload"> <img id="img2" class="loadimg" runat="server" src="../images/gallery/god%202.jpg"> <textarea name="txt2" rows="2" cols="20" id="txt2" class="txtdes"></textarea> </div><div id="ind3" class="dataload"> <img id="img3" class="loadimg" runat="server" src="../images/gallery/god%203.jpg"> <textarea name="txt3" rows="2" cols="20" id="txt3" class="txtdes"></textarea> </div> </div> </div>
edited
k went deeper exception getting... saying..
'((system.web.ui.htmlcontrols.htmlcontainercontrol)(dv)).innerhtml' threw exception of type 'system.web.httpexception'
what mean.. says control not literal.. mean.. why not able find control have appended.. me out please..
add controls in prerender event, access them @ least pageload.
Comments
Post a Comment