I've been building a custom journal application for my website which has a reply sub-app where visitors can post responses to my frequent posts.

In the response sub-app I want to be able to incorporate formatting buttons (bold, link, colour, etc) that will take selected text within the textbox and add the appropriate format codes on either end of it.

The problem I run into is, without incorporating javascript to manage the formatting buttons I can't seem to find the equivalent of textbox.SelectedText within ASP.Net to determine the selected component of the textbox for the parse/replace code required to make this happen via C#.

While I've been trying to keep the application 100% C#/ASP.Net I am curious if anyone has any suggestions on how to go about getting a SelectedText type replace where I can take, for example "visit my site" as a selected segment within the textbox and through a button within the page convert it to [l http://mysite.com]visit my site[/l]. For this example I would also need some sort of pop-up input field where the user could enter their URL.

I've seen some examples of how to (just using C#/ASP.Net) open a child window, enter a variable, and return the variable to the parent but I haven't yet found a way to work on only a selected portion of a textbox's content.

I haven't added any code here simply because this is a prospective addition to my app and not something that's currently coded and in place.

Any help would be appreciated :)

Ok, so I found a solution that works for a simple transfer like adding bold tags (yes, I had to delve into JavaScript to do it).

The problem I'm running into now is, how do I do the same kind of replace while using an input source such as the prompt() function?

function toLink() {
    var url_string = prompt("Enter fully formed URL here");
    if (url_string != "") {
        var ta = document.getElementById("msgBody");
        if (document.selection) {
            str = document.selection.createRange().text
            document.selection.createRange().text = "[l " + url_string + "]" + str + "[/l]";
            return true;
        }
        else if (ta.selectionStart) {
            var startPos = ta.selectionStart;
            var endPos = ta.selectionEnd;
            var str = ta.value.substring(startPos, endPos);
            ta.value = ta.value.substring(0, startPos) + "[l " + url_string + "]" + str + "[/l]" + ta.value.substring(endPos, ta.value.length);
            return true;
        }
        else {
            return false;
        }
    }
    else {
        var tb = document.getElementById("msgBody");
        if (document.selection) {
            str = document.selection.createRange().text
            document.selection.createRange().text = "[l]" + str + "[/l]";
            return true;
        }
        else if (ta.selectionStart) {
            var startPos = ta.selectionStart;
            var endPos = ta.selectionEnd;
            var str = ta.value.substring(startPos, endPos);
            ta.value = ta.value.substring(0, startPos) + "[l]" + str + "[/l]" + ta.value.substring(endPos, ta.value.length);
            return true;
        }
        else {
            return false;
        }
    }
}

This is what I'm trying to do and if I was doing it in C# it would work, taking an input and applying it to the processes below it. When I try to test it however, it gives me the input (prompt()) but doesn't give any results in my textbox.

Any thoughts?

Hehe... nevermind... I found my solution! :P

function toLink() {
    var url_string = prompt("Enter fully formed URL here","http://yourURL.com");
    while (!url_string) {
        continue;
    }
    if (url_string != "http://yourURL.com") {
        var tb = document.getElementById("msgBody");
        if (document.selection) {
            str = document.selection.createRange().text
            document.selection.createRange().text = "[l " + url_string + "]" + str + "[/l]";
            return true;
        }
        else if (tb.selectionStart) {
            var startPos = tb.selectionStart;
            var endPos = tb.selectionEnd;
            var str = tb.value.substring(startPos, endPos);
            tb.value = tb.value.substring(0, startPos) + "[l " + url_string + "]" + str + "[/l]" + tb.value.substring(endPos, tb.value.length);
            return true;
        }
        else {
            return false;
        }
    }
    else {
        var tb = document.getElementById("msgBody");
        if (document.selection) {
            str = document.selection.createRange().text
            document.selection.createRange().text = "[l]" + str + "[/l]";
            return true;
        }
        else if (tb.selectionStart) {
            var startPos = tb.selectionStart;
            var endPos = tb.selectionEnd;
            var str = tb.value.substring(startPos, endPos);
            tb.value = tb.value.substring(0, startPos) + "[l]" + str + "[/l]" + tb.value.substring(endPos, tb.value.length);
            return true;
        }
        else {
            return false;
        }
    }
}
commented: Good work :) +31
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.