Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中经过一段时间后从代码隐藏中清除标签文本?_C#_Asp.net - Fatal编程技术网

C# 如何在asp.net中经过一段时间后从代码隐藏中清除标签文本?

C# 如何在asp.net中经过一段时间后从代码隐藏中清除标签文本?,c#,asp.net,C#,Asp.net,单击按钮后,我将为标签指定一些文本,如下所示: protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Hello"; } 经过一段时间,比如说30秒,我想清除标签文本。 有人能帮我吗? 我尝试了下面的脚本,但它不适合我 $(document).ready(function() { $('#Label1').fadeOut(3000, function() { $(thi

单击按钮后,我将为标签指定一些文本,如下所示:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Hello";
}
经过一段时间,比如说30秒,我想清除标签文本。 有人能帮我吗? 我尝试了下面的脚本,但它不适合我

$(document).ready(function() {
    $('#Label1').fadeOut(3000, function() {
        $(this).html(""); //reset the label after fadeout
    });
});
我的设计

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        $(document).ready(function() {
        $('#<%= myLabel.ClientID %>').fadeOut(3000, function() {
                $(this).html(""); //reset the label after fadeout
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label id="myLabel" runat="server"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

如果这是Asp.NETWebApplication,我认为您应该使用

$(document).ready(function() {
    $('#<%= Label1.ClientID %>').fadeOut(3000, function() {
        $(this).html(""); //reset the label after fadeout
    });
});

为了在指定的时间后运行某些代码,您需要将代码包装在ready handler中

setTimeout(code, timeInMsAfterWitchToRunCode)
打电话。你正在做的是在3秒内立即淡出。您正在传递的3000ms是淡出函数的参数,告诉它淡出应该需要多长时间

这里有一个工作示例:

请注意,对于ASP.NET应用程序,您应该使用:

$('#<%= Label1.ClientID %>')

选择标签。这是因为只有在运行时控件设置了runat=server属性时,您才知道控件的id。

请注意,如果您在下一次回发时不清除标签的文本,您将在每次回发时再次收到相同的消息。在我看来,最好用客户端代码显示标签文本,而不是清除它。或者,您可以在每次加载页面时清除标签文本。

即使它对我不起作用,但您可以发布如何定义标签,我做了一个测试,效果很好。可能不在页面中包含jquery?``我的标签声明首先在您的页面中包含jquery。然后看看我的答案。我可以有这个Jquery链接吗?你说的Jquery链接是什么意思?您是在问如何在页面中包含jQuery吗?您可以下载它或使用CDN服务器。这里是关于脚本的信息,我应该包括一些像你刚刚输入的东西!将src更改为获取jquery的位置。你在上面的评论中查到我的链接了吗。