3

我在 VS2012 中使用 asp.net 和 C# 在我的数据网格上进行了 On Select 更改,但由于某种原因,所选行没有在我的标签中打印出来,我不是 C# 专家,尽管我正在学习,有人可以告诉我是否我犯了一个错误。

谢谢

C# 代码

public void Latest_DVD()
{
    {
        using (OleDbDataAdapter dataquer = new OleDbDataAdapter("SELECT Title,Category,Director,Stock,Year FROM DVD ", conn))
        {
            dataquer.Fill(dt);
        }
    }
    DG_Latest.ShowHeader = true;
    DG_Latest.DataSource = dt;
    DG_Latest.DataBind();
    conn.Close();
    conn.Dispose();
}

void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = DG_Latest.SelectedRow;

    MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />             
       </asp:GridView>
4

2 回答 2

4

你需要SelectedIndexChangedEvent正确接线

尝试这个:

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True"   
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" GridLines="Vertical" 
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />             
   </asp:GridView>  

代码背后:

protected void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = DG_Latest.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
于 2013-12-22T04:11:59.303 回答
3

在标记中,您需要添加OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged"如下:

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" 
BackColor="White" BorderColor="#999999" BorderStyle="None" 
BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />             
</asp:GridView>
于 2013-12-22T03:54:00.583 回答