简体   繁体   English

在updatePanel部分回发之后,客户端pageLoad无法获得新的输入值

[英]Client side pageLoad doesn't get new input values after updatePanel partial postback

I'm having trouble reading a server-side set value from an input, after an UpdatePanel partial postback 在UpdatePanel部分回发之后,我无法从输入中读取服务器端设置值

This is the simplest example I can use to reproduce the issue: 这是我可以用来重现该问题的最简单示例:

<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

        <asp:Panel ID="pnlSrc" runat="server">
            <asp:TextBox ID="txtInput" runat="server" AutoPostBack="true" 
                 OnTextChanged="txtInput_TextChanged"></asp:TextBox>
        </asp:Panel>

        <asp:Panel ID="pnlDst" runat="server">
            <asp:TextBox ID="txtOutput" runat="server" />
            <a id="lnkTest" href="#" onclick="pageLoad();">check txtOutput</a>
        </asp:Panel>

    </ContentTemplate>
</asp:UpdatePanel>

<script type="text/javascript">

    var txtOutput = $('#<%=txtOutput.ClientID%>');

    function pageLoad() {    
        var exe = $(txtOutput).text();    
        alert(exe);
    }

</script>

Server side, I'm just putting txtInput text reversed into txtOutput: 服务器端,我只是将txtInput文本反转为txtOutput:

protected void txtInput_TextChanged(object sender, EventArgs e)
{
    var txt = string.Empty;

    for (int i = txtInput.Text.Length - 1; i >= 0; i--)
    {
        txt += txtInput.Text[i];
    }

    txtOutput.Text = txt;
}

Client side, pageLoad() is executed after every partial postback, but txtOutput value is never updated: it's always the same of the first load (empty, in this case) 客户端,pageLoad()在每次部分回发之后执行,但是txtOutput值从不更新:它总是与第一次加载相同(在这种情况下为空)

I tried also with getInstance().add_endRequest but no success 我也尝试了getInstance()。add_endRequest但没有成功

I would expect that 'exe' contains the actual value of txtOutput.Text, set server side, but it's not so 我希望'exe'包含txtOutput.Text的实际值,设置服务器端,但事实并非如此。

Clicking lnkTest, the same function is invoked with the expected result 单击lnkTest,将调用具有预期结果的相同函数

Sorry if I'm doing something wrong, this is my first post 抱歉,如果我做错了,这是我的第一篇文章

Thank you 谢谢

I see a couple of issues. 我看到几个问题。

First, you are doing the jQuery selector twice. 首先,您要做两次jQuery选择器。 Once when setting the var txtOutput and then again in the pageLoad method when setting exe. 设置var txtOutput时一次,设置exe时再次在pageLoad方法中。 You only need one. 您只需要一个。 The example below eliminates the second - you could go the other way and have txtOutput be just the control ID instead of the actual control object. 下面的示例消除了第二个示例-您可以采用另一种方法,让txtOutput只是控件ID而不是实际的控件对象。

Second, you need to reference the value of the 1st element of the selected object. 其次,您需要引用所选对象的第一个元素的值。

<script type="text/javascript">

    var txtOutput = $('#<%=txtOutput.ClientID%>');

    function pageLoad() {
        var exe = txtOutput[0].value;
        alert(exe);
    }

</script>

your script should be like this 您的脚本应该是这样的

 <script type="text/javascript">
        function pageLoad() {
            alert($('#txtOutput').val());
        }
    </script>

@Karthik Ganesan put me in the right direction, I'll report the final code for convenience of others @Karthik Ganesan为我指明了正确的方向,为了方便其他人,我将报告最终代码

function pageLoad() {

    var txtOutput = $('#<%=txtOutput.ClientID%>');
    var exe = $(txtOutput).val();

    alert(exe);
}

only if var txtOutput = $('#<%=txtOutput.ClientID%>') is inside the pageLoad body , it works even when invoked by ajax's lifecycle; 仅当var txtOutput = $('#<%=txtOutput.ClientID%>') 在pageLoad主体内时 ,即使由ajax的生命周期调用,它也可以工作; I still don't get why, though 我仍然不明白为什么

You can, of course, use static selectors like '#txtOutput', depending on clientIdMode and fx version, and leave it out 当然,您可以根据clientIdMode和fx版本,使用“ #txtOutput”之类的静态选择器,而将其省略

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

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