2011-08-13 54 views
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.NET - 如何刪除一行GridView?

+1

你有什麼迄今所做和您遇到了什麼問題? –

+0

你更新了你的問題嗎? –

回答

1

使用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 row command

+0

如何能感覺標題字段當前 – ashkufaraz

+0

「感覺標題字段當前如何」? –

+0

獲取當前行的字段 – ashkufaraz