0

I have a javascript that gets the value of the cursor position and it works well. Am assigning that value to asp.net label's innerHtml property. When there s a treeview_selectednodechange event happening i want to access this innerHtml property in my program. How can this be achieved?

this is the javascript am using:-

function ShowSelection() {
        var txt1 = document.getElementById("MainContent_txtQuery");
        var currentRange = document.selection.createRange();
        var workRange = currentRange.duplicate();
        txt1.select();
        var allRange = document.selection.createRange();
        var len = 0;
        while (workRange.compareEndPoints("StartToStart", allRange) > 0) 
        {
            workRange.moveStart("character", -1);
            len++;
        }
        currentRange.select();
        document.getElementById("MainContent_lblPos").innerHTML = len;
    }

And the place where i want to access it is:-

 string[] selectedNode = treeViewTables.SelectedNode.Text.Split('<', '>');

            string pos = lblPos.Text;
            if (selectedNode[2].Equals("Table(s)") || selectedNode[2].Equals("Parameter(s)"))
            {
                return;
            }
            string parentNode = treeViewTables.SelectedNode.Parent.Text;

            if (parentNode.Contains("Table(s)"))
            {
                txtQuery.Text = txtQuery.Text + " " + selectedNode[2];
                txtQuery.Text = RemoveSpaces(txtQuery.Text);
            }
            else if (parentNode.Contains("Parameter"))
            {
                //if (txtQuery.Text != "")
                if (lblPos.Text == string.Empty)
                {
                    if (txtQuery.Text.Length == 0)
                    {
                        txtQuery.Text = selectedNode[2];
                    }
                    else if (txtQuery.Text[txtQuery.Text.Length - 1] != ',')
                    {
                        txtQuery.Text = txtQuery.Text + " " + "'" + selectedNode[2] + "'";
                        txtQuery.Text = RemoveSpaces(txtQuery.Text);
                    }
                    else
                    {
                        txtQuery.Text = txtQuery.Text + " " + selectedNode[2];
                        txtQuery.Text = RemoveSpaces(txtQuery.Text);
                    }
                }

            }
            else
            {
                txtQuery.Text = txtQuery.Text + " " + selectedNode[2] + ",";
                txtQuery.Text = RemoveSpaces(txtQuery.Text);
            }
            TreeNode nodeSelected = treeViewTables.Nodes[0];
            nodeSelected.Select();

please help.

Thank You

3 Answers 3

2

The content of a label (span on client side) will never be posted back to the server.

Add an <asp:HiddenField> and set the its value on client side, at the same time that you change the innerHtml of the label. This value will be automatically available on server side.

Sign up to request clarification or add additional context in comments.

Comments

2

DOM elements don't round trip to the server, only form elements. The only thing you will be able to access on the server in a Label is what was set on the server.

In order to do what you want, you are going to need to create a Hidden field and set the value of that to your coordinates so it will make it to the server on postback.

Comments

2

If I understand correctly, you are trying to set the content of a label control in javascript and then access it server side in ASP.NET using C#?

If that is what you are trying to do, you won't be able to do that as a label control is rendered as an HTML span element and isn't a form element. Only form elements get sent back to the server on postback (full or partial). You could set the same value to a hidden field and access it server side or you could use an AJAX call to pass it back to the server.

Also, it probably isn't a good idea to refer to rendered Client IDs in javascript directly. ie. rather than

document.getElementById("MainContent_txtQuery")

you'd be safer with

document.getElementById("<%=txtQuery.ClientID%>")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.