c# - display images dynamically in repeater tag -
i have multiple images want display. image count varies on input. able see broken images on screen. in console err_invalid_url displayed. please let me know wrong.
below aspx code
<asp:repeater id="repeater1" runat="server"> <itemtemplate> <asp:image id="image3" runat="server" imageurl="data:image/jpg;base64,<%# ((view_data)container.dataitem).image%>" /> </itemtemplate> </asp:repeater>
cs code
foreach (datarow row in dt.rows) { byte[] bytes = (byte[])row["image"]; viewdatalist.add(new view_data { image = convert.tobase64string(bytes)}); } repeater1.datasource = viewdatalist; repeater1.databind();
i fetching images database.is correct way this.. kindly suggest
update..
i have changed image tag below. there 4 images need displayed out of able see 1 image , rest 3 broken.. kindly suggest
<img src="data:image/jpg;base64,<%# ((view_data)container.dataitem).image%>" />
try in way requirements. let me know, if have issues once completed below methods.
note: tried separate column name image id.if have in database of more useful proceed this. otherwise, pass image directly repeater , pass same imagehandler.ashx
html page:
<asp:repeater id="repeater1" runat="server"> <itemtemplate> <asp:image id="image3" runat="server" imageurl='<%# "imagehandler.ashx?imid="+ eval("imageid") %>' height="150px" width="150px"/> </itemtemplate>
cs code:
on click event:
sqlconnection connection = new sqlconnection(strcon); sqlcommand command = new sqlcommand("select imagename,imageid [image]",connection); sqldataadapter daimages = new sqldataadapter(command); datatable dt = new datatable(); daimages.fill(dt); repeater1.datasource = dt; repeater1.databind();
imagehandler.ashx
after completion of above code need add httphandler file our project retrieve images database because save our images in binary format getting binary format of data database it’s easy displaying difficult that’s why use httphandler solve problem.
here httphandler simple class allows process request , return response browser. can handler responsible fulfilling requests browser. can handle 1 request @ time, in turn gives high performance.
right click on project add new httphandler.ashx file , give name imagehandler.ashx , write following code in pagerequest method
string strcon =configurationmanager.appsettings["connectionstring"].tostring(); public void processrequest(httpcontext context) { string imageid = context.request.querystring["imid"]; sqlconnection connection = new sqlconnection(strcon); connection.open(); sqlcommand command = new sqlcommand("select image image imageid="+ imageid, connection); sqldatareader dr = command.executereader(); dr.read(); context.response.binarywrite((byte[])dr[0]); connection.close(); context.response.end(); }
here don’t forgot set connection string in web.config file here getting database connection web.config file reason need set connectionstring in web.config file this
<connectionstrings> <add name="dbconnection" connectionstring="datasource=yourservername;integrated security=true;initialcatalog=your databasename"/> </connectionstrings>
source: retrieve image database
Comments
Post a Comment