Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  134] [ 5]  / answers: 1 / hits: 27619  / 11 Years ago, tue, june 25, 2013, 12:00:00

I am working on an ASP.NET web forms application in C#. I have a button that needs to run some client-side Javascript (using the OnClientClick attribute), then run server-side code (using OnClick) if the client-side code returns true. I've done this plenty of times before with jQuery-ui confirmation dialogs, however this time my client-side code isn't a confirmation dialog and it isn't working -- the server-side (OnClick) event never gets triggered even though I'm explicitly returning true.



Here's the client-side code in question, which is working (I've verified that it's returning the correct value with an alert() call):



<script type=text/javascript>
//Function to hide edit fields
function hideEditFields(retVal) {
//Hide all edit divs
var editName = document.getElementsByClassName(editField);
for (i = 0; i < editName.length; i++) {
editName[i].style.display = 'none';
}
//Show all view divs
var viewName = document.getElementsByClassName(viewField);
for (i = 0; i < viewName.length; i++) {
viewName[i].style.display = 'block';
}

//Make investigated & corrective action checkboxes non-highlighted
$('<%=cbInvestigatedY.ClientID%>').removeClass(editable);
$('<%=cbInvestigatedN.ClientID%>').removeClass(editable);
$('<%=cbCorrectiveY.ClientID%>').removeClass(editable);
$('<%=cbCorrectiveN.ClientID%>').removeClass(editable);

//Show edit button
var editButton = document.getElementById(editButton);
editButton.style.display = 'block';
//Hide save button
var saveButton = document.getElementById(saveButton);
saveButton.style.display = 'none';

//alert(returning + retVal);
return retVal;
}
</script>


And here's the button that calls this function:



<asp:Button ID=btnSaveChanges runat=server OnClientClick=return 
hideEditFields(true); OnClick=btnSubmit_Click Text=Save Changes />


I've tried this with UseSubmitBehavior=true and without, which seems to make no difference. I'm getting no errors in the Javascript console, and the Javascript code is doing what it's supposed to do, it's just not calling the server-side method.



Here's how the submit button gets rendered (in view source) by ASP.NET:



<input type=submit name=ctl00$MainContent$btnSaveChanges value=Save Changes
onclick=return hideEditFields(true);WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSaveChanges&quot;, &quot;&quot;,
true, &quot;&quot;, &quot;&quot;, false, false))
id=ctl00_MainContent_btnSaveChanges />

More From » c#

 Answers
15

The real problem was that my server-side method was being executed, but was doing nothing, because it compares the original values in the form (stored as hidden fields) with the new values entered in the form's text boxes and other controls and stores them if they have changed. But I was making the noob mistake of not enclosing the code that populated the form in a check for IsPostBack (and I know better!), so the comparison was always coming out equal, so nothing was changing. See Why is ASP.NET submitting the original value of a TextBox control when the contents have been changed? for more details.


[#77423] Monday, June 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;