聚焦丢失在局部回传,里面的UpdatePanel用户控件 [英] Focus lost on partial postback with UserControls inside UpdatePanel

查看:174
本文介绍了聚焦丢失在局部回传,里面的UpdatePanel用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX UpdatePanel内窗体上的一群用户控件,包括一对夫妇的控制,包括一个文本框。

I have a bunch of user controls on a form inside an AJAX UpdatePanel, containing a couple of controls including a TextBox.

每个用户控件这些拼成一个数据输入表单上的字段。有些字段具有的AutoPostBack开启,并触发另一种形式的现场服务器端更新的价值的事件。

Each of these usercontrols makes up a field on a data entry form. Some of the fields have AutoPostBack turned on and fire an event which updates the value in another form field server-side.

然而,当局部回传回报和计算的字段被更新,表单丢失焦点 - 窗体上的第一个字段获得焦点回来。因此,形式是pretty无用的数据输入。

However, when the partial postback returns and the calculated field is updated, the form focus is lost - the first field on the form gets focus back. Therefore the form is pretty useless for data entry.

我已经看到,涉及的工作进行服务器端的专注于接下来的哪个字段,并使用ScriptManager.SetFocus(),传递的形式下一个用户控件的解决方法,但我不能得到这个与我的用户控件工作。这仍然感觉就像一个黑客无论如何,这是令人失望的是UpdatePanel的不只是使这项工作。

I have seen workarounds that involve working out server-side which field to focus on next and using ScriptManager.SetFocus(), passing in the next UserControl in the form, but I can't get this to work with my usercontrols. This still feels like a hack anyhow, and it's disappointing that UpdatePanel doesn't just make this work.

使用ASP.NET 4.0,Visual Studio 2010中。

Using ASP.NET 4.0, Visual Studio 2010.

推荐答案

好吧,我觉得你的问题是,你是包括UpdatePanel内的一切,我的方法是使用的UpdatePanel只对需要更新的控件(也许你会需要不止一个),该UpdatePanel的需要具有的UpdateMode =条件,并只影响内部控制的控制,但这回发应该是在UpdatePanel之外的控制引发的,我张贴的例子,我'已经已经测试和正常工作。

Ok, I think your problem is that you are including everything inside the updatepanel, my approach would be to use updatepanels only for the controls that need to be updated (maybe you would need more than one), the updatepanels would need have UpdateMode=Conditional and triggered only by the control that affects the controls inside, the control that does the postback should be outside of the updatepanel, I'm posting an example that I've already tested and works fine.

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
    <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" 
                    ontextchanged="TextBox2_TextChanged"></asp:TextBox>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="TextBox2" />
            </Triggers>
        </asp:UpdatePanel>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox4" runat="server" AutoPostBack="True" 
            ontextchanged="TextBox4_TextChanged"></asp:TextBox>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="TextBox4" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

正如您可以猜到TextBox2中更新label1和Textbox4更新Label2的。

As you can guess TextBox2 updates Label1 and Textbox4 updates Label2.

这篇关于聚焦丢失在局部回传,里面的UpdatePanel用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆