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

发布于 2024-11-29 10:21:15 字数 2632 浏览 2 评论 0原文

<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 />

我希望当用户单击 DeleteBox 时删除该行。

<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光沙漏 2024-12-06 10:21:15

使用 RowCommand 事件。

当单击 GridView 控件中的按钮时,将引发 RowCommand 事件。这使您能够提供一个事件处理方法,每当此事件发生时,该方法都会执行自定义例程。

GridView 控件中的按钮还可以调用该控件的一些内置功能。要执行其中一项操作,请将按钮的 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 行命令

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文