在c#中以编程方式访问asp.net Label的value innerHtml值

时间:2012-06-08 14:31:04

标签: c# javascript asp.net

我有一个获取光标位置值的javascript,效果很好。我将该值赋给asp.net label的innerHtml属性。当发生treeview_selectednodechange事件时,我想在我的程序中访问这个innerHtml属性。如何实现这一目标?

这是我正在使用的javascript: -

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;
    }

我想要访问它的地方是: -

 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();

请帮忙。

谢谢

3 个答案:

答案 0 :(得分:2)

DOM元素不会往返于服务器,只是表单元素。您可以在Label中的服务器上访问的唯一内容就是在服务器上设置的内容。

为了做你想做的事,你需要创建一个Hidden字段并将其值设置为你的坐标,这样它就会在回发时进入服务器。

答案 1 :(得分:2)

标签的内容(客户端的跨度)永远不会回发到服务器。

添加<asp:HiddenField>并在客户端设置其值,同时更改标签的innerHtml。该值将在服务器端自动提供。

答案 2 :(得分:2)

如果我理解正确,你试图在javascript中设置标签控件的内容,然后使用C#在ASP.NET中访问它的服务器端?

如果您正在尝试这样做,那么您将无法做到这一点,因为标签控件呈现为HTML span元素而不是表单元素。只有表单元素才会在回发时(全部或部分)发送回服务器。您可以将相同的值设置为隐藏字段并访问服务器端,也可以使用AJAX调用将其传递回服务器。

此外,直接在javascript中引用呈现的客户端ID可能不是一个好主意。即。而不是

的document.getElementById( “MainContent_txtQuery”)

你会更安全

的document.getElementById( “&LT;%= txtQuery.ClientID%&gt;” 中)