I have this asp:TextBox
我有这个asp:TextBox
<asp:TextBox ID="usernameInput" runat="server"
AutoPostBack="true" OnTextChanged="NameChanged"></asp:TextBox>
And would like it to post back to my CodeBehind
in C# whenever the text inside the box changes. I am aware that I need to unfocus the textbox in order to fire the event. I have the feeling OnTextChanged
should not be in the asp:TextBox
markup but seeing this done plenty in examples.
并希望每当文本框内的文字发生变化时,都会在C#中回复我的CodeBehind。我知道我需要对文本框进行疏散以解雇事件。我觉得OnTextChanged不应该在asp:TextBox标记中,但在示例中看到这样做很多。
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Adding the event handler manually does not help either.
usernameInput.TextChanged += new EventHandler(NameChanged);
}
public void NameChanged(Object sender, EventArgs e)
{
Debug.WriteLine("Text Changed"); //Having a breakpoint here that has never been reached.
}
}
So why is it not firing once the box looses focus and it's value has changed?
那么为什么一旦盒子失去焦点并且它的价值发生了变化,它就不会被解雇?
aspx header:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="~/Index.aspx.cs" Inherits="TestProject.Index" %>
This is the complete form:
这是完整的表格:
<form id="signIn" runat="server">
<p>Username</p>
<asp:TextBox ID="usernameInput" runat="server" AutoPostBack="true" CausesValidation="false" OnTextChanged="usernameInput_TextChanged"></asp:TextBox>
<p>Password</p>
<asp:TextBox ID="passwordInput" TextMode="Password" runat="server" />
<p>E-mail</p>
<asp:TextBox ID="emailInput" TextMode="Email" CausesValidation="false" runat="server" /><br /><br />
<asp:CheckBox ID="termsInput" runat="server" /><span style="font-size:10px;">I accept the Terms and Conditions.</span><br /><br />
<asp:Button ID="submit" Text="PLAY" OnClick="ProcessSignIn" runat="server" />
</form>
Current event handler:
当前事件处理程序
public void usernameInput_TextChanged(object sender, EventArgs e)
{
Debug.WriteLine("Text changed!");
}
With this setup the event handler does fire when the submit button is clicked. It seems like it is acting as if AutoPostBack
is set to false.
使用此设置,单击提交按钮时会触发事件处理程序。看起来好像AutoPostBack设置为false。
3 个解决方案
#1
Move your handler assignment to Page Init.
将处理程序分配移至Page Init。
protected void Page_Init()
{
usernameInput.TextChanged += new EventHandler(NameChanged);
}
OR Add this in the aspx Markup for Your TextBox
或者在您的TextBox的aspx标记中添加它
OnTextChanged="NameChanged"
#2
ASP.NET
usernameInput.Attributes.Add("onchange","javascript:usernameInput_changeHandler()");
Javascript
<script type="text/javascript">
function usernameInput_changeHandler()
{
//client side JS code
}
</script>
Update#1
If you need to call your C# method, then you will need to defined that as a WebMethod
如果需要调用C#方法,则需要将其定义为WebMethod
CS File
[WebMethod]
public void FunctionName()
{
//Your .NET code
}
ASPX File
<script type="text/javascript">
function usernameInput_changeHandler()
{
PageMethods.Name(SuccessFunction, FailureFunction);
}
function SuccessFunction(result) {
alert(result);
}
function FailureFunction(error) {
alert(error);
}
</script>
...
<asp:TextBox ID="usernameInput" runat="server" AutoPostBack="true" CausesValidation="false" OnClientClick="usernameInput_changeHandler();return false;"></asp:TextBox>
#3
I found the correct answer but this gave me a serious head injury.
我找到了正确答案,但这给了我严重的头部伤害。
I could not have a asp:Button
with an ID
named submit
, it is probably reserved and throwing an error somewhere. Would love a better explanation.
我没有一个asp:带有名为submit的ID的按钮,它可能是保留的并且在某处抛出错误。会喜欢更好的解释。
I started a very basic test implementation and that worked. Luckily when I was rebuilding my code I didn't add the button yet and once I did with the exact same value for ID
things stopped working. Once I changed the ID
I started getting call backs when losing focus on the TextBox
.
我开始了一个非常基本的测试实现,并且有效。幸运的是,当我重建我的代码时,我还没有添加按钮,一旦我使用完全相同的ID值就停止了工作。一旦我更改了ID,我就开始在失去对TextBox的关注时回调。
#1
Move your handler assignment to Page Init.
将处理程序分配移至Page Init。
protected void Page_Init()
{
usernameInput.TextChanged += new EventHandler(NameChanged);
}
OR Add this in the aspx Markup for Your TextBox
或者在您的TextBox的aspx标记中添加它
OnTextChanged="NameChanged"
#2
ASP.NET
usernameInput.Attributes.Add("onchange","javascript:usernameInput_changeHandler()");
Javascript
<script type="text/javascript">
function usernameInput_changeHandler()
{
//client side JS code
}
</script>
Update#1
If you need to call your C# method, then you will need to defined that as a WebMethod
如果需要调用C#方法,则需要将其定义为WebMethod
CS File
[WebMethod]
public void FunctionName()
{
//Your .NET code
}
ASPX File
<script type="text/javascript">
function usernameInput_changeHandler()
{
PageMethods.Name(SuccessFunction, FailureFunction);
}
function SuccessFunction(result) {
alert(result);
}
function FailureFunction(error) {
alert(error);
}
</script>
...
<asp:TextBox ID="usernameInput" runat="server" AutoPostBack="true" CausesValidation="false" OnClientClick="usernameInput_changeHandler();return false;"></asp:TextBox>
#3
I found the correct answer but this gave me a serious head injury.
我找到了正确答案,但这给了我严重的头部伤害。
I could not have a asp:Button
with an ID
named submit
, it is probably reserved and throwing an error somewhere. Would love a better explanation.
我没有一个asp:带有名为submit的ID的按钮,它可能是保留的并且在某处抛出错误。会喜欢更好的解释。
I started a very basic test implementation and that worked. Luckily when I was rebuilding my code I didn't add the button yet and once I did with the exact same value for ID
things stopped working. Once I changed the ID
I started getting call backs when losing focus on the TextBox
.
我开始了一个非常基本的测试实现,并且有效。幸运的是,当我重建我的代码时,我还没有添加按钮,一旦我使用完全相同的ID值就停止了工作。一旦我更改了ID,我就开始在失去对TextBox的关注时回调。