Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
C#
protected void imgPopupClose_Click(object sender, EventArgs e)
   {
       ButtonClickDemo(sender, e);
   }

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();
}
C#
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

    }
Posted

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

And from master page you can find control using
C#
this.Page.Master.FindControl("controlName")
 
Share this answer
 
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();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900