【问题标题】:asp.net GridView rows are not clickableasp.net GridView 行不可点击
【发布时间】:2015-06-27 22:15:36
【问题描述】:

我需要创建一个代表员工的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>

【问题讨论】:

  • 网格单元格不可点击是什么意思?
  • 是的。网格单元格不可点击。
  • 确保您没有标记单元格ReadOnly
  • 没有只读的。

标签: c# asp.net gridview


【解决方案1】:

ASP.NET GridView 控件不是那么先进,但可以使用详细的客户端代码here

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

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$”是触发选择事件的默认方式;您也可以通过任何事件触发 RowCommand(我相信)。

【讨论】:

  • 我做到了。但是“OnRowDataBound”函数只在运行开始时调用,而不是在单击时调用。这些行仍然不可点击。
  • 此函数将客户端点击事件渲染到行,并触发回发以在服务器上处理。查看页面的源代码并验证 onclick 是否附加到 tr 元素。
  • 页面中没有tr元素。
  • 你确定吗?网格是关于表格的。你能发布一个 GridView 行的标记吗?
  • 对不起。我是asp.net 的新手。代码中哪里是 GridView 行的标记?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 2021-08-30
相关资源
最近更新 更多