asp.net GridView行不可单击

本文关键字:单击 net GridView asp | 更新日期: 2023-09-27 18:00:01

我需要创建一个代表员工的asp.net GridView,如果用户单击一行,它将显示员工的全部详细信息。我创建了一个,但行(删除按钮旁边)是不可点击的。如何使它们可点击?

这是设计代码:

      <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:SqlDataSource ID="SqlDataSourceMain" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:AtidConnectionString %>" 
                    SelectCommand="SELECT [ID], [first_name], [last_name] FROM [Contacs]" 
                    DeleteCommand="DELETE FROM [Contacs] WHERE [ID] = @ID" 
                    InsertCommand="INSERT INTO [Contacs] ([ID], [first_name], [last_name]) VALUES (@ID, @first_name, @last_name)" 
                    UpdateCommand="UPDATE [Contacs] SET [first_name] = @first_name, [last_name] = @last_name WHERE [ID] = @ID">
                    <DeleteParameters>
                        <asp:Parameter Name="ID" Type="String" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="ID" Type="String" />
                        <asp:Parameter Name="first_name" Type="String" />
                        <asp:Parameter Name="last_name" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="first_name" Type="String" />
                        <asp:Parameter Name="last_name" Type="String" />
                        <asp:Parameter Name="ID" Type="String" />
                    </UpdateParameters>
                </asp:SqlDataSource>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                </asp:UpdatePanel>
                <asp:CheckBox ID="CheckBox7" runat="server" />
                <asp:GridView ID="GridView1" runat="server" 
                    AutoGenerateColumns="False"  
                    ShowHeaderWhenEmpty="True"
                    CellPadding="4" 
                    DataKeyNames="ID" 
                    DataSourceID="SqlDataSourceMain"  
                    ForeColor="#333333" Height="256px" PageSize="20" 
                    style="margin-left: 240px; margin-right: 53px; margin-top: 1px" 
                    Width="517px" OnRowCommand="delete_button_Click" 
                    OnRowDataBound="OnRowDataBound" OnDataBound="fun">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:BoundField DataField="ID" HeaderText="ID" 
                            SortExpression="id" />
                        <asp:BoundField DataField="first_name" HeaderText="first name" 
                            SortExpression="first_name" />
                        <asp:BoundField DataField="last_name" HeaderText="last name" 
                            SortExpression="last_name" />
                    </Columns>
                    <EditRowStyle Height="15px" HorizontalAlign="Center" />
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" 
                        Height="10px" HorizontalAlign="Center" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <SortedAscendingCellStyle BackColor="#FDF5AC" />
                    <SortedAscendingHeaderStyle BackColor="#4D0000" />
                    <SortedDescendingCellStyle BackColor="#FCF6C0" />
                    <SortedDescendingHeaderStyle BackColor="#820000" />
                </asp:GridView>
                <asp:CheckBox ID="CheckBox8" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>

asp.net GridView行不可单击

ASP.NET GridView控件没有那么高级,但使用客户端代码是可行的,如下所述。

基本上,你会生成一个客户端点击事件(从文章中发布):

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

这个例子点击了行,但实际上你可以触发任何你想要的操作。使用"Select$"是触发Select事件的默认方式;你也可以用任何事件触发RowCommand(我相信)。