get textbox.ClientID in a usercontrol

D

davidr

Hi,

I've researched this a lot and have a well understanding but still
can't get the darn textbox in the javascript.

I have a textbox that is set to runat="server" and I have a client
function for a comboBox when selectitemindexchanged happens it will
trigger. I know it goes into that because I did a alert("test"). Now
in there I want to take the item in the comboBox and put it in the
textbox that is runat="server". I know I have to use the
textbox.ClientID to get a reference to the textbox because its inside a
usercontrol.

So I am trying to do
var txtBox = document.getElementbyId("<%=textBox.ClientID%>").value;
or
var txtBox = document.getElementbyId("<%=txtBoxSnrInsp.ClientID%>");

alert(txtBox.ID)

to verify I have access to the textbox, but it never works. I've seen
many other examples on the net, but I feel I'm doing something wrong or
it isn't working. I know that since its in a usercontrol that the ID
is preappended with the usercontrol name that is why I must use
ClientID. Of course if I delete runat="server" then I can gain access
to the textbox, but I need that because when the user clicks save on
the usercontrol it will take the attributes in the textboxes and update
them to the database. Please anyone have any code example for this
scenario?
 
M

Mark Rae

I know I have to use the textbox.ClientID to get a reference to the
textbox because its inside a usercontrol.

You don't *have* to do it that way - you just have to know the correct name
of the textbox once it's been sent down to the client machine.
So I am trying to do
var txtBox = document.getElementbyId("<%=textBox.ClientID%>").value;
or
var txtBox = document.getElementbyId("<%=txtBoxSnrInsp.ClientID%>");

alert(txtBox.ID)

Neither of these will work, because your textbox is inside a usercontrol.

Easiest way is to do a View Source once the page has been loaded, and see
how the name of the textbox has been constructed. Thereafter, it'll be a
simple matter for you to refer to it in your client-side JavaScript.
 
D

davidr

Few things,

One this user control is in a multiview. The multiview has several
user controls in it. I can't find the textbox in the source code.
I've read that you can reference a textbox that is in a usercontrol via
ClientID. You are saying there is no way to reference a textbox via
clientID?

David
 
K

Kumar Reddi

If this user control is not dynamically loaded then you can reference
the textbox id in javascript as UserControlID_textBoxID.

For example if you give the id for the user control in question as
"MyUserControl"
and textbox id is "MyTextBox".

Then when this textbox renders to the web page.. its id would become
"MyUserControl_MyTextBox"
 
M

Mark Rae

One this user control is in a multiview.

Sigh... NOW you tell us that...!
The multiview has several user controls in it. I can't find the textbox
in the source code.

Is the control in question in the "current" MultiView page...? Only the
controls in the current page are actually downloaded to the client, unlike
previous implementations of similar functionality achieved by dynamically
showing / hiding divs, etc...
I've read that you can reference a textbox that is in a usercontrol via
ClientID.

Yes you can, so long as you understand how ASP.NET constructs control IDs
before sending the HTML to the client.
You are saying there is no way to reference a textbox via clientID?

No I'm not - I'm saying that a textbox in a UserControl in a MultiView will
be called something different, probably <ControlID>_<ClientID>...

If your textbox is not actually downloaded (i.e. it's not in the current
multiview page), you'll NEVER be able to reference it client-side...
 
D

davidr

Mark said:
Sigh... NOW you tell us that...!


Is the control in question in the "current" MultiView page...? Only the
controls in the current page are actually downloaded to the client, unlike
previous implementations of similar functionality achieved by dynamically
showing / hiding divs, etc...


Yes you can, so long as you understand how ASP.NET constructs control IDs
before sending the HTML to the client.


No I'm not - I'm saying that a textbox in a UserControl in a MultiView will
be called something different, probably <ControlID>_<ClientID>...

If your textbox is not actually downloaded (i.e. it's not in the current
multiview page), you'll NEVER be able to reference it client-side...
 
D

davidr

The multiview page index is set by what a user selects on a panel. So
he clicks the panel the usercontrol appears on the screen by setting
the multiview index for that usercontrol. Inside that usercontrol I
have javascript there were I want to reference that textbox that is ran
on the server side. Hope that helps out more. Sorry if there was
confusion on my part.
I'll try the controlID_clientID thing.

David
 
D

davidr

One more thing. Does the textbox being inside a telerik ajaxpanel also
make a difference as to what level I need to reference this textbox via
client side? So on the usercontrol, I have a telerik ajaxpanel. So
when a few of the controls do postbacks they use ajax. It works well
and nice. Inside that ajax panel is the textbox that I want to
reference clientside, that ajax panel is on the usercontrol, that
usercontrol is in a multiview page, on the main webpage, The main
webpage can change which user control is viewable via a panel, that
will change its index.

Hope that clears it up more.
 
D

davidr

I was able to solve it. I looked at the Page load event to see inside
there what the textbox.clientID was to get it since it wasn't appearing
in the source code. It was like you said the Usercontrol_textboxid.
But it was appending a 1 to the usercontrol so like
usercontrol1_textboxid. Thanks,

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top