开发者

How do we restrict the length of a combo box control in ASP.NET?

开发者 https://www.devze.com 2023-04-05 00:16 出处:网络
Basically I want that the Ti开发者_如何学编程tle field(Combo Box) should not allow me to enter more than 40 characters.

Basically I want that the Ti开发者_如何学编程tle field(Combo Box) should not allow me to enter more than 40 characters.

Can you provide any pointers?


I looks like the control itself does not have that functionality, so you will probably have to write your own version.

You could create a custom control to extend the ComboxBox control. Check out this blog post.

Another idea is to use jQuery to prevent more than 40 characters from being added to the input control the ComboBox control generates:

$(function() {

    var comboxBoxControlInput = $("#<%=comboBoxControlId.ClientID%>$TextBox");
    $(comboxBoxControlInput).keyup(function() {
        limitLenth(this, 40);
    });

});

function limitLength(control, length) {
    var currentContent = $(control).val();
    var currentLength = currentContent.length;
    if(currentLength > length) {
        $(control).val(currentContent.substr(0, length));
        return false;
    }
}

Unfortunately it's a bit hacky. You have to get the ClientID of the ComboBox control (<%=comboBoxControlId.ClientID%>) and then append $TextBox to the end in order for jQuery to select the correct control.

Edit: Another way to select the correct input control is to do this:

$("#<%=comboBoxControlId.ClientId%>").find("input[type=text]");

This selects the first text input within the div the ComboBox control creates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号