Click here to Skip to main content
15,922,529 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I need to replace a text by another in a textBox in asp.net c# 2.0 can anyone help ...
It´s very important for me........

Thanks .........
Posted
Comments
Ricardo Vela 25-May-11 13:41pm    
I need replace a selected word in the text of textBox not all text
I can´t use SelectedText or SelectionLength like windows Form in asp.net

sorry for my english.
Ricardo Vela 25-May-11 13:55pm    
I do not know that word is selected.Is there any method that returns just the selected part of text written in a textBox?
yesotaso 25-May-11 13:58pm    
Rephrase: "How can I replace selected portion of a text in textbox with an arbitrary value in ASP.NET?"
If I were you I'd look for JavaScript solutions as well. Of course that depends on your need but that is not quite clear atm.
Ricardo Vela 25-May-11 14:01pm    
have you a JavaScript for this solution..???
yesotaso 25-May-11 14:07pm    
Checkout:
http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript
or
Google : javascript replace selected text textbox

You can use string.Replace().

Its a good one.
 
Share this answer
 
Well unless I have understood the question wrong, I would do text1.text= newText;.
 
Share this answer
 
I have the solution and work in IE,Firefox and chrome

this funtion remplace a selected string in textbox

example:

x=hello (the selected text in textbox)
v1=<strong>
v2=</strong>

<input type="button" value="Bold" onclick="remplace('<strong>','</strong>')" /> (input button)

C#
function remplace(v1,v2)
   {
   
    var elementRef = document.getElementById("IDofTextBox");

     // IE ...
    if (document.selection) {
             str = document.selection.createRange().text
             document.selection.createRange().text = v1 + str + v2;
     }
     else if ((elementRef.selectionStart) || (elementRef.selectionStart == '0'))
     {
       // Mozilla/Netscape...

             var startPos = elementRef.selectionStart;
             var endPos = elementRef.selectionEnd;
             var str = elementRef.value.substring(startPos, endPos);
             elementRef.value = elementRef.value.substring(0, startPos) + v1 + str + v2 + elementRef.value.substring(endPos, elementRef.value.length);
     }
     else
      {
       elementRef.value += valueToInsert;
      }
   }



the result <strong>x</strong> inside of textBox

If you use master.page and add this code in a asp:Content you need this function to find the real Id of the control

C#
function RealID(id)
   {
       var strId="";
       var elm="";
       for(i = 0; i < document.forms[0].elements.length; i++)
       {
           elm = document.forms[0].elements[i];
           if (elm.id.indexOf(id)!=-1)
           {
           strId=elm.id;
           return(strId);
           }
       }
   }
 
Share this answer
 
C#
TextBox1.Text = "ABCABC"
string toReplace = "D";
string toFind = "B";
string Results = TextBox1.Text.Replace(toFind, toReplace);

//Results == "ADCADC";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900