I have a little problem with case change in ASP.NET webforms. What I have in my code, converts the whole entered text to uppercase, I want just a piece of the text to be converted. For example if I enter John Doe, the code bellow converts it to JOHN DOE. I need a result when I select only John to return JOHN Doe, this should work similar like a text editor. If anyone has a clue if this could be done in ASP.NET webforms I would be very grateful. Thanks in advance.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="UpperCase" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Style.Add("text-transform", "uppercase");
}
Since you have a button click, and can (we assume) are going to run code, then this will work:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string[] sParts = TextBox1.Text.Split(' ');
sParts[0] = sParts[0].ToUpper();
TextBox1.Text = string.Join(" ",sParts);
}
}
So, above takes the text to the first found space. Converts to upper case, and then shoves the results back into the text box.
FYI: The use of single quote and double quotes as per above is required for this to work.
So, we want to allow the user to select some text in a box, and then convert to upper case.
So say we drop in a text box - lets go with multi-line, and alow the user to type in some text, and select some text, and then convert the selected text to upper case.
Thus this markup:
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
Height="238px" Width="492px" ClientIDMode="Static" >
</asp:TextBox>
<br />
<br />
<asp:Button ID="cmdUpper" runat="server" Text="Convert selected text to uppper"
CssClass="btn"
OnClientClick="MyUpper();return false" />
<script>
function MyUpper() {
tBox = $('#TextBox1')
selStart = tBox[0].selectionStart - 1
selEnd = tBox[0].selectionEnd
myUpper = tBox.val().substring(selStart, selEnd)
myUpper = myUpper.toUpperCase()
myResult = tBox.val().substring(0, selStart) + myUpper +
tBox.val().substring(selEnd)
tBox.val(myResult)
}
</script>
So, we can now select some text, and hit the button, it will convert the text to upper case.
Eg this:
I of course assumed jQuery is available.