我有一个 GridView,我正在用我的数据库中的数据填充它。数据显示完美。我的问题是我希望能够单击一行,然后在其他地方显示该行中的信息(最终我将有一个分屏来很好地格式化所选数据)。选择有效,因为它突出显示了当前行。但是,我无法弄清楚如何在其他地方显示该数据。现在,我正在尝试在名为 testLabel 的标签中显示单个单元格的内容。我尝试了许多不同的方法,当我单击新行时,testLabel 永远不会更新。任何人都可以看到这里出了什么问题吗?
C#
protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = DefaultGrid.SelectedRow;
testLabel.Text = row.Cells[2].Text;
}
ASPX
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID = "ScriptManager" runat="server" />
<div>
<div>
<asp:Label runat="server" id = "orderByLabel" Text = "Order By: " />
<asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
<asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
<asp:ListItem Value="lName">Last Name</asp:ListItem>
<asp:ListItem Value="state">State</asp:ListItem>
<asp:ListItem Value="zip">Zip Code</asp:ListItem>
<asp:ListItem Value="cwaSource">Source</asp:ListItem>
<asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label runat="server" ID="searchLabel" Text="Search For: " />
<asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
<asp:Button ID="searchButton" runat="server" Text="Search" />
</div>
<div align="center">
<asp:UpdatePanel ID = "up" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID = "orderByList"
EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlId="searchButton" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName"
onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
autogenerateselectbutton = "true"
selectedindex="0">
<SelectedRowStyle BackColor="Azure"
forecolor="Black"
font-bold="true" />
<Columns>
<asp:ButtonField CommandName="Select" Visible="false" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<div>
<asp:Label runat="server" ID="testLabel" Text="test" />
</div>
</form>
</body>
</html>