2013-02-13 41 views
0

我有一个标签,声明如下;单击链接时在标签上打印值

<asp:Label ID="Label1" runat="server" Text=" "></asp:Label> 

此外,我有链接声明如下;

<a> Hello </a>

当链路Hello用户点击我需要的文本Hello复制到上面定义的标签。我怎样才能做到这一点 ?

+0

从来没有用过ASP.NET ..但是JavaScript呢...... – AmazingDreams 2013-02-13 08:37:55

回答

1

这种简单的JavaScript的工作:

<asp:Label ID="Label1" runat="server" Text="lol"></asp:Label> 
<a id="myLink" onclick="linkClick()"> Hello </a> 

<script type="text/javascript" language="javascript"> 
    function linkClick() { 
     var value = document.getElementById('myLink').innerText; 
     document.getElementById('<%= Label1.ClientID %>').innerText = value; 
    } 
</script> 

或者作为Devang Rathod建议您可以使用jQuery。

1

您可以使用jquery。但是你必须在你的页面上使用jquery.js。

$(document).ready(function(){ 
    $('a').click(function() { 
    $("#Label1").attr('Text',$("a").text()); 
    }); 
}); 
1

标记:

<asp:Label ID="Label1" runat="server" Text=" "></asp:Label> 
<asp:LinkButton id="button" runat="server" Text="Hello" onClick="button_onclick" /> 

后面的代码:

protected void button_onclick(Object sender,EventArgs e) 
{ 
    Label1.Text = button.Text; 
}