在行选择上从.NET DataGrid更新控件

时间:2015-06-19 11:38:50

标签: asp.net vb.net datagrid sqldatasource

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

我的页面上有一个SqlDataSource控件,它返回(例如)6列。我在datagrid中显示了其中的5个,并且当用户选择一行时想要标签中显示的第六列。

我尝试了各种各样的事情,但收效甚微。我确信其中一种方法是将列包含在datagrid中,但将其设置为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

2 个答案:

答案 0 :(得分:0)

您可以使用带有标签的隐藏<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

答案 1 :(得分:0)

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

lblNotes.Style.Item("display") = "none"的某些内容。如果你不想要回页,你也可以在Javascript中这样做。