用户控制按钮单击后,关注页面各自的文本框 [英] Focus on page respective Textbox after user control button click

查看:52
本文介绍了用户控制按钮单击后,关注页面各自的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我在母版页面的webform中有一个3文本框。当第二个或第三个或第一个文本发生更改时,将打开一个包含一个按钮的用户控件。当我单击用户控件中的按钮时,应该聚焦相应的文本更改文本框。我尝试了以下代码



在UserControl中

公共事件EventHandler ButtonClickDemo;

Hi all

I am having a 3 textbox in my webform inside the master page . when text changed of 2nd or 3rd or 1st happens one usercontrol will open which contains one button.When i click the button in user control ,the respective text changed text box should be focused. I tried the following code

In UserControl
public event EventHandler ButtonClickDemo;

protected void imgPopupClose_Click(object sender, EventArgs e)
   {
       ButtonClickDemo(sender, e);
   }



在网页中



在页面

popMsg.ButtonClickDemo + = new EventHandler(Demo1_ButtonClickDemo);

这里 popMsg 是usercontrol



protected void Password_TextChanged(object sender,EventArgs e)

{

Session [s] =Password.ID;

popMsg.Message =请输入有效密码;

popMsg.ShowPopUp();

}


In Web Page

In page
popMsg.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);
here popMsg is usercontrol

protected void Password_TextChanged(object sender, EventArgs e)
{
Session["s"] = "Password.ID;
popMsg.Message = "Please enter valid password";
popMsg.ShowPopUp();
}

protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
    {
        string s = Session["s"].ToString();
        this.FindControl(s).Focus();//here am getting error as object reference not set to instance of the object

    }

推荐答案

从UserControl您可以使用

From UserControl you can find control from Page using
this.Page.FindControl("controlName")



从页面中找到控件从主页面你可以使用


And from master page you can find control using

this.Page.Master.FindControl("controlName")


仅当主页面为没用过....





我的朋友之一得到了解决方案....





protected void Demo1_ButtonClickDemo(object sender,EventArgs e)

{

string s = Session [s ] .ToString();





MasterPage ctl00 = FindControl(ctl00)作为MasterPage; //获取对ContentPlaceHolder的引用

ContentPlaceHolder MainContent = ctl00.FindControl(MainContent)as ContentPlaceHolder;







MainContent.FindControl(s).Focus();



}
This will work only when if master page is not used....


One of my friend got the solution....


protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
{
string s = Session["s"].ToString();


MasterPage ctl00 = FindControl("ctl00") as MasterPage; // Get a reference to the ContentPlaceHolder
ContentPlaceHolder MainContent = ctl00.FindControl("MainContent") as ContentPlaceHolder;



MainContent.FindControl(s).Focus();

}


这篇关于用户控制按钮单击后,关注页面各自的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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