Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Images in DTC Grid

Status
Not open for further replies.

Algon

Programmer
Dec 22, 2002
3
US
Hi,

I am trying to insert an image within the Grid DTC (Design Time Control). This function works:

<SCRIPT LANGUAGE=JavaScript RUNAT=Server>

function w(){
return('<IMG src=&quot; +
'pict.gif&quot;>');'&quot;'
}
</SCRIPT>

Field/Expression:
=w()

The function above works with a referred picture path (pict.gif) only (I mean the referred path in this case) ; however, if I use a modified function to insert variable &quot;img&quot; instead of pict.gif I get error message &quot;undefined&quot;, in other words, I am not able to write variable &quot;img&quot; into the Grid DTC using Response.Write:

function w(){
return('<IMG src=&quot; +
Response.Write(img)&quot;>');'&quot;'
}

Variable &quot;img&quot; is obtained from the following code:

set adoRS=oConn.Execute (&quot;select prod_id, pict from product &quot; &_
&quot;where(prod_id) = 2000&quot;) // prod_id is a variable, code is simplified for demonstration purpose only

img = adoRS(&quot;pict&quot;).value

This works fine, I am able to write variable &quot;img&quot; into the page using server side code <%=img%>,
For example, <IMG src=&quot;
However, a function written in VBScript using <%=img%> and called within the Grid control, doesn’t work within the DTC Grid that is written in JavaScript and run at server.

If anyone knows a way to work around this I would be very appreciative of the assistance.
 
The code in a GridDTC column cell is evaluated using the JScript 'eval' command. This will not understand the Response.Write part (which does not make sense, as it is the returned value of the function that matters here, not its side-effect [of writing text to the page, in this case]).

If G_strImg was a global page variable (ie not defined within any function), then you should be able to just write:

function w(){
return('<IMG src=&quot;' +
G_strImg + '
&quot;>');
}

But in this case the image file name would be the same for the whole grid.

If you needed a different image on each row, then the data source for the Grid should provide the image file name, and pass it thru the grid to the function as a parameter.
 
Hi Merlin:

thank you for your response. Since I need a different image on each row I can’t use a global page variable G_strImg as recommended. Pursuant to your advice, I wrote the following code. The code works fine but I am not able to get the image into the DTC Grid. Any idea?

<SCRIPT LANGUAGE=JavaScript RUNAT=Server>

function w(hrtg){
var DBConn = Server.CreateObject('ADODB.Connection');
DBConn.ConnectionTimeout = Application('dbconn1_ConnectionTimeout');
DBConn.CommandTimeout = Application('dbconn1_CommandTimeout');
DBConn.CursorLocation = Application('dbconn1_CursorLocation');
DBConn.Open(Application('dbconn1_ConnectionString'), Application('dbconn1_RuntimeUserName'), Application('dbconn1_RuntimePassword'));
var cmdTmp = Server.CreateObject('ADODB.Command');
var rsTmp = Server.CreateObject('ADODB.Recordset');
cmdTmp.ActiveConnection = DBConn;
rsTmp.Source = cmdTmp;
cmdTmp.CommandType = 1;
cmdTmp.CommandTimeout = 30;
cmdTmp.CommandText = 'SELECT prod_id, pict, star FROM mm_product WHERE(prod_id) = 2000';
rsTmp.CacheSize = 100;
rsTmp.CursorType = 3;
rsTmp.CursorLocation = 3;
rsTmp.LockType = 3;
Search_.setRecordSource(rsTmp);
Search_.open();

hrtg=Search_.fields.getValue('pict'); //pict is a field in the database containing the image file name

//the code above works upto this point, hrtg value is correct; I am able to write hrtg to the page(using Response.Write to test value of hrtg) but not to the DTC Grid

// this line doesn’t work
return ('<IMG src=&quot; 'hrtg&quot;>');'&quot;'
}
</SCRIPT>
 
This seems a bit odd (it should work) - and a bit long winded too!

Is the grid based upon some other recordset? If so, could you not just include the filename column in that recordset? This would give you an image filename for each row, without an extra SQL call, via the function above. This could be entered into the grid column as follows:

='<IMG src=&quot; +
[pict] + '&quot;>'

Note the square brackets around the column name. This is converted by the DTC into the correct rsRecordset.fields.getValue('pict') syntax for you - a great time saver!

Also, your code will return the same image each time anyway - the
... WHERE(prod_id) = 2000' is a constant, and not dependant on the parameter passed to the function!
 
Hi Merlin:

Thank you for your advice. I changed the stored procedure to get &quot;pict&quot; without an extra SQL call, and included the filename column in the recordset and pass it through the DTC grid as you recommended. This code works:

Field/Expression:
='<IMG src=&quot; +[pict] + '&quot;>'

As to your note &quot;...WHERE(prod_id)= 2000 is a constant...&quot; as mentioned before, the code is simplified for demonstration purpose; in fact, prod_id is variable.

Thank you once again for your help. It is amazing how it works, the picture can be formatted as well.

Needless to say that I apologize for lack of knowledge of DTCs’! (By the way, they have never been fully documented).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top