I wrote two posts before regarding the integration and the customization of FCKEditor in ASP.NET and was asked by a reader how to use the “Save” icon in the FCKEditor’s toolbar set to perform the actual save on the server side. I am sorry for the long delayed answer for that question because I have been tied up with my work and haven’t had much time to look into the problem until recently.
This will work if you don’t have any other server control that cause PostBack. However, the more common scenario is you have some other server controls that also cause PostBack, such as Button, LinkButton, HyperLink, ImageButton, etc., then the above code will not be able to tell which PostBack was caused by the “Save” icon and which was caused by the other server controls. The good news is that ASP.NET injects two famous hidden fields when a page is rendered: “__EVENTTARGET” and “__EVENTARGUMENT”. “__EVENTTARGET” stores the control that causes the PostBack and “__EVENTARGUMENT” stores the argument of the PostBack, and they are populated only when a PostBack is generated by server control. It means that if the “Save” icon is clicked, then “__EVENTTARGET” field will have an empty value upon PostBack, so it tells us that the PostBack is caused by the “Save” icon, not a server control. The above code can be modified as follows:
The above code works fine until you have Button control and ImageButton control on your page because when a Button (or ImageButton) control is clicked, __EVENTTARGET is not populated and the above code will still think the PostBack is coming from the “Save” icon. The reason that __EVENTTARGET is not populated is that those controls are rendered as <input type=”submit”>, which will just submit the form. So if there is any server control on the page that will be rendered as <input type=”submit”>, the above code will not work as expected.
So what are we gonna do then? What we can do is to use another hidden field to store the source of the PostBack (from client or server), then in the code behind we first check “__EVENTTARGET”, if it is empty, then we check this hidden field to see where the PostBack came from. If it came from client, then we know the PostBack was caused by the “Save” icon, otherwise, it was caused by a server control, then we can skip it to let the control’s event handler to take over the process.
Here is the hidden field:
The Page_Load is modified as follows:
And set the Button control’s OnClientClick=”setPostBackSource()”:
Remember, for any server control that will not populate __EVENTTARGET field upon PostBack, you will need to add OnClientClick=”setPostBackSource()”.
Here is the complete code snippet of the code behind:
I hope this will help someone and appreciate any comment on this post.