2012-11-05 104 views
1

我試圖從一個GridView行傳遞一個文件名作爲參數傳遞給JS函數(C#)(。):傳遞參數與點到JavaScript函數

onclick='<%# Eval("ImageName", "return loadImage({0})") %>' 

其被渲染爲:

onclick="return loadImage(Happy.jpg)" 

和ERROR:

Uncaught ReferenceError: Happy is not defined 

當我手動加單引號(」「),它的工作正常,像

onclick="return loadImage('Happy.jpg')" 

現在我該怎麼編程添加引號,否則我如何傳遞JS參數與點(。)?

('{0}')did'nt工作,錯誤:「服務器標記不正確形成。」

=============== SOLUTION ==================

好了,解決它像這樣:

添加onclick="javascript:loadImage(this.src);"

<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/UploadedImages/"+ Eval("ImageName") %>' Height="43px" Width="55px" onclick="javascript:loadImage(this.src);" CssClass="dlImage" /> 

了js函數內部完整路徑即

"http://localhost:52636/ChuttiTime_Alka/UploadedImages/Happy.jpg"

切片它來獲取文件名

function loadImage(imageName) { 
var bigImage = document.getElementById("ctl00_ContentPlaceHolder1_FEPropertyImageControl1_propertyBigImage") 
var filename = imageName.replace(/^.*[\\\/]/, '') 
bigImage.src='../UploadedImages/'+filename; 
} 

回答

0

好了,解決了這個問題是這樣的:

添加onclick="javascript:loadImage(this.src);"

<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/UploadedImages/"+ Eval("ImageName") %>' Height="43px" Width="55px" onclick="javascript:loadImage(this.src);" CssClass="dlImage" /> 

了js函數內部完整路徑即

"http://localhost:52636/ChuttiTime_Alka/UploadedImages/Happy.jpg"

將其切片得到文件名稱

function loadImage(imageName) { 
var bigImage = document.getElementById("ctl00_ContentPlaceHolder1_FEPropertyImageControl1_propertyBigImage") 
var filename = imageName.replace(/^.*[\\\/]/, '') 
bigImage.src='../UploadedImages/'+filename; 
}