0

我要做的是根据已输入文本框中的字符数启用或禁用特定按钮。

该事件仅在我单击文本框时触发,也许我正在寻找其他东西?

<telerik:RadTextBox ID="InputVinInformationTextBox" runat="server"
    Skin="Office2010Blue"
    Width="250px"
    MaxLength="16"
    OnTextChanged="InputVinInformationTextBox_OnText"
    AutoPostBack="true">
</telerik:RadTextBox>

protected void InputVinInformationTextBox_OnText(object sender, EventArgs e)
{
    if (InputVinInformationTextBox.Text.Length >= 8)
    {
        VinSubmitButton.Enabled = true;
    }
    else
    {
        VinSubmitButton.Enabled = false;
    }
}
4

2 回答 2

0

如果您不介意使用 jQuery,您可以删除该OnTextChanged函数并将该功能绑定到keyup事件。您需要做的就是在<head>标签中添加以下内容:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    $(document).ready(function() {
        $("#<%=InputVinInformationTextBox.ClientID%>").keyup(function() {
            if (InputVinInformationTextBox.Text.Length >= 8)
            {
               VinSubmitButton.Enabled = true;    
            }
            else
            {
                VinSubmitButton.Enabled = false;
            }
        }
    });
</script>

我不确定您的 VinSubmitButton 是什么,但我假设,因为它在您的示例中有效,所以这是您已经存储的元素。

于 2012-07-26T19:55:17.497 回答
0

此行为是设计使然:

所有 RadInput 控件都提供 TextChanged 服务器事件,该事件在 AutoPostBack 属性设置为 true、用户键入有效条目并且输入失去焦点时引发。

仅当输入控件的值实际更改时才会发生 TextChanged 事件。如果用户更改了输入控件中的字符串,但实际上并未更改值...则不会发生 TextChanged 事件。

有关详细信息,请参阅 Telerik 网站上的此帮助主题。

我建议OnTextChanged您像以前一样使用该事件,因为它只会在输入更改之前的值的有效文本时触发RadTextBox

或者,您可以使用 JavaScript 确定何时输入了适当数量的字符,然后手动触发请求。

<telerik:RadTextBox ID="InputVinInformationTextBox" runat="server"
    Skin="Office2010Blue"
    Width="250px"
    MaxLength="16"
    OnLoad="InputVinInformationTextBox_OnLoad">
    <ClientEvents OnKeyPress="InputVinInformationTextBox_OnKeyPress" />
</telerik:RadTextBox>


<script type="text/javascript">
    function InputVinInformationTextBox_OnKeyPress(sender, args) {
        var text = sender.get_value();
        if (text.length >= 8) {
            var ajaxManager = $find('<%= RadAjaxManager.GetCurrent(Page) %>');
            ajaxManager.ajaxRequestWithTarget(sender.get_id(), '');
        }
    }
</script>

上面的代码假设您也在RadAjaxManager页面上使用 a 并且它已被配置为在由RadTextBox. 您需要在OnLoad控件事件期间(或以后)检查 RadTextBox 值,以便为控件初始化所有 ViewState 和表单值。

protected void InputVinInformationTextBox_OnLoad(object sender, EventArgs e)
{
    if (InputVinInformationTextBox.Text.Length >= 8)
    {
        VinSubmitButton.Enabled = true;
    }
    else
    {
        VinSubmitButton.Enabled = false;
    }
}
于 2012-07-26T19:55:30.593 回答