2013-07-12 129 views
0

我有一個Gridview,每行有一個關閉linkbutton。當用戶點擊關閉Button時,它應該將數據寫入該特定行。我的數據已經寫入,但我當我點擊例如第三行linkbutton關閉,然後寫入第一行。我希望它寫入第三行。當Gridview點擊鏈接按鈕時寫入到Gridview行

<asp:GridView ID="gvMain" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
Width="688px" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
OnRowCommand="gvTransInfo_RowCommand"> 
<AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
<Columns> 
    <asp:TemplateField HeaderText="Transaction Type" SortExpression="TransTypeDesc"> 
     <EditItemTemplate> 
      <asp:DropDownList ID="DDLTrans" runat="server" DataSourceID="SqlDataSourceTrans" 
       DataTextField="TransTypeDesc" DataValueField="TransTypeId"> 
      </asp:DropDownList> 
      <asp:SqlDataSource ID="SqlDataSourceTrans" runat="server" ConnectionString="<%$ ConnectionStrings:iQueueConnectionString %>" 
       SelectCommand="SELECT [TransTypeId], [TransTypeDesc] FROM [tblTransType]"></asp:SqlDataSource> 
     </EditItemTemplate> 
     <ItemTemplate> 
      <asp:Label ID="lblTrans" runat="server" Text='<%# Bind("TransTypeDesc") %>'></asp:Label> 
     </ItemTemplate> 
     <ItemStyle HorizontalAlign="Center" /> 
    </asp:TemplateField> 
    <asp:BoundField DataField="Received" HeaderText="Received" SortExpression="Received"> 
     <ItemStyle HorizontalAlign="Center" /> 
    </asp:BoundField> 
    <asp:BoundField DataField="Complete" HeaderText="Complete" SortExpression="Complete"> 
     <ItemStyle HorizontalAlign="Center" /> 
    </asp:BoundField> 
    <asp:BoundField DataField="TransTime" HeaderText="Est. Transaction Time" SortExpression="TransTime"> 
     <ItemStyle HorizontalAlign="Center" /> 
    </asp:BoundField> 
    <asp:TemplateField ShowHeader="False"> 
     <ItemTemplate> 
      <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" 
       OnClick="CloseClick_Click">Close</asp:LinkButton> 
      <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow">Edit</asp:LinkButton> 
      &nbsp;<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow">Cancel</asp:LinkButton> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="UpdateRow" 
       Text="Update" CommandArgument='<%# Eval("TicketId")%>'></asp:LinkButton> 
      &nbsp;<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="CancelUpdate" 
       CommandArgument='<%# Eval("TicketId")%>' Text="Cancel"></asp:LinkButton> 
     </EditItemTemplate> 
     <FooterStyle HorizontalAlign="Center" /> 
     <ItemStyle HorizontalAlign="Center" /> 
    </asp:TemplateField> 
</Columns> 

protected void CloseClick_Click(object sender, EventArgs e) 
    { 
     var StartLink = (Control)sender; 
     GridViewRow row = (GridViewRow)StartLink.NamingContainer; 
     firstCellText = row.Cells[0].Text; 
    } 
+0

CloseClick_Click的代碼是什麼? –

+0

@DavideLettieri更新 – Apollo

回答

0

使用帶select屬性,而不是用LinkButton

一個TemplateField一個CommandField,並在YourGridView_SelectedIndexChanged handler使用YourGridView.SelectedIndex與您單擊的行工作。

然後你可以說:

firstCellText = row.Cells[YourGridView.SelectedIndex].Text; 
0

得到它解決。我只是做了一個查詢,並根據我的firstcellText值顯示它,解決了問題。