thevan
thevan

Reputation: 10364

During PostBack JavaScript losing its functionality

I have one GridView which has one CheckBox and three TextBoxes in its template column. The logic is, when I check the CheckBox, the corresponding TextBoxes should get enabled. If I uncheck the CheckBox, then the corresponding TextBoxes should get disabled. I have written JavaScript for this functionality.

Everything is working fine, but in that page I have one DropDownList too. When I change the DropDownList value, the page gets PostBack and at that time I lost the JavaScript functionality, i.e. the enabled TextBoxes in the GridView gets disabled. How to solve this problem?

Upvotes: 2

Views: 4250

Answers (5)

Aristos
Aristos

Reputation: 66641

After every post back in the update panel you need to reinitialize your javascript, because as you understand the struct of the html has change and javascript runs on the previous one that not exist after the update panel updates. Microsoft gives a functionality to do that as follow.

This is javascript lines.

<script type="text/javascript"> 
    var prm = Sys.WebForms.PageRequestManager.getInstance();    
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    function InitializeRequest(sender, args) {      
    }

    function EndRequest(sender, args) {
      // here initialize again your javascript
    }
</script>

Upvotes: 4

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

Use the RowCreated event for the ASP Grid to add the client function on the check box, this way the client function will stay on each postback

Upvotes: 1

user840182
user840182

Reputation: 1

JavaScript is working on DOM. When page gets PostBack values from ViewState are written to DOM and overwrites your JavaScript changes. You should store state of checkboxes in persistent way ie. jQuery data() or hiddenfield and after PostBack write changes back to DOM.

Upvotes: -1

samy
samy

Reputation: 14962

You must register your javascript at each postback for it to run, by using the ScriptManager.RegisterStartupScript method: here's an example (edited for article in english, sorry :p).

Upvotes: 0

hungryMind
hungryMind

Reputation: 7009

Use auto post back with check box to enable disable text box from server side code. This will update the view state and so it will be maintained on select change. Use these inside UpdatePanel for ajax. Alternatively, you can add drop down into updatepanel as well, so that select change does not affect other page elements.

Upvotes: -1

Related Questions