Asp.net多视图/使用Javascript或jQuery检查ActiveViewIndex
为什么下面的警报总是显示空值Asp.net多视图/使用Javascript或jQuery检查ActiveViewIndex,javascript,asp.net,null,multiview,activeview,Javascript,Asp.net,Null,Multiview,Activeview,为什么下面的警报总是显示空值 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Keyup._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Keyup._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<%-- <script src="JQuery/jquery-1.4.1.js" type="text/javascript"></script>--%>
<script type="text/javascript">
document.onkeyup = onkeyupOfDocument;
function onkeyupOfDocument(evt) {
//var MultiView = $("*[id$='TextBox1']");
var MultiView = document.getElementById("MultiView1");
alert(MultiView);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:View>
<asp:View ID="View2" runat="server">
</asp:View>
</asp:MultiView>
</div>
</form>
</body>
</html>
这不是真的
提前感谢。在我们的页面完全加载到浏览器并查看源代码后,我们可以说,仅使用javascrip或jquery无法更改asp.net中常规多视图的ActiveViewIndex(不检查)。 因为没有具有MultiView1 id->的元素,只有一个div存在 我们只能检查MultiView1的ActiveViewIndex,因为Nathan用以下代码回答:
var activeViewIndex = <%=MultiView1.ActiveViewIndex %>;
var-activeViewIndex=;
请参见以下链接了解更多信息(最新帖子):因此,以下代码没有任何含义:
var MultiView = document.getElementById("<%=MultiView1.ClientID %>");
var MultiView=document.getElementById(“”);
如果您想更改(不检查)客户端的ActiveViewIndex,有一个技巧->请查看以下代码:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void butSubmit_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = int.Parse(HiddenField1.Value);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Demo</title>
<script language="javascript" type="text/ecmascript">
function OnClientClick( ServerControID,IndexControlID, Index){
var objDemo = document.getElementById(ServerControID);
if(objDemo){
document.getElementById(IndexControlID).value = Index;
objDemo.click();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<span style="color: #ff0000; background-color: #33ccff"><strong>Hi, I am View 1</strong></span></asp:View>
<asp:View ID="View2" runat="server">
<strong><span style="color: background; background-color: #99ff00">Hi, I am View 2</span></strong></asp:View>
</asp:MultiView></div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<input id="btnShow1" type="button" value="Show View 1" onclick="OnClientClick('butSubmit','HiddenField1','0')" />
<input id="btnShow2" type="button" value="Show View 2" onclick="OnClientClick('butSubmit','HiddenField1','1')" />
<div style="display: none">
<asp:Button ID="butSubmit" runat="server" OnClick="butSubmit_Click" Text="Submit" /></div>
</form>
</body>
</html>
受保护的void butSubmit\u Click(对象发送者,事件参数e)
{
MultiView1.ActiveViewIndex=int.Parse(HiddenField1.Value);
}
演示
函数OnClientClick(ServerControID、IndexControlID、Index){
var objDemo=document.getElementById(ServerControID);
if(objDemo){
document.getElementById(indexControl).value=Index;
objDemo.click();
}
}
嗨,我是视图1
嗨,我是视图2
下面是页面加载到ie 9后上面代码的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>
Demo
</title>
<script language="javascript" type="text/ecmascript">
function OnClientClick(ServerControID, IndexControlID, Index) {
var objDemo = document.getElementById(ServerControID);
if (objDemo) {
document.getElementById(IndexControlID).value = Index;
objDemo.click();
}
}
</script>
</head>
<body>
<form method="post" action="WebForm3.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIODMxNDI3MTNkGAEFCk11bHRpVmlldzEPD2RmZJjYXp6H2AsOwVGwRlIRlk0x9agdyp/Kg++cmPNXKpTg" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKrwZG1BAKQo8KrDX7rF3izcHDs+E9bwpx3GnVGoIZVi2Gpv0IOOu9xXNMo" />
</div>
<div>
<span style="color: #ff0000; background-color: #33ccff"><strong>Hi, I am View 1</strong></span></div>
<input type="hidden" name="HiddenField1" id="HiddenField1" value="1" />
<input id="btnShow1" type="button" value="Show View 1" onclick="OnClientClick('butSubmit','HiddenField1','0')" />
<input id="btnShow2" type="button" value="Show View 2" onclick="OnClientClick('butSubmit','HiddenField1','1')" />
</form>
</body>
</html>
演示
函数OnClientClick(ServerControID、IndexControlID、Index){
var objDemo=document.getElementById(ServerControID);
if(objDemo){
document.getElementById(indexControl).value=Index;
objDemo.click();
}
}
嗨,我是视图1
更改
var MultiView = document.getElementById("MultiView1");
到
var MultiView=document.getElementById(“”);
警报始终为空的原因是客户端上没有名为MultiView1的元素:这是控件的服务器端id
要获取客户端的活动视图索引,请使用以下命令:
var activeViewIndex = <%=MultiView1.ActiveViewIndex %>;
var-activeViewIndex=;
在回答这个问题的过程中,有人因为之前的文章而投票否决了我/我正在编辑我的答案/请投票支持我/这不公平…@LostLord:不要担心投票被否决,重要的是你得到了你的答案。当然,排除你不需要给出答案的所有部分会使答案更具可读性。亲爱的@NickLarsen:我删除了对我答案的接受,直到我确定我们不能仅使用JavaScript更改ActiveViewIndex,也不能在没有服务器端代码帮助的情况下更改上面的答案!由于Nathan的评论,我对此表示怀疑。我们需要更多的人来关注这个问题。@LostLord,我的评论没有表明这可以在没有服务器端交互的情况下完成-事实上,它不能-您的问题是如何在客户端上获取活动视图(这是可行的)-您没有声明需要更改视图。这有道理吗?总之,如果您的问题是“如何使用javascript更改活动视图?”,那么答案是“仅通过向服务器回电话”。@Nathan我再次检查了您的代码,并为我的坚持/document.getElementById(“”)表示歉意;返回null&activeViewIndex=;工作完美/我告诉过你这个代码有错误,因为在vs 2010中,它下面有一个绿色的小标记/但是你的答案与我的Q有关,我的答案是另一个问题,请回答这个问题!请稍等/i正在测试它您错了,兄弟-在添加ScriptManager->function pageLoad(){var MultiView=document.getElementById(“”;alert(MultiView);}后,我这样测试您的代码仍然返回空值。。。(我们使用ClientID是因为母版页和内容页-但这里没有渲染后的多视图,只有一个div)请给我们一个解释!!!document.getElementById(“”)表示什么;渲染为?我列出的代码在正常情况下是正确的,听起来你的多视图/服务器端好像出了什么问题。你的多视图渲染了吗?请在浏览器中查看源代码并将其添加到问题中。
var MultiView = document.getElementById("<%=MultiView1.ClientID %>");
var activeViewIndex = <%=MultiView1.ActiveViewIndex %>;