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.

Hyperlink a selected text from a specific textbox

P: 3
Hello,

In my page i have two textboxes and a html button. I'm using vb.net 2.0 . what i need is i want whatever text selected in the textbox1 to be hyperlinked when clicked the button... this is no problem.. i'm getting it easily.. But my problem is , if i select some text from the textbox2 and click the button it is also hyperlinked. i want only whatever text selected in the textbox1 to be hyperlnked and not in textbox2. Please tell me how to achieve this. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="SwapnaTestpage.aspx.vb" Inherits="SwapnaTestpage" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8.  
  9.     <script type="text/javascript" >
  10.     function InsertLink()
  11.     {
  12.         var    txt= document.getElementById("<%=textbox1.ClientID%>");
  13.  
  14.         if ((document.selection.createRange().text.length)>0)
  15.         {
  16.                 txt = document.selection.createRange();
  17.             txt.text = '<a href="http://www.google.com" target=_blank >' + txt.text + '</a>';             
  18.         }
  19.         else
  20.         {
  21.             alert("Please select text from textbox1 to insert hyperlink.");
  22.         }    
  23.     }
  24.     </script>
  25. </head>
  26. <body>
  27.     <form id="form1" runat="server">
  28.     <table>
  29.     <tr id="rwBody" runat="server">
  30.             <td  align="left" colspan="2" style="width: 550px" >
  31.                 <asp:TextBox ID="textbox1" runat="server" Height="216px" TextMode="MultiLine" Width="527px" CausesValidation="True"></asp:TextBox>
  32.  
  33.             </td>
  34.         </tr>
  35.         <tr id="rwHPHeading" runat="server">
  36.             <td align="left" colspan="2" style="height: 40px; width: 550px;">
  37.                 <asp:TextBox ID="textbox2" runat="server" CausesValidation="True" Height="30px"
  38.                     MaxLength="100" TextMode="MultiLine" Width="527px"></asp:TextBox></td>
  39.         </tr>
  40.         <tr runat="server">
  41.             <td align="left" colspan="2" style="width: 550px; height: 40px">
  42.                 <input type="button" id="btnInsertLink" onclick="InsertLink()" value='Insert Link'  name="btnInsertLink" runat="server" /></td>
  43.         </tr>
  44.     </table>
  45.     </form>
  46. </body>
  47. </html>
  48.  
Dec 29 '08 #1
Share this Question
Share on Google+
2 Replies


P: 3
I got the Answer myself...

function InsertLink()
{
var txt= document.getElementById("<%=textbox1.ClientID%>");
txt.focus();
if ((document.selection.createRange().text.length)>0)
{
newRange = document.selection.createRange();
newRange.text = '<a href="http://www.google.com" target=_blank >' + newRange.text + '</a>';
}
else
{
alert("Please select text from textbox1 to insert hyperlink.");
}
}
Dec 31 '08 #2

P: 1
Thanks, but once I added the hyperlink, I want to show the text ie., Rich TExt with Hyperlink.

Is there any way to show rich text in Asp.net text box with out using third party component like Telerik etc.,?

Thanks.
Nov 1 '10 #3

Post your reply

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