Saturday, 15 March 2014

asp.net - Hide button in gridview when field is Null -



asp.net - Hide button in gridview when field is Null -

i have grid view of rows have attached pictures. when press button2 pull info sql record, folder on server has pictures. works already, can not button visible in rows have image folder attached.

i've googled long time , found different solutions similar following, can not work. doing wrong?

<asp:sqldatasource id="sqldsodinrssfeb" runat="server" connectionstring="<%$ connectionstrings:herning_brand_dk_dbconnectionstring %>" selectcommand="select pubdatetime, melding, station, photofolder odinrss "> </asp:sqldatasource> <asp:gridview id="gridview14" runat="server" datasourceid="sqldsodinrssfeb" autogeneratecolumns="false" width="500px" onrowcommand="button_rowcommand" > <columns> <asp:boundfield datafield="pubdatetime" headertext="tidspunkt" /> <asp:boundfield datafield="melding" headertext="melding udkaldet" /> <asp:boundfield datafield="station" headertext="station" /> <asp:templatefield headertext="foto" > <edititemtemplate> <asp:textbox id="textbox1" runat="server" text='<%# bind("photofolder") %>'></asp:textbox> </edititemtemplate> <itemtemplate> <asp:button id="button2" runat="server" text="foto" visible='<%# eval("photofolder") != "null" %>' commandname="buttonclick" commandargument='<%# eval("photofolder") %>' /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>

my .cs

protected void button_rowcommand(object sender, gridviewcommandeventargs e) { if (e.commandargument != null) { switch (e.commandname) { case "buttonclick": { int folder = convert.toint32(e.commandargument); photolist(folder); } break; } } } void photolist(int foldernumber) { var imagepaths = directory.getfiles(server.mappath("photofolder\\" + foldernumber)); var imagenames = new string[imagepaths.length]; (int = 0; < imagepaths.length; i++) { imagenames[i] = imagepaths[i].substring(imagepaths[i].lastindexof("\\") + 1); } var dt = new datatable(); dt.columns.add("imagename", typeof(string)); dt.columns.add("imagepath", typeof(string)); foreach (var imgname in imagenames) { datarow dr = dt.newrow(); dr["imagename"] = removeextension(imgname); dr["imagepath"] = "photofolder/" + foldernumber + "/" + imgname; dt.rows.add(dr); } datalist1.datasource = dt; datalist1.databind(); } string removeextension(string imgname) { homecoming imgname .replace(".jpg", "") .replace(".png", ""); }

the sql field "photofolder" nvarchar(50). if there photos record, field has number 100 , up, refares folder containing photos. if there no photo record, field contains "null"

i have tried:

<asp:button id="button2" runat="server" text="foto" visible='<%# eval("photofolder").tostring() != "null" %>'

but button shown in rows, not ones has string(number) in "photofolder"

you can in gridview rowdatabound event. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

asp.net button gridview visible itemtemplate

No comments:

Post a Comment