Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<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
C#
protected void Txt_TextChanged1(object sender, EventArgs e)
    {
        Lbl.Text = Txt.Text;
    }

But this was also not working
Posted
Comments
KaushalJB 4-Mar-15 4:32am    
Unclear !! Unclear !! Unclear !!
Member 11111143 4-Mar-15 4:40am    
No there is no error but it is not showing me the label value into textbox
Member 11111143 4-Mar-15 5:58am    
why the label value is not shown in the the textbox is there any javascript error
[no name] 4-Mar-15 4:34am    
Do u got any error? put a break point and try again.
Member 11111143 4-Mar-15 4:39am    
No there is no error but it is not showing me the label value into textbox

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:
XML
<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>
 
Share this answer
 
v2
Comments
Member 11111143 4-Mar-15 7:37am    
Ya now its working but I want this to happen without a button is there any way
Peter Leow 4-Mar-15 8:32am    
Then how and when do you want the function to run?
Member 11111143 4-Mar-15 23:23pm    
Actually I am using session and the label shows the branch login so I want to copy The branch login into the textbox without the button click event.
Peter Leow 5-Mar-15 0:22am    
OK. Comment away some lines and call the function at the end of page load,
//document.getElementById("L2T").addEventListener("click", function () {
labelToText("<%= Lbl.ClientID %>", "<%= Txt.ClientID %>");
//});
Member 11111143 5-Mar-15 1:02am    
I tried working this out like this to test
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Lbl" runat="server" Text="start">
<asp:TextBox ID="Txt" runat="server">

</div>
</form>
<script type = "text/javascript">
document.getElementById("L2T").addEventListener("click", function () {
labelToText("<%= Lbl.ClientID %>", "<%= Txt.ClientID %>");
});
I tested this but this is not working.I'm somewhere wrong pls tell me
You have written many events in JavaScript as well as in Server side. But nothing is invoking these events.
JavaScript
document.getElementById("L2T")
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.
 
Share this answer
 
Comments
Member 11111143 4-Mar-15 7:35am    
What Shall I Do I am not understanding such a simple thing. The Java script is not working also.
So, tell me first. What exactly you need to do? On which event you want to fill the textbox?
Member 11111143 4-Mar-15 8:22am    
when the user Logs in the user name is shown in the label username, I want that username(label) to copy it in a textbox(username)
That means you want to put that as a placeholder. No need to do all these stuff.

See - HTML input placeholder Attribute. This will work with all modern browsers.

If you want to support old browsers, then you can do like it is shown - here by implementing a onfocus event.
You have missed to make few changes.
Replace this-
ASP.NET
<asp:textbox id="Txt" runat="server" ></asp:textbox>

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



Hope, it helps :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900