如何在ASP.NET中的文本框上实现click事件? [英] How to implement a click event on textbox in ASP.NET?

查看:107
本文介绍了如何在ASP.NET中的文本框上实现click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我需要一个功能,以便当用户单击文本框输入值时,它应该使按钮和其他字段可见?

In my web application I need a functionality so that when users click on textbox to input values, it should make the button and the other fields visible?

我正在使用下面提供的代码,但无法正常工作.

I am using the code provided below but, could not get it working.

C#:

protected void TextBox1_Click(object sender, EventArgs e)
{
    ButtonSearch.Visible = true;
}

ASP.NET:

<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" Visible="False" />

如何做到这一点?

推荐答案

设置 AutoPostback ="True" .这样,事件将在服务器端触发,并且按钮将变为可见.

Set AutoPostback="True". This way the event will be fired server-side, and the button will become visible.

<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"     OnClick="TextBox1_Click" AutoPostBack="true"></asp:TextBox>

但是,如果您只想切换按钮的可见性,则应该考虑使用javascript.这样可以节省返回服务器的行程.

However, if you only want to toogle visility of a button, you really should considerate javascript. This will save a trip back to the server.

<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>

<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" style="display:none;" />

<script type="text/javascript">
    function txtBox1_ClientClicked(){
        var theButton = document.getElementById('<%=ButtonSearch.ClientID%>');
        theButton.style.display = 'block';
    }
</script>

这篇关于如何在ASP.NET中的文本框上实现click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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