C# GridView SelectedIndexChanging事件,在TemplateField中查找文本

C# GridView SelectedIndexChanging事件,在TemplateField中查找文本,c#,asp.net,gridview,C#,Asp.net,Gridview,我有网格,它是SelectIndexchange事件 我的aspx代码: <asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"

我有网格,它是SelectIndexchange事件

我的aspx代码:

 <asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow"
               BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"
    OnRowEditing="gvClients_RowEditing"  Font-Size="Medium" 
    onrowcancelingedit="gvClients_RowCancelingEdit" 
    onrowdeleting="gvClients_RowDeleting" onrowupdating="gvClients_RowUpdating" 
    onselectedindexchanging="gvClients_SelectedIndexChanging">
    <FooterStyle BackColor="Tan" />
    <Columns>
        <asp:CommandField ShowEditButton="True" ButtonType="Image" CancelImageUrl="~/Images/Cancel.png"
            UpdateImageUrl="~/Images/save.png" EditImageUrl="~/images/Edit.png" ItemStyle-Width="25px"
            UpdateText="Update" CancelText="Cancel" />
        <asp:CommandField ShowDeleteButton="true" DeleteImageUrl="~/Images/delete.png" ButtonType="Image"
            ItemStyle-Width="25px" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton runat="server" ID="imgSelect" CommandName="Select" AlternateText="Select"
                    ImageUrl="~/Images/allowed.png" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ClientId" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("ClientId")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtClientClientId" Text='<%#Eval("ClientId") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ClientName" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("ClientName")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtClientClientName" Text='<%#Eval("ClientName") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" ItemStyle-Width="150px" ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("Active")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtClientActive" Text='<%#Eval("Active") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" Font-Size="Smaller" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
问题是,文本为空。有数据,但不是值,而是空的…同样的代码在另一个页面上工作。所以,请告诉我。我做错了什么


如果您使用
模板字段
而不是
边界字段
单元格中的文本总是空的,谢谢。你要么必须

  • 使用
    模板字段中的控件
    并使用
    行。FindControl(id)
    查找它(我推荐)
  • 或者您必须将第一个控件投射到有点奇怪的位置,因为您无法看到该控件。它会自动添加到单元格的控件集合中

    GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
    txtClientNumber.Text = ((ITextControl)row.Cells[3].Controls[0]).Text;
    

谢谢您的建议…实际上,根据您的回答,结果如下:“\r\n Atomic Park.com”\r\n”…但我使用了修剪功能…结果如我所愿。再次感谢
GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
txtClientNumber.Text = ((ITextControl)row.Cells[3].Controls[0]).Text;