Click here to Skip to main content
Click here to Skip to main content

When somebody changes my code...

By , 15 May 2012
Rate this:
Please Sign up or sign in to vote.

Well, initially I might get upset, but only if they break something. But here I am not talking a team member changing your code... No... that's OK

This blog entry is about that 'non-deterministic' behavior that sometimes happens to 'us' while developing software and all of the sudden something does not work as it 'should' be. Who does not hate that kind of bugs right? This example is a real case scenario using Telerik components in a web application.

A basic example

Consider the following piece of ASPX code, I have placed a button and a label, the button 's click event in code-behind event will change the value of the label.

<asp:Button runat="server" ID="confirmButton" OnClientClick="return confirm('are you sure?');" OnClick="ConfirmButtonClick" Text="Confirm this button click!"/>
<asp:Label runat="server" ID="outputLabel"></asp:Label>

Pretty simple right? Just a button with a javascript confirmation dialog before performing the page's post back. Now lets 'ajaxify' this...

<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <contenttemplate>            
     <asp:Button runat="server" ID="confirmButton" OnClientClick="return confirm('are you sure?');" OnClick="ConfirmButtonClick" Text="Confirm this button click!"/>
     <asp:Label runat="server" ID="outputLabel"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

Lets use telerik...

Using the new ajaxified code now have pretty much same effect, except we are not performing a full page post back, but only updating elements inside the UpdatePanel control. But what if I have a page already with lot of telerik stuff? I wont be using the classic asp.net controls, I will use the Telerik stuff.. now lets do it with Telerik !

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ajaxsettings>
            <telerik:AjaxSetting AjaxControlID="confirmButton">
                <updatedcontrols>
                    <telerik:AjaxUpdatedControl ControlID="outputLabel" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
     
     <asp:Button runat="server" ID="confirmButton" OnClientClick="return confirm('are you sure?');" OnClick="ConfirmButtonClick" Text="Confirm this button click!"/>
     
     <asp:Label runat="server" ID="outputLabel"></asp:Label>
What a surprise, It will *not* work.... however... it should... why is it failing then?

The problem (and the fix)

In order to figure out that, let's check the HTML code rendered by the ASP.NET page where we used the Telerik components.
<input type="button" name="confirmButton" value="Confirm this button click!" onclick="return confirm('are you sure?');__doPostBack('confirmButton','')" id="confirmButton" />

Wow.... my javascript has changed... and it has the "do postback" call after it! But I didn't do that right? That's not a new behavior, postback codes are changed a lot in .NET, but for my experience, whenever ASP.NET or anything change that code, it always works... Fortunately is easy to fix, just a small change; instead of return confirm('are you sure?'); I just changed that line of code to: if (confirm('are you sure?')) That way it will generate a complete condition that will do the postback after the confirmation.

if (confirm('are you sure?')) __doPostBack('confirmButton','')
Don't get me wrong, I like Telerik a lot, and I used their components a lot... but.. you know...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Leonardo Paneque
Team Leader
United States United States
Leonardo loves to code with C# and thinks .NET platforms rocks.
He has a Master degree in Computer Sciences and likes to share code and ideas.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 15 May 2012
Article Copyright 2012 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid