Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript not running on PostBack (ASP.NET, C#)

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
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.
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>
 
is the textbox set for "multi-line"? I don't think max length works on textareas (multi-line textbox), only text input elements.

you are registering setMaxLength to execute when the page loads, but you need to validate the text length when the page is submitted and/or when the user enters text.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Sorry, I left off a piece. onClick for the radio buttons also exectutes the function.

The textbox is not multiline.


I did find a solutoion but not an explaination.

i put Sys.Application.add_load(setMaxLength); in the beginning of the <script> tag.

I was told that if the page isnt doing a full post back then the javascript will not run again without this. As far as I know I am doing a full pastback. I wouldnt know how to do a partial.
 
Er, your code is set up to register the scripts if it's not a post back, hence it doesn't register them if it is a post back...
Code:
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Set intial checked value radio button
        ...
        //Set maxLength of textbox based on the selected radio button
        ...
    }



Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
The OP is referring to the maxlength script which is not in the IF statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top