您的位置:首页 > 编程语言 > ASP

ASP.net Repeater控件隐藏列 自写一个测试

2011-09-09 13:16 459 查看
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 src="jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
function fun() {
var tb = document.getElementById("table1");
var len = tb.rows.length;
var i = 0;
for (i; i < len; i++) {
tb.rows[i].cells[0].setAttribute("display","none");
}

}

function ViewOnly() {
//隐藏第一列
$("#thEdit").hide();

}
function ShowViewOnly() {
//显示第一列
$("#thEdit").show();

}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />

<asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_ItemCommand" OnItemDataBound="rptData_ItemDataBound">
<HeaderTemplate>
<table class="TABLE1" id="table1" cellspacing="0" cellpadding="0" style="width:100%;border-collapse:collapse;">
<tr>
<th  scope="col" style=" width:60px" id="thEdit">测试列1</th>

<th  scope="col" style=" width:60px" id="th1">测试列2</th>

</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td runat="server" id="tdEdit">
<asp:TextBox ID="txtOrderBy" runat="server" Text='<%#Eval("startip")%>'  Width="30px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtHotCount" runat="server" Text='<%#Eval("country")%>'  Width="30px"></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<asp:CheckBox ID="CB_bd" runat="server" Text="选中" />
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
public static readonly string connstr = "配置数据库连接地址";

protected void Page_Load(object sender, EventArgs e)
{

//if (!IsPostBack)
//{
DataTable dt = new DataTable();
using (SqlConnection sqlconn = new SqlConnection(connstr))
{
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand();
command.CommandText = "select top 10 * from 表";
command.CommandType = CommandType.Text;
command.Connection = sqlconn;
adapter.SelectCommand = command;
adapter.Fill(dt);
}
rptData.DataSource = dt;
rptData.DataBind();
//}

}
protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
{

}
protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{

HtmlTableCell tdEdit = e.Item.FindControl("tdEdit") as HtmlTableCell;

if (this.CB_bd.Checked == true)
{
tdEdit.Visible = false;
}

}

}

protected void  Button1_Click(object sender, EventArgs e)
{
//调用前台的Javascript函数

if (this.CB_bd.Checked == true)
{
ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>ViewOnly();</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>ShowViewOnly();</script>");
}

}

}


效果图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: