- 热门文章:
- · DPC:Creating a DataBound List of Radio Buttons--PART1[等级:中]
- · DPC:Creating a DataBound List of Radio Buttons--PART2[等级:中]
- · DPC:Creating a DataBound List of Radio Buttons--预览页面source code[等级:中]
- · .NET几大热点问题(转)
- · DataList控件也玩分页 (转自aspcn.com)
- · 用 StringBuilder 类替代 String
- · 杂志目录(页面部分CodeBehind)
- · 杂志目录(数据库访问部分)
- · 杂志目录(页面部分)
- · 推荐一个免费的CSharp编辑器
- · 使用JScript.NET创建asp.net页面(二)
- · 使用JScript.NET创建asp.net页面(三)
DPC:Hiding Columns In A DataGrid[等级:初 中]
One of the frequently asked questions over at Asplists.com is: "How do I hide a column in a datagrid?". One very important point to note is that you cannot hide autogenerated columns in a data grid. The reason for this is that autogenerated columns are not added to the DataGrid@#s DataGridColumn collection. So, in order for a column to be hidden it must be either programmatically added to the DataGrid at runtime or explicitly defined (using templates) at design time with the AutoGenerateColumns property set to false (the code in this article will use this method).
This article provides sample code to hide a column (could easily be modified to hide more) for two different scenarios - hiding columns in response to an event on the page (common in web reports) or hiding columns to provide different functionality based on security levels.
First, let@#s look at how we can hide and show a column in a DataGrid in response to an event (the click of a button) on the page. You can see a live example here.
The code:
<%@ 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>
Next, let@#s look at how we can hide a column in a DataGrid based on the security level of the user. This example is not *actually* secure and security is not discussed in this article - we are trying to show how to hide DataGrid columns not how to secure a page! The code below checks a Querystring parameter called @#Security@# each time the page loads. If @#Security@# equals "Admin" then all of the columns are displayed. If @#Security@# equals anything other than "Admin" then the first column in the DataGrid is hidden.
Click here to view the DataGrid with no priveleges. (No pub_id column)
Click here to view the DataGrid with Admin priveleges. (All columns visible)
The code:
<%@ 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>
相关文章:
- · 使用JScript.NET创建asp.net页面(四)
- · 使用JScript.NET创建asp.net页面(五)
- · 使用JScript.NET创建asp.net页面(六)
- · 使用JScript.NET创建asp.net页面(七)
- · 有好东西不要自己藏着,拿出来大家分享啊,这是我的:关于bate2的MD5加密方法.
- · 转:ASP.NET验证控件详解
- · 在ASP.NET中获取文件属性
- · datalist分页(codebehind部分)
- · datalist分页(cj168.DataAccess.MagsDB中的2个方法)
- · datalist分页(页面部分)
- · .net beta2 操作cookie的例子(转)
- · 如何在DataGrid控件中隐藏列
- · Common ASP.NET Code Techniques (DPC&DWCReference)--1
- · Common ASP.NET Code Techniques (DPC&dwc Reference)--2
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--3
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--4
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--5
- · Common ASP.NET Code Techniques (DPC&DWC Reference)--6
- · 在datagrid中删除时确定(精华区的补充)
- · Displaying ListView items - with class! by Rob Birdwell
- · 一个带checkbox的webcontrol
- · 保护 XML Web 服务免受黑客攻击 [第一部分]
- · 保护 XML Web 服务免受黑客攻击, [第二部分]
- · treeview的源代码
- · 在datagrid中删除时确定?(转)
- · vs.net beta 2中利用DataGrid分页详解
- · COM组件对象与.NET类对象的相互转换
- · 关于DataGrid对象的属性设置(VB)
- · XmlNodeList
- · 用 FormsAuthentication.SetAuthCookie 做权限验证
- · 在datalist中选取数据。
- · Finding a Control Inside a Template
- · DataBinding DropDownList
- · 用asp.net向其他服务器post一条信息
- · Asp.net中用核选框显示数据的方法及ButtonColumn的使用方法
- · 如何在DataGrid控件中实现编辑、删除、分类以及分页操作
- · .NET框架类览胜( Ccident Net )
- · 使用JScript.NET创建asp.net页面(一)
