从行 select 上的 .NET DataGrid 更新控件

Update control from .NET DataGrid on row select

我有一个表面上看起来非常简单的要求。我想在用户单击数据网格控件中的字段时使用数据源中的值更新标签。

我的页面上有一个 SqlDataSource 控件,returns(例如)6 列。我在数据网格中显示其中的 5 列,并希望在用户选择一行时在标签中显示第六列。

我尝试了各种方法,但收效甚微。我确信可行的一种方法是将列包含在数据网格中,但将其设置为 Visible="false"。然而,事实证明,如果你这样做,row(5).text 的值是“”......不是我所期望的。

有什么快速实现方法吗?

-- 编辑 - 添加到代码示例中 --

<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
        <asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
        <asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
        <asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
        <asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
        <asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
        <asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
        <asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
        <asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />
    </Columns>
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#594B9C" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

VB代码:

Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged

    Dim row As GridViewRow = gridL250Tickets.SelectedRow
    Label1.Text = row.Cells(9).Text

End Sub

您可以改用带有标签的隐藏 <asp:TemplateField>。我对此进行了测试,它似乎有效。

<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
        <asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
        <asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
        <asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
        <asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
        <asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
        <asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
        <asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
        <%--<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />--%>
        <asp:TemplateField Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

    //...

然后只需在您的活动中找到标签并使用其文本即可。

Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged

    Dim row As GridViewRow = gridL250Tickets.SelectedRow
    Dim lblNotes As Label = row.FindControl("lblNotes")
    Label1.Text = lblNotes.Text

End Sub

尝试设置给定列的实际 CSS,而不是设置 Visible = "False"Display = "none",点击网格控件时切换回来

类似 lblNotes.Style.Item("display") = "none" 的内容。如果您不想使用分页,也可以在 Javascript 中执行此操作。