如何在asp.net中实现onChange事件 [英] how to implement onChange event in asp.net

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

问题描述

大家好...
我正在使用C#开发一个asp.net应用程序. 我希望文本框的值应根据另一个文本框的值自动填充.....

请帮助.....

hello all...
i am developing a asp.net application using C#..
i want the value of the text box should be filled automatically according to the value of another text box........

Please Help........

推荐答案

我建​​议您花一些时间来学习和理解基本知识.

我希望文本框的值应根据另一个文本框的值自动填充.....

最简单的选择是启用TextBox的AutoPostBack并处理其TextChanged事件.

该代码将类似于:

I would suggest you to give some time to learn and understand the basics.

i want the value of the text box should be filled automatically according to the value of another text box........

The simplest option is to enable AutoPostBack of the TextBox and handle it''s TextChanged event.

The code will be something like:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ontextchanged="TextBox1_TextChanged"></asp:TextBox>



而在后面的代码中,它将是:



And in the code behind, it will be like:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    //assign value to another text box here.
}



如果需要一些数据库交互或强制性的服务器端处理,建议使用此方法.
如果可以在客户端处理这些值,我建议使用JavaScript进行相同的操作,因为这样可以避免回发页面.

我上面建议的方法也可以使用AJAX完成.

希望对您有所帮助!



This method is recommended if you need some database interaction or compulsory server side processing.
If the values can be processed on the client side, I recommend using JavaScript to do the same because this avoids a page post back.

The method I suggested above can also be done using AJAX.

Hope this helps!


我建​​议您为此使用javascript
崩溃
I would like to suggest you use javascript for this
Collapse
<asp:textbox id="TextBox1" runat="server" >
<asp:textbox id="TextBox2" runat="server" >



现在在page_load上,在下面写一行



and now on page_load write the line below

TextBox1.Attributes.Add("onblur","ManipulateValue(this)")


然后在javascript
中进行处理


and then handle it in javascript

<script language="javascript">
function ManipulateValue(txt)
{
var text2=document.getElementById("<%=TextBox2.ClientID %>");
text2.value=txt.value;
}
</script>


在这里,我将textbox1的值分配为2,但是您可以对此执行一些任务.

这样可以避免在服务器端代码上发生回发..

--Pankaj


here i assign the value of textbox1 to 2 but you can perform some task on that.

This will avoid postback which will be happen on server side code..

--Pankaj


请参阅以下非常简单的 ^ ]-肯定会为您提供帮助.
See this very simple example[^] - it will surely help you out.


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

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