简体   繁体   English

如何在 ASP.NET 中的文本框上实现单击事件?

[英]How to implement a click event on textbox in ASP.NET?

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

I am using the code provided below but, could not get it working.我正在使用下面提供的代码,但无法使其正常工作。

C#: C#:

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

ASP.NET: 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" />

How to accomplish this?如何做到这一点?

Set AutoPostback="True" .设置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>

However, if you only want to toogle visility of a button, you really should considerate javascript.但是,如果您只想显示按钮的可见性,您真的应该考虑使用 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>

jQuery is the perfect solution for your problem. jQuery 是您问题的完美解决方案。 The code would be something like this:代码将是这样的:

$("#TextBox1").on("click",function(){$("#ButtonSearch").css("visibility", "visible");})

You include the script by adding <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the page and then you can add the piece of code above to within <script></script> tags.您可以通过将<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>到页面来包含<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> ,然后您可以添加该片段<script></script>标签内的代码。

You do not need to post back to the server to accomplish your job.您不需要回发到服务器来完成您的工作。 You can use client side onFocus event and javascript/jquery, for example.例如,您可以使用客户端onFocus事件和 javascript/jquery。

I know I used an input of type text, and you are using an ASP Control which posts on server, but here is a JSFiddle to get you on the right track: http://jsfiddle.net/Mmjtz/1/我知道我使用了文本类型的输入,而您使用的是在服务器上发布的 ASP 控件,但这里有一个 JSFiddle 可以让您走上正轨: http : //jsfiddle.net/Mmjtz/1/

$("<%= ButtonSearch.ClientID %>").click(function(){
$("#TextBox1").show():
});

In this code you need to pass fields ID which you want to visible on the click of button.在此代码中,您需要传递要在单击按钮时可见的字段 ID。

Put the textbox inside a div and use the div 's onClick event from codebehind.将文本框放在div并使用代码隐藏中的divonClick事件。 It's not what you asked but it works for me without any errors.这不是你问的,但它对我有用,没有任何错误。 Here is a javascript function to implement requested event:这是一个实现请求事件的javascript函数:

function toggleVisibility() 
{
document.getElementById('TextBox1').disabled = true;
            /*
            ...some other code...
            */
} 

And of course, you have to define your onclick event at the div definition after implementing this JS function.当然,你必须在实现这个 JS 函数后在div定义中定义你的onclick事件。

<div id="TBdiv" onClick="toggleVisibility()">
<asp:TextBox ID="TextBox1"..../>
</div>

IMPORTANT: Since you now disabled your TextBox from codebehind, you have to enable it in somewhere before you want to use it again.重要提示:由于您现在从代码隐藏中禁用了TextBox ,因此您必须在某处启用它,然后才能再次使用它。 Otherwise you will not see it while the page is running.否则在页面运行时您将看不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM