Asp.net MultiView / 使用 Javascript 或 jQuery 检查 ActiveViewIndex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3300628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:47:01  来源:igfitidea点击:

Asp.net MultiView /check ActiveViewIndex With Javascript Or jQuery

javascriptasp.netnullmultiviewactiveview

提问by SilverLight

Why does the below alert always show me null?

为什么下面的警报总是显示为空?

<%@ 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>

After solving null problem how can I check ActiveViewIndexwith JavaScript or jQuery?

解决空问题后,如何ActiveViewIndex使用 JavaScript 或 jQuery 进行检查?

It seems

它似乎

if(MultiView.ActiveViewIndex == 0)

is not true!!

不是真的!!

Thanks in advance.

提前致谢。

回答by SilverLight

after our page is completely loaded into the browser & viewing the source code , we can say it's not possible to CHANGE (Not CHECK)the ActiveViewIndex of Regular MultiView In asp.net with only javascrip or jquery. because there is no element out there with MultiView1 id -> just a div Existed.

在我们的页面完全加载到浏览器并查看源代码后,我们可以说不可能仅使用 javascript 或 jquery更改(不检查)asp.net 中常规 MultiView 的 ActiveViewIndex。因为那里没有带有 MultiView1 id 的元素 -> 只是一个 div 存在。

we only can check the ActiveViewIndex of MultiView1 as Nathan has answered by below code :

我们只能检查 MultiView1 的 ActiveViewIndex,因为 Nathan 已通过以下代码回答:

var activeViewIndex = <%=MultiView1.ActiveViewIndex %>;

plz see the below link for more information (latest post):
MultiView Is A reach Element
therefore the below code has no meaning :

请参阅以下链接以获取更多信息(最新帖子):
MultiView 是一个到达元素,
因此下面的代码没有意义:

var MultiView = document.getElementById("<%=MultiView1.ClientID %>");

if you want to CHANGE(Not Check) the ActiveViewIndex in client side there is a trick -> plz look at the below codes :

如果你想改变(不检查)客户端的 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>

and here is the source code of the upper codes after the page is loaded in ie 9 :

这是页面加载后的上层代码的源代码 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>

回答by Nathan

change

改变

var MultiView = document.getElementById("MultiView1");

to

var MultiView = document.getElementById("<%=MultiView1.ClientID %>");

the reason the alert is always null is that there is no element on the client-sidecalled MultiView1: that's the server-sideid of the control.

警报始终为空的原因是客户端上没有名为 MultiView1 的元素:这是控件的服务器端id。

to get the active view index to the client-side, use this:

要将活动视图索引发送到客户端,请使用以下命令:

var activeViewIndex = <%=MultiView1.ActiveViewIndex %>;