2

我有网格,它是 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>

现在,我的SelectedIndexChanging代码:

protected void gvClients_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
    txtClientNumber.Text = row.Cells[3].Text;
}

问题是,文本来了 null 。有数据,但不是值,而是空值....同样的代码也在另一个页面上工作。所以,请告诉我。我做错了什么?

谢谢

4

1 回答 1

2

如果你使用TemplateFields而不是总是空BoundFields的。Cell.Text你要么必须

  • 使用控件TemplateField并使用row.FindControl(id)来找到它(我推荐的)
  • 或者您必须将第一个控件转换DataBoundLiteralControl为有点奇怪的控件,因为您看不到该控件。它会自动添加到单元格的控件集合中。

    GridViewRow row = gvClients.Rows[e.NewSelectedIndex];
    txtClientNumber.Text = ((ITextControl)row.Cells[3].Controls[0]).Text;
    
于 2013-09-25T10:54:30.183 回答