I have an asp.net button which on click gets a value from a text box.

I want to then look up a value from the database and insert the value into a ajax html editor as '<img src='the looked up value'/>' at the point where the cursor currently is.

It is my understanding that to do this i need to use javascript.

I also understand that my problem is that the javascript will fire before the server code i thought if on server code I could register the javascript and then fire it once registered but im stuck on the last part.

am i going down the right line?

Code:
protected void btnInsert_Click(object sender, EventArgs e)
    {
        string imagelink = this.txtImageID.Text;

        //Go Lookup actual URL from Umbraco
       
        // Define the name and type of the client script on the page.
        String csName = "ButtonClickScript";
        Type csType = this.GetType();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Check to see if the client script is already registered.
        if (!cs.IsClientScriptBlockRegistered(csType, csName))
        {
            StringBuilder csText = new StringBuilder();
            csText.Append("<script type=\'text/javascript\'>");
            csText.Append("var editor = this._designPanel;");
            csText.Append("var sel = editor._getSelection();");
            csText.Append("var range = editor._createRange(sel);");
            csText.Append("var parent = Sys.Extended.UI.HTMLEditor.getSelParent(editor); function DoClick() {");
            csText.Append("if (Sys.Extended.UI.HTMLEditor.isIE) {");
            csText.Append("var original = range.htmlText;");
            csText.Append("if (original != '')");
            csText.Append("range.pasteHTML('<img src='" + imagelink + "'></img>');");
            csText.Append("}");
            csText.Append("else {");
            csText.Append("var edittext = sel.getRangeAt(0);");
            csText.Append("if (edittext.toString() != '') {");
            csText.Append("var h = document.createElement('img');");
            csText.Append("edittext.surroundContents(h);");
            csText.Append("}");
            csText.Append("</");
            csText.Append("script>");
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
        //Now somehow raise an event which will cause the javascript to fire
        }