开发者

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

开发者 https://www.devze.com 2023-03-27 17:25 出处:网络
<asp:GridViewID=\"gridInboxMessage\" runat=\"server\" AllowPaging=\"True\" AllowSorting=\"True\" AutoGenerateColumns=\"False\" DataSourceID=\"LinqDataSource1\"
<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.


Use RowCommand Event.

The RowCommand event is raised when a button is clicked in the GridView control. 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. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

 <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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号