Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to select a certain area of a TextBox control in asp.net WebForms application. The situation is that the textbox contains email addresses seperated by ;. I wrote a little MailValidator class that sorts out valid and invalid email addresses.

Now i want the wrong email address to be selected, so that the user can correct it right away. As i mentioned, I know which addresses are valid and which aren't.

How do I do that? I know how to select the whole text with .Focus() (works at least in IE, and that's enough) But how can I only select a certain area? Is TextBox.Text.Select<> the way to go? If so, can someone provide an example? I don't fully understand what .Select<> actually does.

share|improve this question
1  
TextBox.Text.Select<> is not for selecting perticular text inside a textbox. .Select<> is a projection operator that can be applied to any list or array. Since TextBox.Text returns a String i.e. a array of char type, so you are seeing Select<> in VS intellisence. It has nothing to do with selecting textbox data. So don't be cofused with that! –  Sanjeev Rai Jun 13 '13 at 7:16
    
Thanks. I already suspected that .Select<> has nothing to do with text selection. –  Mathew Jun 13 '13 at 7:38

1 Answer 1

up vote 0 down vote accepted

I think you need to use Javascript with this function, you just need to find the start and end of your selection.

function createSelection(field, start, end) {
    if( field.createTextRange ) {
      var selRange = field.createTextRange();
      selRange.collapse(true);
      selRange.moveStart('character', start);
      selRange.moveEnd('character', end);
      selRange.select();
      field.focus();
    } else if( field.setSelectionRange ) {
      field.focus();
      field.setSelectionRange(start, end);
    } else if( typeof field.selectionStart != 'undefined' ) {
      field.selectionStart = start;
      field.selectionEnd = end;
      field.focus();
    }
  }
share|improve this answer
    
Allright, so it's not possible with c#. Now I just have to figure out how to access javascript in asp.net (Yes, I am quite new to asp.net :) ) But I am sure that I will get there. Thanks for the Answer! I will report back later. –  Mathew Jun 13 '13 at 7:37
    
would that be something along the line: <asp:TextBox ID="txtAdditionalEmail" runat="server" TabIndex="130" Width="264px" CssClass="Feld2" onFocus="createSelection(frmTelefonzettel.txtAdditionalEmail.Text, 1, 5)" OnTextChanged="TextBox_TextChanged"></asp:TextBox> –  Mathew Jun 13 '13 at 8:18
    
Discregard this comment. The keyword 'this' was enough to tell the function which field i wanted to use. Thanks you very much for your help!! Question marked as answered. –  Mathew Jun 13 '13 at 8:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.