2015-06-27 141 views
0

我需要创建一个代表员工的asp.net GridView,并且如果用户点击一行,它将显示该员工的完整详细信息。我创建了一个,但行(不包括删除按钮)不可点击。如何让他们点击?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> 
+0

你的意思是网格单元不可点击? – Rahul

+0

是的。网格单元不可点击。 –

+0

确保您没有标记“ReadOnly”单元格。 – Rahul

回答

0

的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."; 
    } 
} 

这个例子点击该行,但真的是你可以触发你想要的任何行动。使用“选择$”是触发选择事件的默认方式;你也可以在任何事件中触发RowCommand(我相信)。

+0

我做到了。但“OnRowDataBound”函数仅在运行开始时调用,而不是在单击时调用。行仍然不可点击。 –

+0

此函数将客户端单击事件呈现给该行,并触发回发以在服务器上进行处理。查看页面的源代码,并确认onclick已附加到tr元素。 –

+0

页面中没有tr元素。 –