询问者
如何用脚本和DropDownList控件来隐藏GridView的某列?

问题
-
下面是一个可以批量更新的GridView的简单示例,为了使用方便有时候需要显示或隐藏某些列,因此我设置了DropDownList控件来控制显示或隐藏某些列。但是在使用过程中,我反复测试,还是找不到利用DropDownList控件的Visible属性,来确保GridView的正常运行。一旦隐藏了某些列,再点击更新,则被隐藏的部分数据被清空。因此我想尝试用编写脚本的方式来显示或隐藏某些列,但我还不知道该如何入手,请大家帮忙看看!谢谢!
-----------------------------前台--------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> <style type="text/css"> .style1 { width: 73%; } </style> </head> <body> <form id="form1" runat="server"> <div> <table align="center" class="style1"> <tr> <td align="left"> <asp:GridView ID="GridView1" Runat="server" DataKeyNames="ID,au_lname" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" Width="854px" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <Columns> <asp:BoundField DataField="ID" HeaderText="编号" /> <asp:TemplateField SortExpression="au_lname" HeaderText="员工姓名" > <ItemTemplate> <asp:TextBox Runat="server" Text='<%# Bind("au_lname") %>' ID="txtName"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="员工性别"> <ItemTemplate> <asp:TextBox Runat="server" Text='<%# Bind("sex") %>' ID="txtSex"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="员工学历"> <ItemTemplate> <asp:TextBox ID="txtState" runat="server" Text='<%# Bind("state") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="电话号码"> <ItemTemplate> <asp:TextBox ID="txtPhone" runat="server" Text='<%# Bind("phone") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="家庭地址"> <ItemTemplate> <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("address") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </td> </tr> <tr> <td> <asp:Button ID="Button1" Runat="server" Text="一次性修改所有行" OnClick="Button1_Click" Width="128px" /> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db_DataControlConnectionString %>" SelectCommand="SELECT ID, au_lname, sex, state, phone, address FROM tb_mrEmp" UpdateCommand="update tb_mrEmp set au_lname=@name,sex=@sex,state=@state,phone=@phone,address=@address where ID=@ID"> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="sex" Type="String" /> <asp:Parameter Name="state" Type="String" /> <asp:Parameter Name="phone" Type="String" /> <asp:Parameter Name="address" Type="String" /> <asp:Parameter Name="ID" /> </UpdateParameters> </asp:SqlDataSource> </td> </tr> </table> </div> </form> </body> </html>
--------------------------------后台--------------------------
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { GridViewRow row = GridView1.Rows[i]; SqlDataSource1.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("txtName")).Text; //SqlDataSource1.UpdateParameters[0].DefaultValue = GridView1.DataKeys[i].Values[1].ToString(); SqlDataSource1.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[1].FindControl("txtSex")).Text; SqlDataSource1.UpdateParameters[2].DefaultValue = ((TextBox)row.Cells[1].FindControl("txtState")).Text; SqlDataSource1.UpdateParameters[3].DefaultValue = ((TextBox)row.Cells[1].FindControl("txtPhone")).Text; SqlDataSource1.UpdateParameters[4].DefaultValue = ((TextBox)row.Cells[1].FindControl("txtAddress")).Text; SqlDataSource1.UpdateParameters[5].DefaultValue = GridView1.DataKeys[i].Values[0].ToString(); SqlDataSource1.Update(); } } }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
全部回复
-
<script language="javascript"><!-- function change() { if(document.all.ddl_class.value=="0") { hongtou.style.display="none"; wailai.style.display="none"; wailai1.style.display="none"; } else if(document.all.ddl_class.value=="1") { hongtou.style.display="block"; wailai.style.display="none"; wailai1.style.display="none"; } else { hongtou.style.display="none"; wailai.style.display="block"; wailai1.style.display="block"; } } </script> <asp:DropDownList ID="ddl_class" runat="server" onchange="change()"> <asp:ListItem Value="0">新建文件</asp:ListItem> <asp:ListItem Value="1">红头文件</asp:ListItem> <asp:ListItem Value="2">外来文件</asp:ListItem> </asp:DropDownList> <table> <tr id="hongtou" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>份数:</td> <td></td> </tr> <tr id="wailai" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>单位:</td> <td></td> </tr> <tr id="wailai1" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>联系人:</td> <td></td> </tr> </table>
试试看这个例子.. -
不知道我理解是否正确,下面是两段脚本代码放在一起,是否把黑体字加下划线的部分去掉就可以了?
两段代码的开头
<script type="text/javascript">
<script language="javascript">
不太一样,我保留了
<script type="text/javascript">
<script type="text/javascript"> function enter(obj) { var tr = obj.parentNode.parentNode; if (event.keyCode == 40) //向下的时候 { if (tr.rowIndex < tr.parentNode.rows.length - 1) tr.parentNode.rows[tr.rowIndex + 1].cells[obj.parentNode.cellIndex].children[0].focus(); return; } if (event.keyCode == 37) //向左的时候 { if (obj.parentNode.cellIndex > 0) { var i = 1; var node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex - i].children[0]; while (!(node.nodeName == "INPUT"||node.nodeName == "TEXTAREA") && obj.parentNode.cellIndex - 1 >= 0) { i++; node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex - i].children[0]; } node.focus(); node.select(); } return; } if (event.keyCode == 39) //向右的时候 { if (obj.parentNode.cellIndex < tr.cells.length - 1) { var i = 1; var node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex + i].children[0]; while (!(node.nodeName == "INPUT" || node.nodeName == "TEXTAREA") && obj.parentNode.cellIndex < tr.cells.length - 1) { i++; node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex + i].children[0]; } node.focus(); node.select(); } return; } if (event.keyCode == 38) //向上的时候 { if (tr.rowIndex > 1) tr.parentNode.rows[tr.rowIndex - 1].cells[obj.parentNode.cellIndex].children[0].focus(); return; } } <script language="javascript"><!-- function change() { if(document.all.ddl_class.value=="0") { hongtou.style.display="none"; wailai.style.display="none"; wailai1.style.display="none"; } else if(document.all.ddl_class.value=="1") { hongtou.style.display="block"; wailai.style.display="none"; wailai1.style.display="none"; } else { hongtou.style.display="none"; wailai.style.display="block"; wailai1.style.display="block"; } } </script> </script>
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年6月1日 0:56
-
意思是一样的 你完全可以 按照我下面的写法!
<script type="text/javascript"> function enter(obj) { var tr = obj.parentNode.parentNode; if (event.keyCode == 40) //向下的时候 { if (tr.rowIndex < tr.parentNode.rows.length - 1) tr.parentNode.rows[tr.rowIndex + 1].cells[obj.parentNode.cellIndex].children[0].focus(); return; } if (event.keyCode == 37) //向左的时候 { if (obj.parentNode.cellIndex > 0) { var i = 1; var node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex - i].children[0]; while (!(node.nodeName == "INPUT"||node.nodeName == "TEXTAREA") && obj.parentNode.cellIndex - 1 >= 0) { i++; node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex - i].children[0]; } node.focus(); node.select(); } return; } if (event.keyCode == 39) //向右的时候 { if (obj.parentNode.cellIndex < tr.cells.length - 1) { var i = 1; var node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex + i].children[0]; while (!(node.nodeName == "INPUT" || node.nodeName == "TEXTAREA") && obj.parentNode.cellIndex < tr.cells.length - 1) { i++; node = tr.parentNode.rows[tr.rowIndex].cells[obj.parentNode.cellIndex + i].children[0]; } node.focus(); node.select(); } return; } if (event.keyCode == 38) //向上的时候 { if (tr.rowIndex > 1) tr.parentNode.rows[tr.rowIndex - 1].cells[obj.parentNode.cellIndex].children[0].focus(); return; } } function change() { if(document.all.ddl_class.value=="0") { hongtou.style.display="none"; wailai.style.display="none"; wailai1.style.display="none"; } else if(document.all.ddl_class.value=="1") { hongtou.style.display="block"; wailai.style.display="none"; wailai1.style.display="none"; } else { hongtou.style.display="none"; wailai.style.display="block"; wailai1.style.display="block"; } } </script>
-
感谢您的提示,但首先因为我使用了母版页,遇到了问题(不使用母版页就没问题)。下面是我把您的代码贴在含母版页的Web窗体后的代码、前台代码及错误提示(有4个错误提示),这个问题以前也遇到过,没有彻底解决,但可以运行;这一次不能运行了,请问该如何处理呢?
-----------------------母版页---------------------
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body> <form id="form1" runat="server"> <div> <table border="0" style="height: 75px; width: 100%;"><%--边框的厚度,注意引号--%> <tr> <td> <%--首先要设计网站的.sitemap文件(站点地图文件),再用以下的控件来实现界面中的样式--%> <%--三种网站导航控件:SiteMapPath-全部列出来,其中RenderCurrenNodeAsLink属性指示当前节点是否呈现为节点:True ;TreeView和SiteMapTath类似--%> <%--以上三种控件的数据源都是SiteMapDataSource--%> <%--此处使用Menu-可以展开的控件--%> <strong > <span style="font-size:10pt;font-family:Tanoma"><%--点击span,在属性Style后面可直接选择--%> <asp:Menu ID="Menu1" runat="server" Width="100%" DataSourceID="SiteMapDataSource1" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Overline="False" Font-Size="0.9em" ForeColor="#990000" Height="32px" Orientation="Horizontal" StaticSubMenuIndent="10px" > <%--静态、动态选项风格,在“样式”里--%> <StaticSelectedStyle BackColor="#FFCC66" /> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DynamicHoverStyle BackColor="#990000" ForeColor="White" /> <DynamicMenuStyle BackColor="#FFFBD6" /> <DynamicSelectedStyle BackColor="#FFCC66" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DataBindings> <asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="Url" TextField="Title" /> </DataBindings> <DynamicHoverStyle BackColor="#666666" ForeColor="White" /> <StaticHoverStyle BackColor="#990000" ForeColor="White" /> </asp:Menu> <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> <asp:SiteMapPath ID="SiteMapPath1" runat="server" Font-Names="Verdana" Font-Size="0.8em" PathSeparator=" : "> <PathSeparatorStyle Font-Bold="True" ForeColor="#990000" /> <CurrentNodeStyle ForeColor="#333333" /> <NodeStyle Font-Bold="True" ForeColor="#990000" /> <RootNodeStyle Font-Bold="True" ForeColor="#FF8000" /> </asp:SiteMapPath> </span> </strong> </td> </tr> <%--以下是内容页,母版页之外的可编辑区域--%> <tr> <td> <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server"> <span style ="font-size: 10px; font-family: Tahoma;" ></span> </asp:ContentPlaceHolder> </td> </tr> </table> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html>
-----------------------------前台代码------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default14.aspx.cs" Inherits="Default14" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <script language="javascript"><!-- function change() { if(document.all.ddl_class.value=="0") { hongtou.style.display="none"; wailai.style.display="none"; wailai1.style.display="none"; } else if(document.all.ddl_class.value=="1") { hongtou.style.display="block"; wailai.style.display="none"; wailai1.style.display="none"; } else { hongtou.style.display="none"; wailai.style.display="block"; wailai1.style.display="block"; } } </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <asp:DropDownList ID="ddl_class" runat="server" onchange="change()"> <asp:ListItem Value="0">新建文件</asp:ListItem> <asp:ListItem Value="1">红头文件</asp:ListItem> <asp:ListItem Value="2">外来文件</asp:ListItem> </asp:DropDownList> <table> <tr id="hongtou" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>份数:</td> <td></td> </tr> <tr id="wailai" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>单位:</td> <td></td> </tr> <tr id="wailai1" style="DISPLAY:none" mce_style="DISPLAY:none"> <td>联系人:</td> <td></td> </tr> </table> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> </asp:Content>
--------------------错误截图1---------------------
-----------------------------错误截图2---------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
谢谢您的解答,我是做好了备份再进行测试的,不是正式项目。
按照您的提示我删除了<!- 这个符号,但是又出现了新问题。我估计是因为我使用了母版页,造成了某些冲突吧(母版页的代码在上面有)!
以下是错误的截图(共有4个错误):
-------------------------------图1--------------------------------
----------------------------图2----------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年6月1日 8:15
-
关于那个type的 可以改成
<script type="text/javascript"> </script>
mce_style="display:none"可以删掉 留 style="display:none" 这句话是说 隐藏这个tr
那个警告不是错误吧
应该可以编译通过的啊?
- 已编辑 chenrensongModerator 2012年6月1日 14:07
- 已标记为答案 Song TianModerator 2012年6月5日 1:58
- 取消答案标记 linjiangxian11 2012年6月6日 7:09
-
谢谢您的回复!代码补上,还未发现该如何解决这个问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default15.aspx.cs" Inherits="Default15" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function change() { if (document.all.CheckBoxList1.Items[0].selected == false) { GridView1.Columns[0].style.display="none"} if (document.all.CheckBoxList1.Items[1].selected == false) {GridView1.Columns[1].style.display = "none"} if (document.all.CheckBoxList1.Items[2].selected == false) {GridView1.Columns[1].style.display = "none"} } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList ID="CheckBoxList1" runat="server" onchange="change()" AutoPostBack="True"> <asp:ListItem Value="ReceivableID">ReceivableID</asp:ListItem> <asp:ListItem Value="ContractNumberR">ContractNumberR</asp:ListItem> <asp:ListItem Value="ManageProjectR">ManageProjectR</asp:ListItem> </asp:CheckBoxList> <table> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns > <asp:BoundField DataField="ReceivableID" HeaderText="ReceivableID" SortExpression="ReceivableID" /> <asp:BoundField DataField="ContractNumberR" HeaderText="ContractNumberR" SortExpression="ContractNumberR" /> <asp:BoundField DataField="ManageProjectR" HeaderText="ManageProjectR" SortExpression="ManageProjectR" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" SelectCommand="SELECT [ReceivableID], [ContractNumberR], [ManageProjectR] FROM [ReceivableTable]"> </asp:SqlDataSource> </table> </div> </form> </body> </html>
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!