2013-04-22 47 views
0

我有這樣的GridView:GridViewCosts.SelectedRow.Cells [1]。文本不工作,ItemTemplate中

<div class ="gridView"> 
    <asp:GridView ID="GridViewCosts" runat="server" ShowFooter="True" ShowHeaderWhenEmpty="True" 
     AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px" 
     OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged" 
     OnPageIndexChanging="GridViewCosts_PageIndexChanging" 
     AllowPaging="True" 
     CssClass="mGrid" 
    PagerStyle-CssClass="pgr" 
    AlternatingRowStyle-CssClass="alt" PageSize="5"> 
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
     <Columns> 
      <asp:BoundField DataField="Id" HeaderText="Номер" /> 
       <asp:TemplateField HeaderText="Стойност"> 
        <EditItemTemplate> 
         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value") %>'></asp:Label> 
        </ItemTemplate> 
        <ControlStyle Width="100px" /> 
      </asp:TemplateField> 

而且在後面的代碼我想:

protected void GridViewCosts_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      TextBoxValue.Text = GridViewCosts.SelectedRow.Cells[1].Text; 
} 

這是不工作時,列值是TemplateField。如果該列是BoundFIeld,那麼它正在工作。我該怎麼辦 ?

回答

1

如果使用TemplateFields你必須使用FindControl去控制一個參考:

Label Label2 = (Label)GridViewCosts.SelectedRow.FindControl("Label2"); 
TextBoxValue.Text = Label2.Text; 
0

試試這個示例代碼

For Each gvr As GridViewRow In gvInvoice.Rows 
Dim TctValue As TextBox= DirectCast(GridViewCosts.Cells(1).FindControl("TextBox2"), TextBox) 
totamount = totamount + Convert.ToDouble(lblAmount.Text) 
Next 
txtTotal.Text = totamount.ToString()