我有一个页面,其中包含使用 Javascript 显示特定文件夹(幻灯片放映)中的图像的图像控件。我已在页面加载时设置了 HiddenField 值的值,并希望使用 Javascript 访问这些值。但是,在页面加载上设置隐藏字段的值后,Javascript 中的隐藏字段的值显示为 NULL。
在 .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();
}
}
此处,隐藏字段值在 alert() 框中显示为 null。帮助赞赏。