473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fill text in textbox by dropdown only when clicking on button

2 New Member
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 3110
ThatThatGuy
449 Recognized Expert Contributor
@polluxsoftech
you can paste the same in ascx markup page...
Jun 29 '10 #2
Frinavale
9,735 Recognized Expert Moderator Expert
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.getEle mentById() 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.getEle mentById('DropD ownList1').opti ons[document.getEle mentById('DropD ownList1').sele ctedIndex].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.getEle mentID to use the "ddlClientI D" 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

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

Similar topics

0
1376
by: Mark Donners | last post by:
I have access 2002 database I like to fill a record field in a table with a text when i click a button I have two tables say table1 and table2. I have a form based on table1, it has a subform showing table2. When i click a button i like to fill a field in table 2 with text "test" . How can i do this?
0
2004
by: jeff | last post by:
is there a way to display an hourglass for the client's cursor when clicking a button running server-side code? thanks.
2
3156
by: Joey | last post by:
Hi There, I am trying to get the selected value of a listbox when I click a button, everything works ok and I can bind the list and when I have a basic page and click a button to invoke a sub it works fine, but when I try to place it in the sub I want to use it give me an eror saying that the "Object reference not set to an instance of an...
4
2082
by: Dale Levesque | last post by:
I get this message. any ideas?? See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. ************** Exception Text ************** System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0,...
2
3195
by: André | last post by:
Hi, I made a detailsview containing several fields for inserting only. I also made a dropdownlist which is invisible at start. Now, when clicking in a particular textbox, the dropdownlist must become visible (in order to put the selected value of the dropdownlist into the textbox of the detailsgrid.). I must say, the detailsview contains...
4
2184
by: tadisaus2 | last post by:
Hello, I am a newbie with Javascript. I have this question. I have created a form with 3 radio buttons, (male, female, other), whenever a user click on other, I like to have the text box enabled (it is disbled by default, greyed out). <input type="radio" name="gender" id="m" value="male" />Male <input type="radio" name="gender" id="f"...
10
3690
by: pechar | last post by:
Hi all, I created a test web application in asp.net. It works like a dream in IE but when it gets to Firefox I get an exception as follows ----------------------------------------------------------------------------------------- "System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages...
3
1849
by: Patrick | last post by:
Hi, Newbish on JS here. Found this script on the web and it does what I want in FF, but works oddly in IE. When I check the box in IE nothing happens, but when I click on the body after the box is checked it displays the dropdown. Any ideas on how to make this work correctly in IE (displaying the dropdown when box checked)? Thanks for your...
2
3758
by: PHPstarter | last post by:
As the title says, how would one go by doing this? One button, one main image. Click the button and the image will change into another specified. And there could be more than just 2 images (starting image and onClick image) Thanks in advance.
3
2012
by: codejason | last post by:
Hello, I am new to asp.net and web applications, but have reasonable experience in winforms. I have a webcontrol where I have 4 asp.net textbox controls. When I click on these textboxes (especially when I click on them successively) my browser will freeze occasionally and become non responsive for a second -> many seconds. I do not...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.