470,636 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

fill text in textbox by dropdown only when clicking on button

i want to enter text in textbox by dropdown only when clicking on button
i want that all this perform at client side by javascript..
this is dont in apsx page but i want it in .ascx

this is following code that i used in aspx page and successfully run as i want:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.         function insertToken(element) 
  4.     {
  5.             var ivxtoken = document.getElementById('DropDownList1').options[document.getElementById('DropDownList1').selectedIndex].value;
  6.  
  7.             var element_v = document.getElementsByName(element)[0];
  8.  
  9.             if (document.selection) {
  10.                 element_v.focus();
  11.                 sel = document.selection.createRange();
  12.                 sel.text = ivxtoken;
  13.                 return;
  14.             } if (element_v.selectionStart || element_v.selectionStart == "0") {
  15.                 var t_start = element_v.selectionStart;
  16.                 var t_end = element_v.selectionEnd;
  17.                 var val_start = element_v.value.substring(0, t_start);
  18.                 var val_end = element_v.value.substring(t_end, element_v.value.length);
  19.                 element_v.value = val_start + ivxtoken + val_end;
  20.             } else {
  21.                 element_v.value += ivxtoken;
  22.             }
  23.         }                     
  24.     </script>
  25.  
  26. <table style="width:100%;" width="%">
  27.             <tr>
  28.                 <td class="style1" width="20%">
  29.                     <asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
  30.                         <asp:ListItem>veera</asp:ListItem>
  31.                         <asp:ListItem>jethva</asp:ListItem>
  32.                         <asp:ListItem>pollux</asp:ListItem>
  33.                         <asp:ListItem>ahmedabad</asp:ListItem>
  34.                         <asp:ListItem>gujrat</asp:ListItem>
  35.                         <asp:ListItem>India</asp:ListItem>
  36.                     </asp:DropDownList>
  37.                 </td>
  38.                 <td class="style1" width="30%">
  39.                      <input id="Button2" type="button" value="Inser Value" onclick="insertToken('TextBox1');" />
  40.                 </td>
  41.                 <td class="style1">
  42.                 </td>
  43.             </tr>
  44.             <tr>
  45.                 <td>
  46.                     &nbsp;</td>
  47.                 <td>
  48.                     &nbsp;</td>
  49.                 <td>
  50.                     &nbsp;</td>
  51.             </tr>
  52.             <tr>
  53.                 <td colspan="2">
  54.                     <asp:TextBox ID="TextBox1" runat="server" Height="91px" TextMode="MultiLine" 
  55.                         Width="280px"></asp:TextBox>
  56.                 </td>
  57.                 <td>
  58.                     &nbsp;</td>
  59.             </tr>
  60.         </table>
  61.  
but i dont know how to make it work in .ascx
please solve it ..
Jun 29 '10 #1
2 2940
ThatThatGuy
449 Expert 256MB
@polluxsoftech
you can paste the same in ascx markup page...
Jun 29 '10 #2
Frinavale
9,735 Expert Mod 8TB
The unique ID given to the DropDownList1 when it's rendered as a <select> HTML element in the browser is not "DropDownList1". It's something else. You need to provide the correct ID in order for the document.getElementById() method to work. To do this you need to provide the DroDownList1's ClientID to the getElementById() method.

If your JavaScript is in your ASCX page source code, instead of having:
var ivxtoken = document.getElementById('DropDownList1').options[document.getElementById('DropDownList1').selectedI ndex].value;

You need to provide the ClientID property of DropDownList1...like this:
Expand|Select|Wrap|Line Numbers
  1.  var ivxtoken = document.getElementById('<%=DropDownList1.ClientID%>').options[document.getElementById('<%=DropDownList1.ClientID%>').selectedIndex].value;
If the JavaScript is not located in the ASCX page then it would be best to modify your method to accept the DropDownList1's Client ID and provide it to your JavaScript when you add a reference for an in your C#/VB.NET server-side code.

For example, you'd change your JavaScript function's signature to something like this:
Expand|Select|Wrap|Line Numbers
  1. function insertToken(element, ddlClientID) 
And then change your document.getElementID to use the "ddlClientID" parameter:
Expand|Select|Wrap|Line Numbers
  1.  var ivxtoken = document.getElementById(ddlClientID).options[document.getElementById(ddlClientID).selectedIndex].value;
But you'd need to change your server-side code that adds the call this this method to something like:

Expand|Select|Wrap|Line Numbers
  1. Button1.Attributes.Add("onclick","insertToken(this,'"+DropDownList1.ClientID+"'); return false")
-Frinny
Jun 29 '10 #3

Post your reply

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

Similar topics

reply views Thread by jeff | last post: by
3 posts views Thread by Patrick | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.