DukeBrymin DukeBrymin - 4 months ago 19
ASP.NET (C#) Question

Would like to trigger Update button inside a DetailsView from external button

I'm working in ASP.Net Webforms, and I have a DetailsView that has, for its last Template, the Update and Cancel buttons:

<EditItemTemplate>
<asp:LinkButton ID="MyUpdateButton" CommandName="Update" Text="Update" runat="server"></asp:LinkButton>
<asp:LinkButton ID="MyCancelButton" CommandName="Cancel" Text="Cancel" CausesValidation="false" runat="server"></asp:LinkButton>
</EditItemTemplate>


For esthetics, I want to move those buttons outside of the DetailsView. However, in doing so, I lose the ability to trigger the
OnItemUpdating
handler. I can add an
OnClick
handler to the buttons, but then I lose the
DetailsViewUpdateEventArgs
data, and it doesn't call the validators.

So, I thought, "why not have a button outside the DetailsView that calls the .click() on the button inside the DetailsView?". So I wired up another button, made the current Update button invisible (via
'style="display: none;"'
) and set the
OnClientClick
for the external button to a function that finds that internal button and calls the .click() handler on it. I've tested that function, and it's working the way it should, in that it finds the right button. But it still doesn't trigger the
OnItemUpdating
handler.

Any thoughts on what I might be doing wrong? Or a better way to trigger that button (internal to the DetailsView) from an external button?

Answer

According to my tests, simulating a click on a LinkButton does not trigger a postback. You should replace the hidden LinkButton by a hidden Button:

<asp:Button ID="MyUpdateButton" CommandName="Update" Text="Update" style="display: none;" runat="server" />