[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-16
5
Medium Priority
?
949 Views
Last Modified: 2012-05-06
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
Comment
Question by:zack_online
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:rdogmartin
ID: 23656105
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
 

Author Comment

by:zack_online
ID: 23656229
Can you post some example here.

Thanks
0
 
LVL 6

Assisted Solution

by:rdogmartin
rdogmartin earned 150 total points
ID: 23656300
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
 
LVL 12

Accepted Solution

by:
CB_Thirumalai earned 600 total points
ID: 23656476
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
 

Author Comment

by:zack_online
ID: 23667346
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question