Apoorva
Apoorva

Reputation: 1047

Access the value innerHtml value of asp.net Label programmatically in c#

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

Upvotes: 0

Views: 3618

Answers (3)

wizzardmr42
wizzardmr42

Reputation: 1644

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%>")

Upvotes: 2

JotaBe
JotaBe

Reputation: 39015

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.

Upvotes: 2

Josh
Josh

Reputation: 44906

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.

Upvotes: 2

Related Questions