Customizing the DetailsView Command Bar

I just want to add some Javascript validation when an element is inserted or edited. The only way I know this (using inline Javascript) is to disable the AutoGenerateXxxButton properties for the DetailsView and make your own. The problems I'm having is replacing them with custom LinkButtons (and keeping the default action) and adding Javascript to them. Is there a way that I can do this easily? The only option I can see is to edit the template and put it in a footer or something.

Tips? Tricks? Obvious things I'm missing?

0


a source to share


1 answer


I stumbled upon this chatter while looking for other questions about the in-depth review. What is relatively easy to do is turn this field into a template field. Here's some code from pivoting the delete command line in 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 box ...



Now, to add JavaScript to this delete button, you can do this in the DetailsView's DataBound event handler:

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 works on a delete button, but you can also use this concept for insert and update buttons.

+2


a source







All Articles