Basically this function limits the amount of text that can be entered in a textbox based on the radion button selected.
The problem is that on postback of page neither javascript function is being called so no maxlength is set on the textbox. Postback occurs when user clicks a submit button, a search method is called and and the results are made visible via a hidden panel.
FYI: this is going on in a web user control (ascx), not directly on aspx.
Code behind.
html
The problem is that on postback of page neither javascript function is being called so no maxlength is set on the textbox. Postback occurs when user clicks a submit button, a search method is called and and the results are made visible via a hidden panel.
FYI: this is going on in a web user control (ascx), not directly on aspx.
Code behind.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Set intial checked value radio button
Page.ClientScript.RegisterStartupScript(this.GetType(), "initScript", "initDefaultRadio();", true);
}
//Set maxLength of textbox based on the selected radio button
Page.ClientScript.RegisterStartupScript(this.GetType(), "addScript", "setMaxLength();", true);
}
html
Code:
<script type="text/javascript">
function initDefaultRadio() {
document.getElementById('<%= EmailOption.ClientID %>').checked = true;
}
function setMaxLength() {
var myAlert = false; //for testing
var searchText = document.getElementById('<%= SearchText.ClientID %>');
var maxlabel = document.getElementById('<%= lbl_maxLength.ClientID %>');
if (document.getElementById('<%= EmailOption.ClientID %>').checked) {
alert("Email option selected"); //for testing
myAlert = true; //for testing
var max = 5;
searchText.maxLength = max;
maxlabel.innerHTML = "(Maximum " + max + " characters allowed)";
if (searchText.value.length > max) { searchText.value = searchText.value.substr(0, max); }
}
else if (document.getElementById('<%= CompanyOption.ClientID %>').checked) {
alert("Company option selected"); //for testing
myAlert = true; //for testing
var max = 10;
searchText.maxLength = max;
maxlabel.innerHTML = "(Maximum " + max + " characters allowed)";
if (searchText.value.length > max) { searchText.value = searchText.value.substr(0, max); }
}
else if (document.getElementById('<%= ShiptoOption.ClientID %>').checked) {
alert("ShipTo option selected"); //for testing
myAlert = true; //for testing
var max = 15;
searchText.maxLength = max;
maxlabel.innerHTML = "(Maximum " + max + " characters allowed)";
if (searchText.value.length > max) { searchText.text.value = searchText.value.substr(0, max); }
}
if (!myAlert) { alert("setMaxLength funciton called"); } //for testing
}
</script>