简体   繁体   English

从Javascript触发回发失败

[英]Triggering a postback from Javascript fails

I am trying to trigger a postback if a certain condition is true. 如果某个条件为真,我试图触发回发。 Initially, a user will click on a button on my form, the server does some work, and in the process of doing that work it assigns a hidden field the value of '1'. 最初,用户将单击我表单上的按钮,服务器将执行一些工作,并且在执行该工作的过程中,它会为隐藏字段分配值“ 1”。 When the page reloads after that very first postback, I am trying to use javascript to check the hidden field, and if it is '1' I need the page to postback again so the server side can do some additional processing. 当页面在第一次回发后重新加载时,我正在尝试使用javascript检查隐藏字段,如果它是'1',则我需要再次回发页面,以便服务器端可以进行一些其他处理。 The reason for doing it this roundabout way is so that I can create controls on the form from my C# code behind as well as download a file in 1 user interaction. 之所以采用这种回旋方式,是因为我可以从后面的C#代码在表单上创建控件,也可以在1个用户交互中下载文件。 Here is what I have so far: 这是我到目前为止的内容:

<script type="text/javascript" language="JavaScript">
        function atload() {
            var HWInfo = document.getElementById('HiddenHW').value;
            if (HWInfo == '1') {
                alert("flag has been set");

                __doPostBack('<%= hdnHidden.UniqueID  %>', '');
             }

        }
        $(document).ready(atload);

    </script>

The alert that says the flag has been set correctly fires, but the __doPostBack does not. 提示已正确设置标志的警报会触发,但__doPostBack不会触发。 In my ASPX file, here is the relevant part: 在我的ASPX文件中,这是相关部分:

 <form id="InventoryForm" runat="server">

<div>
    <asp:Label ID="lblClientList" runat="server" Text="Client List"></asp:Label>
    <asp:DropDownList ID="comboClientList" runat="server"></asp:DropDownList>
    <asp:Label ID="spacer1" runat="server" Text="     "></asp:Label>
    <asp:Button ID="btnGenerateHWReport" runat="server" Text="Generate Hardware Inventory Report" />
    <asp:Label ID="spacer2" runat="server" Text="     "></asp:Label>
    <asp:Button ID="btnGenerateSWReport" runat="server" Text="Generate Software Inventory Report" />
    <br />
    <br />

    <asp:Panel ID="MissingCompPanel" runat="server"></asp:Panel>
    <asp:HiddenField ID="HiddenHW" runat="server" Value="0" />
    <asp:HiddenField ID="hdnHidden" runat="server" />

</div>
</form>

I can tell the postback never fires, because I have breakpoints in the Page_Load C# codebehind that never get tripped. 我可以说回发永不触发,因为在Page_Load C#代码的后面有断点,不会断路。 I have break points on almost every single line of this: 我几乎在每一行都有断点:

        if (!Page.IsPostBack)
        {
            // Page is not a postback, this is the first visit here
            string foo = HiddenHW.Value;
        }
        else
        {
            // Page is a postback and not initial load
            string foo = HiddenHW.Value;
        }

Why is my __doPostBak after the alert not firing, and is there a better way to do this? 为什么警报发出后我的__doPostBak无法触发,还有更好的方法吗? The end result I want is if my hidden field is '1', then I want my 2nd trip to the server to 1) actually happen and 2) know that the hidden field is '1' and not its default '0'. 我想要的最终结果是,如果我的隐藏字段为“ 1”,那么我希望我的服务器第二次访问到1)实际发生,并且2)知道隐藏字段为“ 1”,而不是其默认值“ 0”。

Thanks! 谢谢!

仅以编程方式单击“提交”按钮并将其按正常方式调用__doPostBack怎么样?

尝试调用页面上的__doPostBack方法而不是隐藏控件

__doPostBack('__Page', '');

When you get the value of HiddenHW , you're not using the right ID. 当您获得HiddenHW的值时,就没有使用正确的ID。 If you look at the rendered source, the ID of the control is something like ctl00_HiddenHW . 如果查看呈现的源,则控件的ID类似于ctl00_HiddenHW To get that ID, you should use HiddenHW.ClientID . 要获取该ID,您应该使用HiddenHW.ClientID I believe __doPostBack also needs the ClientID, not UniqueID. 我相信__doPostBack还需要ClientID,而不是UniqueID。

function atload() {
    var HWInfo = document.getElementById('<%= HiddenHW.ClientID %>').value;
    if (HWInfo == '1') {
        alert("flag has been set");

        __doPostBack('<%= hdnHidden.ClientID %>', '');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM