将标签值放入textboxe [英] Putting label value into textboxe

查看:82
本文介绍了将标签值放入textboxe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type = "text/javascript">
        function labelToText(lblConversionFactor, txtConversionFactor) {
            document.getElementById(txtConversionFactor).value =
                document.getElementById(lblConversionFactor).innerHTML;
        }
        document.getElementById("L2T").addEventListener("click", function () {
            labelToText( "Txt","Lbl");
        });
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Lbl" runat="server" Text="UserName"></asp:Label>
        <asp:TextBox ID="Txt" runat="server"></asp:TextBox>
    </div>

    </form>
</body>
</html>



我尝试过使用


I have tried using

protected void Txt_TextChanged1(object sender, EventArgs e)
    {
        Lbl.Text = Txt.Text;
    }



但这也不起作用


But this was also not working

推荐答案

当然,有错误:

1.在没有L2T的地方,你附加了一个点击事件监听器,它无处可寻。我假设它是一个按钮。

2.在创建L2T之前声明了addEventListener,因为它是在加载假想的L2T之前放置的。

3 。不要在asp.net中使用静态id,因为运行时间id可能不同,请参考: ASP.NET 4.0客户端ID功能 [ ^ ]

让我们看看正确的版本:

Definitely, there are errors:
1. Where is no "L2T", you are attaching a click event listener to it and it is no where to be found. I assume it is a button.
2. The addEventListener was declared before the L2T was created as it is placed before the loading of supposedly a L2T in the body.
3. Do not use static id in asp.net as the run time id could be different, refer: ASP.NET 4.0 Client ID Feature[^]
Let's see the correct version:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Lbl" runat="server" Text="UserName"></asp:Label>
        <asp:TextBox ID="Txt" runat="server"></asp:TextBox>
        <asp:Button ID="L2T" runat="server" Text="Button" />
    </div>

    </form>
        <script type = "text/javascript">
            function labelToText(lblConversionFactor, txtConversionFactor) {
                document.getElementById(txtConversionFactor).value =
                    document.getElementById(lblConversionFactor).innerHTML;
            }
            document.getElementById("L2T").addEventListener("click", function () {
                labelToText("<%= Lbl.ClientID  %>", "<%= Txt.ClientID  %>");
            });
        </script>
</body>
</html>



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




++++++++++[Round 2}+++++++++++++++

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Lbl" runat="server" Text="UserName"></asp:Label>
        <asp:TextBox ID="Txt" runat="server"></asp:TextBox>
    </div>
 
    </form>
        <script type = "text/javascript">
            function labelToText(lblConversionFactor, txtConversionFactor) {
                document.getElementById(txtConversionFactor).value =
                    document.getElementById(lblConversionFactor).innerHTML;
            }
                
            labelToText("<%= Lbl.ClientID  %>", "<%= Txt.ClientID  %>");

        </script>
</body>
</html>


您已在 JavaScript 以及服务器端编写了许多事件的。但是没有任何东西在调用这些事件。

You have written many events in JavaScript as well as in Server side. But nothing is invoking these events.
document.getElementById("L2T")

没有ID L2T



Txt_TextChanged1 未在标记中定义,那么它将如何调用该事件?没有什么可以自动工作,你必须告诉他们这样做。

There is no such element with ID "L2T".

Txt_TextChanged1 is not defined in the markup, so how it will invoke the event? Nothing work automatically, you have to tell them to do.


你错过了一些改变。

替换它 -

You have missed to make few changes.
Replace this-
<asp:textbox id="Txt" runat="server" ></asp:textbox>



with


with

<asp:textbox id="Txt" runat="server" autopostback="True" ontextchanged="Txt_TextChanged1" ></asp:textbox>

< br $>




希望,它有帮助:)




Hope, it helps :)


这篇关于将标签值放入textboxe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆