0

我有这个网格视图:

<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 它正在工作。我该怎么办 ?

4

2 回答 2

1

如果你使用TemplateFields你必须使用FindControl来获取对控件的引用:

Label Label2 = (Label)GridViewCosts.SelectedRow.FindControl("Label2");
TextBoxValue.Text = Label2.Text;
于 2013-04-22T13:38:40.647 回答
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() 
于 2013-04-22T13:38:52.857 回答