• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 953
  • Last Modified:

How do I get control id from a user contror using javascript

Hi expert,

My scenarion is, I have an aspx page (Index.aspx). In codebehind of my aspx, i add a user control using the ff code

string UserControl = "myUserControl1.ascx" or "myUserControl2.ascx" or "myUserControl3.ascx"

Control ucTemplate = LoadControl(UserControl );
 myBody.Controls.Add(ucTemplate);

Inside myUserControl1.ascx i have a place holder that contains another usercontrol which is myContent1.ascx...

Now my problem is how am i gonna get all my textbox in myContent1.ascx.

Thanks
- zack
0
zack_online
Asked:
zack_online
  • 2
  • 2
2 Solutions
 
rdogmartinCommented:
From where do you want to access the textbox? From the aspx page? Typically you expose controls as properties when you want to access them from container controls. In your case you would create a public property in myContent1.ascx. That allows you to access it from myUserControl1.ascx. Then create another public property in myUserControl1.ascx that exposes the first property. That gives the parent aspx page access to the textbox. Voila!
0
 
zack_onlineAuthor Commented:
Can you post some example here.

Thanks
0
 
rdogmartinCommented:
In the control that contains the textbox, create a property that exposes it:

public TextBox MyTextBox
{
  get
  {
    return txtMyTextBox;
  }
}

Then, in the containing user control, create another property that exposes the inner property:

public TextBox TheTextBox
{
  get
  {
    return InnerUserControlInstance.MyTextBox;
  }
}

Now your aspx page should be able to access the textbox by accessing the new property of your user control:

Textbox txt = UserControlInstance.TheTextBox;
0
 
CB_ThirumalaiCommented:
You can use the .FindControl method to get the desired control from the Main control.  For example, ucTemplate is the control you are adding to the page and it contains any of [ "myUserControl1.ascx" or "myUserControl2.ascx" or "myUserControl3.ascx" ], which again contains, myContent1.ascx.  So, from ucTemplate find the textbox as using
Textbox T = (Textbox)ucTemplate.FindControl("<CONTROL_ID>");

To get all the text boxes without putting the Control IDs, use the code below
for (int i = 0; i < ucTemplate.Controls.Count; i++)
{
    string sControlID = ucTemplate.Controls[i].ID;
    if (sControlID != null)
    {
        if (sControlID.IndexOf("ph") == 0)
        {
            PlaceHolder p = (PlaceHolder)ucTemplate.Controls[i];
            if (p.Controls.Count > 0)
            {
                for (int j = 0; j < p.Controls.Count; j++)
                {
                    if (p.Controls[j].GetType().ToString() == "System.Web.UI.WebControls.TextBox")
                    {
                        TextBox txtValue = (TextBox)p.Controls[j];
                        if (txtValue != null)
                        {
                            // do something
                        }
                    }
                }
            }
        }
    }
}

Open in new window

0
 
zack_onlineAuthor Commented:
Actually im asking for a javascript function to get those controlid inside my user control (myContent1.ascx). Because im planning to execute the javascript when "myUserControl1.ascx" is load. Anyways thank you for the reply.

zack
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now