简体   繁体   English

专注于C#中的用户控件

[英]focus on the user control in C#

I have a user control(error control) in a master page. 我在母版页中有一个用户控件(错误控件)。 by default the user control is hidden(visible false) 默认情况下,用户控件是隐藏的(可见false)

on click of a submit button i am showing the error control.Now on post back I want to bring focus to this control.I am writing the java script in register Startup script as following: 在单击提交按钮时,我正在显示错误控件。现在,在回发时,我要重点关注此控件。我在注册启动脚本中编写了Java脚本,如下所示:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Scroll", "window.scrollTo(0, 0)", true);

on postback page goes up shows this control and then comes back to its previous scroll position. 在回发页面上,显示此控件,然后返回到其先前的滚动位置。

Have you set MaintainScrollPositionOnPostback to true in the page directive at the top of your page/master page or in the <pages> element of your web.config? 您是否已在页面/母版页顶部的page指令中或web.config的<pages>元素中将MaintainScrollPositionOnPostback设置为true

You could also look at using the Focus method of the control in your postback handler 您还可以在回发处理程序中使用控件的Focus方法

Edit to clarify: 编辑以澄清:

If any of these are set to true then it's very likely that they will override your startup script to send the page back to the top of the page - have you tried setting it to false in either the Page directive, or on the Page object in the same code that displays the error control? 如果其中任何一个设置为true则很有可能它们将覆盖您的启动脚本,以将页面发送回页面顶部-您是否尝试在Page指令中或在Page对象中将其设置为false?显示错误控制的相同代码?

Finally I have solved this issue. 终于我解决了这个问题。 following is the way i am doing it... 以下是我这样做的方式...

i have added a text box of width Zero% in the same where my user control(error control is placed) 我在用户控件(放置错误控件)的位置添加了宽度为零%的文本框

now whenever an error occurs I call following method. 现在,每当发生错误时,我都会调用以下方法。

public void SetFocus()
        {
            ScriptManager3.SetFocus(txtFocus.ClientID);
        }

although error control is not focused in reality this clever work around helps me achieve my goal. 尽管错误控制实际上并不是针对性的,但这种巧妙的解决方法可以帮助我实现目标。

hope it is helpful for others 希望对其他人有帮助

 private void Control_GotFocus(object sender, EventArgs e)
    {
        Console.WriteLine("Control GotFocus : " + ((sender as Control).Name));
    }

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

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