我有一個頁面,其中包含使用Javascript顯示來自特定文件夾(幻燈片放映)的圖像的圖像控件。我在頁面加載中設置了HiddenField Value的值,並希望使用Javascript訪問這些值。但是,在頁面加載中設置隱藏字段的值後,Javascript中的隱藏字段的值顯示NULL。如何在頁面加載時設置HiddenField的值,並在asp.net中使用Javascript進行訪問?
在.aspx頁面中:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js"></script>
</head>
<script type="text/javascript">
var folderNm = document.getElementById('<%#HiddenFieldFolderName.ClientID%>');
var MaxIndex = document.getElementById('<%#HiddenFieldMaxIndex.ClientID%>');
var mainImage = document.getElementById('mainImage');
//mainImage.src = "Presentations/7/Slide1.GIF";
//Initilize start value to 1 'For Slide1.GIF'
var currentIndex = 1;
//NOTE: Set this value to the number of slides you have in the presentation.
//var maxIndex = 7;
var maxIndex = MaxIndex;
alert("Folder Name " + folderNm + "\n MaxIndex " + MaxIndex);
function swapImage(imageIndex) {
//Check if we are at the last image already, return if we are.
if (imageIndex > maxIndex) {
currentIndex = maxIndex;
return;
}
//Check if we are at the first image already, return if we are.
if (imageIndex < 1) {
currentIndex = 1;
return;
}
currentIndex = imageIndex;
//Otherwise update mainImage
//document.getElementById("mainImage").src = 'PPT/GIFs/Slide' + currentIndex + '.GIF';
document.getElementById("mainImage").src = 'Presentations/' + folderNm + '/' + 'Slide' + currentIndex + '.GIF';
// document.getElementById("mainImage").src = 'Presentations/7/Slide' + currentIndex + '.GIF';
return;
}
</script>
<body>
<form id="form1" runat="server" >
<div>
<div>
<%-- <img src="PPT/GIFs/Slide1.GIF" id="mainImage" name="mainImage" width="50%" height="50%" alt="">--%>
<img id="mainImage" name="mainImage" width="25%" height="25%" alt="">
</div>
<div>
<a href="#" onclick="swapImage(0);">
<img src="/images/firstss.png" border="0" alt="First"></a>
<a href="#" onclick="swapImage(currentIndex-1);">
<img src="/images/prev.png" border="0" alt="Previous"></a>
<a href="#" onclick="swapImage(currentIndex+1);">
<img src="/images/nexts.png" border="0" alt="Next"></a>
<a href="#" onclick="swapImage(maxIndex);">
<img src="/images/lasts.png" border="0" alt="Last"></a>
</div>
<div>
<asp:HiddenField ID="HiddenFieldMaxIndex" runat="server" />
<asp:HiddenField ID="HiddenFieldFolderName" runat="server" />
</div>
</div>
</form>
</body>
</html>
在.aspx.cs文件:
protected void Page_Load(object sender, EventArgs e)
{
string foldername = string.Empty;
if (Request.QueryString["di"] != null)
{
foldername = Request.QueryString["di"].ToString();
HiddenFieldFolderName.Value = foldername;
HiddenFieldMaxIndex.Value = Request.QueryString["Files"].ToString();
}
}
在這裏,隱藏字段值顯示警告()框爲空。幫助讚賞。
此外,請確保您採取的是價值,而不是對象本身。 var folderNm = document.getElementById('<%#HiddenFieldFolderName.ClientID%>').value; – Yeronimo
雅我也試過'價值'它也顯示'空':( –
@SHEKHARSHETE,你是否用'<%='而不是'<%#'來試試 – Habib