简体   繁体   English

ASP.NET - 如何删除 GridView 的一行?

[英]ASP.NET - How do I delete a row of a GridView?

<asp:GridView  ID="gridInboxMessage" runat="server" AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
    OnSelectedIndexChanged="gridInboxMessage_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>                                        
                <asp:Button runat="server" ID="DeleteInbox" Text="delete" />
            </ItemTemplate>
        </asp:TemplateField>                                
        <asp:CommandField ShowSelectButton="True" SelectText="show text" />                                
        <asp:BoundField DataField="Title" HeaderText="title" ReadOnly="True" SortExpression="Title" />
        <asp:TemplateField SortExpression="Body" HeaderText="body">
            <ItemTemplate>
                <asp:Label ID="MyBody" runat="server" Text='<%# TruncateText(Eval("Body"))%>'>                            
                </asp:Label>
                <asp:Label ID="fullBodyRecieve" Visible="false" runat="server" Text='<%# Eval("Body")%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="Sender" HeaderText="sender">
           <ItemTemplate>
               <asp:Label ID="sender" runat="server" Text='<%# GetCompanyNameById(Eval("Sender"))%>'>
               </asp:Label>
           </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="Date1" HeaderText="date">
           <ItemTemplate>
              <asp:Label ID="PersianDateRecieve" runat="server" Text='<%# GetPersianDate(Eval("Date1"))%>'>
              </asp:Label>
           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="orange" />
</asp:GridView>
<div id="contentBodyMessageRecieve" style="width:300px; border:1px silid black" runat="server">
</div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" Select="new (Title, Body, Sender, Date1)" TableName="PrivateMessages" Where="Receptor == @Receptor">
    <WhereParameters>
        <asp:QueryStringParameter Name="Receptor" QueryStringField="idCompany" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
</fieldset>
<br />
<br />

i want when user click on DeleteBox delete that row.我想当用户单击DeleteBox时删除该行。

Use RowCommand Event.使用 RowCommand 事件。

The RowCommand event is raised when a button is clicked in the GridView control.在 GridView 控件中单击按钮时会引发 RowCommand 事件。 This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.这使您能够提供一种事件处理方法,该方法在此事件发生时执行自定义例程。

Buttons within a GridView control can also invoke some of the built-in functionality of the control. GridView 控件中的按钮也可以调用控件的一些内置功能。 To perform one of these operations, set the CommandName property of a button to one of the values in the following table.若要执行这些操作之一,请将按钮的 CommandName 属性设置为下表中的值之一。

 <asp:gridview id="ContactsGridView" 
          datasourceid="ContactsSource"
          allowpaging="true" 
          autogeneratecolumns="false"
          onrowcommand="ContactsGridView_RowCommand"
          runat="server">

          <columns>
            <asp:buttonfield buttontype="Link" 
              commandname="Delete" 
              text="Delete"/>
            <asp:boundfield datafield="ContactID" 
              headertext="Contact ID"/>
            <asp:boundfield datafield="FirstName" 
              headertext="First Name"/> 
            <asp:boundfield datafield="LastName" 
              headertext="Last Name"/>
          </columns>



        </asp:gridview>




Sub ContactsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

' If multiple buttons are used in a GridView control, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Delete" Then

  ' Convert the row index stored in the CommandArgument
  ' property to an Integer.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

      'Call you delete function here 
    End IF      
 End Sub**strong text**

GridView row command GridView 行指令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM