Understanding the delete link in ASP.NET Dynamic Data scaffolds

有些话、适合烂在心里 提交于 2020-01-15 10:14:28

问题


Since the automatic scaffolding of ASP.NET Dynamic Data Web Pages does most of the things I need to do for this project on its on, I'd like to use it as a basis.

Now, I like to add another link to the "Edit" "Delete" "Details" trio on my custom table view. I'd like it behave much like the "Delete" button, i.e. not call another page, but do something in the background (Here: Send an email.) and then refresh the view. Alas, I fail to understand how this "Delete" link works.

It is defined in the automatically generated code as

<asp:LinkButton ID="DeleteLinkButton" 
     runat="server" CommandName="Delete"
     CausesValidation="false" Text="Delete"
     OnClientClick='return confirm("Are you sure you want to delete this item?");'/>

What exactly happens here? Is there a method in the code somewhere named "Delete" (like used in the CommandName property)? What arguments are passed there? And: How would I call a custom method?

I tried stepping through it using the debugger, but it is easy to loose oneself in the LINQ Dataclasses, so I found nothing.

Thanks in advance!


回答1:


The Delete CommandName is normally ties to an equivalent DeleteCommand on the same page under the datasource tag, for example:

 <asp:SqlDataSource ID="SqlDataSourcePending" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringPending %>" 
        DeleteCommand="DELETE FROM [CSNTable] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [CSNTable] ([CSNDate], [CSNStatus], [CSNAuthor], [CSNSubject], [CSNMessage]) VALUES (@CSNDate, @CSNStatus, @CSNAuthor, @CSNSubject, @CSNMessage)" 
        SelectCommand="SELECT ID, CSNDate, CSNStatus, CSNAuthor, CSNSubject, CSNMessage FROM CSNTable WHERE (CSNStatus LIKE 'Pending')" 
        UpdateCommand="UPDATE [CSNTable] SET [CSNDate] = @CSNDate, [CSNStatus] = @CSNStatus, [CSNAuthor] = @CSNAuthor, [CSNSubject] = @CSNSubject, [CSNMessage] = @CSNMessage WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int16" />
        </DeleteParameters>
        <UpdateParameters>
            .........etc...

You can configure the delete command via the datasource control property, or via the page.

As far as a new command, the usual way is to add a new linkbutton, change the command name to something that makes sense for what you want to do CommandName="EmailNotice".

Then catch this button click commandname in the {YourDataTableName}_ItemCommand event by evaluating the eventargs.CommandName (e.CommandName), this is speaking very generally since I do not know what your custom data table is comprised of. When the e.CommandName=="EmailNotice", then you do what you need.

EDIT: Linq is a little different! You can refer to this MSDN article, but the main thing is using the GetCommand method



来源:https://stackoverflow.com/questions/2341920/understanding-the-delete-link-in-asp-net-dynamic-data-scaffolds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!