By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

referencing asp.net controls in javascript

P: 50
hello all
i am trying to pass some text from a textbox to a dropdownlist in my asp.net page, i am using the modal popup extender and vs 2008, how do i go about referencing asp controls when writing the javascript? i have the pop up display with two textbox's and two buttons, when the ok button is clicked i want whatever is in the textboxs to go to the asp dropdown, so how would i go about doing this?

thanks
May 19 '08 #1
Share this Question
Share on Google+
15 Replies


acoder
Expert Mod 15k+
P: 16,027
Check how the controls are rendered on the client-side.

If you can get the IDs and/or names it should be relatively straightforward to access the elements. If it's not easy to determine the ID/name from what the ASP.NET code looks like, use ASP.NET code to get the client-side ID and use that with document.getElementById().
May 19 '08 #2

P: 50
Check how the controls are rendered on the client-side.

If you can get the IDs and/or names it should be relatively straightforward to access the elements. If it's not easy to determine the ID/name from what the ASP.NET code looks like, use ASP.NET code to get the client-side ID and use that with document.getElementById().
Is that how i call an asp.net server control? like for the button 1 click event i have me.dropdownlist1.items.add(textbox2.text), the textbox that resides in the modal popup...is there a way to reference this control by just getting its id/name, or do i need to redo it in javascript?
May 19 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Show how the code is rendered in your browser.
May 19 '08 #4

P: 50
ok, so if use this code:

var fill
function onclick()
{
document.getelementbyid('<%=dropdownlist1.clientid %>')
}

will just using this reference that control and make the click event for the control happen?...also i am aiming this event at a contol that is in an update panel, so do i need it to do a postback?
May 19 '08 #5

P: 50
ok, this is what i have my modal popup extender pointing too for the onokscript..
<script type="text/javascript">
function onclick()
{

}
</script>

this is what i have in the button1 click event
Me.commentsbox.Text = "textbox1.text"

textbox1.text and button1 are in the modalpopup, so what i need to know is what ajax function do i use to call this event when the button1 is clicked so that it fills the comment box with whatever is in textbox1? i have searched google and cant find anything relating to what i am wanting to do, maybe im just searching for the wrong thing...PLEASE HELP!!!
May 20 '08 #6

acoder
Expert Mod 15k+
P: 16,027
ok, so if use this code:
...
document.getelementbyid('<%=dropdownlist1.clientid %>')
...
will just using this reference that control and make the click event for the control happen?
JavaScript is case-sensitive, so it's document.getElementById(). The code would reference the control, but that's it. You need to call a method or set a property to actually do something with it.
May 20 '08 #7

acoder
Expert Mod 15k+
P: 16,027
so what i need to know is what ajax function do i use to call this event when the button1 is clicked so that it fills the comment box with whatever is in textbox1?
As I've mentioned before, you need the client-side ID.

As for the filling, say you've referenced them, then the following should work:
Expand|Select|Wrap|Line Numbers
  1. // commentbox is a ref. to the comment box and textbox1 to the text box
  2. commentbox.value = textbox1.value;
  3. // if you need to add to a drop down:
  4. try {
  5.     dropdown1.add(textbox1.value,null); // standard-compliant
  6. } catch (err) {
  7.     dropdown1.add(textbox1.value); //IE-only
  8. }
Is the modal popup that you refer to a real popup window or a DHTML pseudo-popup? If it's a window, you will need to reference the window first to gain access to the elements within it from the parent page, or vice versa.
May 20 '08 #8

P: 50
As I've mentioned before, you need the client-side ID.

As for the filling, say you've referenced them, then the following should work:
Expand|Select|Wrap|Line Numbers
  1. // commentbox is a ref. to the comment box and textbox1 to the text box
  2. commentbox.value = textbox1.value;
  3. // if you need to add to a drop down:
  4. try {
  5.     dropdown1.add(textbox1.value,null); // standard-compliant
  6. } catch (err) {
  7.     dropdown1.add(textbox1.value); //IE-only
  8. }
Is the modal popup that you refer to a real popup window or a DHTML pseudo-popup? If it's a window, you will need to reference the window first to gain access to the elements within it from the parent page, or vice versa.
its dhtml, ok i cant try this right now because im at work, maybe on lunch, but this will be my new code....
Expand|Select|Wrap|Line Numbers
  1. function onok()
  2. {
  3. document.getElementById("textbox1")
  4. document.getElementById("dropdownlist1")
  5. try{
  6.        dropdownlist1.add(textbox1.value,null);
  7. }catch (err) {
  8.                    dropdownlist1.add(textbox1.value);
  9. }
  10.  
im using the vs2008 and when i was trying last night it kept on trying to get me to use nodevalue for some reason, is that just the way vs2008 does it?

thanks for the help so far
May 20 '08 #9

acoder
Expert Mod 15k+
P: 16,027
Change that to:
Expand|Select|Wrap|Line Numbers
  1. function onok()
  2. {
  3. var textbox1 = document.getElementById("textbox1");
  4. var dropdownlist1 = document.getElementById("dropdownlist1");
  5. try{
  6.        dropdownlist1.add(textbox1.value,null);
  7. }catch (err) {
  8.                    dropdownlist1.add(textbox1.value);
  9. }
May 20 '08 #10

P: 50
ok im using this as my code
Expand|Select|Wrap|Line Numbers
  1. function onok()
  2.  
  3.       {
  4.  
  5.       var textbox1 = document.getElementById("textbox1");
  6.  
  7.       var commentbox = document.getElementById("commentsbox");
  8.  
  9.       try{
  10.  
  11.             commentsbox.nodevalue = textbox1.nodevalue;
  12.             alert(textbox1.nodeValue);
  13.  
  14.       }catch (err) {
  15.  
  16.                          commentsbox.value = textbox1.value;
  17.  
  18.       }
  19.  
and this is the modal pop up code
Expand|Select|Wrap|Line Numbers
  1. <cc1:ModalPopupExtender ID="HyperLink1_ModalPopupExtender" runat="server" 
  2.                    CancelControlID="button2" DropShadow="True" OkControlID="button1" 
  3.                    PopupControlID="panel2" TargetControlID="HyperLink1" 
  4.                    onokscript="onok()">
  5.                </cc1:ModalPopupExtender>
  6.  
when i have the popup come up and i click on the button, nothing happens, im using vs2008, so could this be the problem?
May 20 '08 #11

acoder
Expert Mod 15k+
P: 16,027
Firstly, are the IDs on the client-side "textbox1" and "commentsbox"?

If they are, you still need to make sure you use the correct variable name. You have commentbox and commentsbox.

There's no nodeValue in JavaScript, use value instead. There's also no need for a try/catch here.

If that doesn't solve the problem, show me what the code looks like in your browser (view source).
May 20 '08 #12

P: 50
yeah the ids are the same...

i used this code also
Expand|Select|Wrap|Line Numbers
  1. function onok()
  2. {
  3. var textbox1 = document.getElementById("textbox1");
  4. var dropdownlist1 = document.getElementById("dropdownlist1");
  5. try{
  6.        dropdownlist1.add(textbox1.value,null);
  7. }catch (err) {
  8.                    dropdownlist1.add(textbox1.value);
  9. }
  10.  
and still nothing, does it have something to do with my button? should i make the button click event point to the ajax script? because right now all i have is the modal pop up extender onokscript event pointing to it...
May 20 '08 #13

acoder
Expert Mod 15k+
P: 16,027
Can you post a test page link? If not, at least post the code (client-side, not .NET), so I can see where the problem might lie.
May 21 '08 #14

P: 50
i acutally got it to work last night, turns out i forgot that javascript is case sensitive, so my textbox id was TextBox1 and i was looking for textbox1, so when i changed to TextBox1 it started working, thanks for the help though....
also, im trying to add items to a dropdownlist that is nested in an update panel, the thing i couldnt get to work last night was the adding items to the dropdownlist, is there a way a make the updatepanel do a postback within the script?
May 21 '08 #15

acoder
Expert Mod 15k+
P: 16,027
i acutally got it to work last night, turns out i forgot that javascript is case sensitive, so my textbox id was TextBox1 and i was looking for textbox1, so when i changed to TextBox1 it started working, thanks for the help though....
That's good. Could've been spotted if you posted the client-side code.
also, im trying to add items to a dropdownlist that is nested in an update panel, the thing i couldnt get to work last night was the adding items to the dropdownlist, is there a way a make the updatepanel do a postback within the script?
Post the relevant HTML code as it appears in the browser.
May 21 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.