前言:
asp.net在提供我们丰富的内置控件方便我们开发的同时,一些格式化的东西也限制了我们灵活的要求。在DataGrid控件中,我们遇到的一个非常典型的问题就是如何把我们不想显示的列根据需要随时隐藏掉。
解决方案:
我们不能隐藏在DataGrid里自动生成列的主要的一点原因是:DataGrid里的column不能被DataGrid的属性DataGridColumn增加。
基于以上原因,我们可以提出两种不同的方案来解决这个问题。
方案一:
根据页面请求的事件来隐藏列:
代码:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = new _
SqlConnection("Data Source=(local)NetSDK; Trusted_Connection=Yes;_ Initial Catalog=pubs")
Dim myCommand As SqlCommand = New SqlCommand("Select * From_ Publishers",myConnection)
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader_ (CommandBehavior.CloseConnection)
myDataGrid.DataBind()
End Sub
Sub HideShow_Click(Sender As Object, E As EventArgs)
If myDataGrid.Columns(0).Visible = False Then
myDataGrid.Columns(0).Visible = True
Else
myDataGrid.Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp:DataGrid id="myDataGrid" Width="25%" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Container.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Container.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Container.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Container.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Container.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="HideShow" Text="Hide/Show" OnClick="HideShow_Click" runat="server" />
</form>
</body>
</html>
程序执行执行演示:
Show:
(图show)
Hide
(图hide)
方案二:
方案二实际上是一的变通,我就简单的介绍一下。在一中我们通过button的OnClick事件来判断是Show还是Hide,那么我们也可以通过联接获取参数的值来做判断。
代码:
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = new _
SqlConnection("Data Source=(local)NetSDK; Trusted_Connection=Yes;_ Initial Catalog=pubs")
Dim myCommand As SqlCommand = New SqlCommand("Select * From Publishers",_ myConnection)
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader_(CommandBehavior.CloseConnection)
myDataGrid.DataBind()
If Request.QueryString("Security") = "Admin" Then
myDataGrid.Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp:DataGrid id="myDataGrid" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Container.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Container.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Container.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Container.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Container.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
程序执行演示:
1.Security参数的值:Nothing
(图:hidde)
Security参数:Admin
(图:Show)
asp.net在提供我们丰富的内置控件方便我们开发的同时,一些格式化的东西也限制了我们灵活的要求。在DataGrid控件中,我们遇到的一个非常典型的问题就是如何把我们不想显示的列根据需要随时隐藏掉。
解决方案:
我们不能隐藏在DataGrid里自动生成列的主要的一点原因是:DataGrid里的column不能被DataGrid的属性DataGridColumn增加。
基于以上原因,我们可以提出两种不同的方案来解决这个问题。
方案一:
根据页面请求的事件来隐藏列:
代码:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = new _
SqlConnection("Data Source=(local)NetSDK; Trusted_Connection=Yes;_ Initial Catalog=pubs")
Dim myCommand As SqlCommand = New SqlCommand("Select * From_ Publishers",myConnection)
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader_ (CommandBehavior.CloseConnection)
myDataGrid.DataBind()
End Sub
Sub HideShow_Click(Sender As Object, E As EventArgs)
If myDataGrid.Columns(0).Visible = False Then
myDataGrid.Columns(0).Visible = True
Else
myDataGrid.Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp:DataGrid id="myDataGrid" Width="25%" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Container.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Container.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Container.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Container.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Container.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="HideShow" Text="Hide/Show" OnClick="HideShow_Click" runat="server" />
</form>
</body>
</html>
程序执行执行演示:
Show:
(图show)
Hide
(图hide)
方案二:
方案二实际上是一的变通,我就简单的介绍一下。在一中我们通过button的OnClick事件来判断是Show还是Hide,那么我们也可以通过联接获取参数的值来做判断。
代码:
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = new _
SqlConnection("Data Source=(local)NetSDK; Trusted_Connection=Yes;_ Initial Catalog=pubs")
Dim myCommand As SqlCommand = New SqlCommand("Select * From Publishers",_ myConnection)
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader_(CommandBehavior.CloseConnection)
myDataGrid.DataBind()
If Request.QueryString("Security") = "Admin" Then
myDataGrid.Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp:DataGrid id="myDataGrid" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Publisher's ID">
<ItemTemplate>
<span><%# Container.DataItem("pub_id") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Publisher's Name">
<ItemTemplate>
<span><%# Container.DataItem("pub_name") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<span><%# Container.DataItem("city") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<span><%# Container.DataItem("state") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country">
<ItemTemplate>
<span><%# Container.DataItem("country") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
程序执行演示:
1.Security参数的值:Nothing
(图:hidde)
Security参数:Admin
(图:Show)