tags:

views:

93

answers:

1

I simply want to add some Javascript checks when an item is inserted or edited. The only way I know of to do this (using inline Javascript) would be to disable the AutoGenerateXxxButton properties for the DetailsView and make my own. The issues I'm having is replacing them with custom LinkButtons (and keeping the default action) and adding Javascript to them. Is there a way I can do this easily? The only option I see is to edit the template and put them in the footer or something.

Hints? Tricks? Blatantly obvious things that I'm missing?

+2  A: 

I came across this wile searching for some other questions on the detailsview. What is relatively easy to do is to turn that field into a template field. Here some code from turning the delete command line into a template field:

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
         <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="False" 
                         CommandName="Delete" Text="Delete"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

You can put any markup you link in the template field...

Now, to add some JavaScript to that delete button, you can do that in the DataBound event handler for the DetailsView:

Protected Sub dgFileDetails_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgFileDetails.DataBound
    Dim btnDelete As LinkButton = CType(dgFileDetails.FindControl("btnDelete"), LinkButton)
    If Not btnDelete Is Nothing Then
       btnDelete.OnClientClick = String.Format("return confirm('Are you sure you want to delete the division {0}?');", dgFileDetails.DataKey.Value)
    End If
End Sub

I know this is working on the delete button, but you should be able to take this concept fro the insert and update buttons too.

Ken Ray