GridView操作
来源:互联网 发布:申请淘宝卖家账号注册 编辑:程序博客网 时间:2024/05/17 01:13
//前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewTest.aspx.cs" Inherits="WebApplication.GridViewTest" %>
<!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 getToPage() {
window.location.href = "myTestWebForm.aspx";
}
//解决gridview分页时动态地对checkbox选中状态的处理。
function SetCheckBoxStatus() {
var tb = document.getElementById("<%=GridView1.ClientID %>");
var eleName = document.getElementById("<%=txtName.ClientID %>").value;
for (var i = 1; i < tb.rows.length - 1; i++) {
var chk = tb.rows[i].cells[0].firstChild;
if (eleName.indexOf(tb.rows[i].cells[2].innerText + ",") > -1) {
chk.checked = true;
}
}
}
function checkbox_onchange() {
var tb = document.getElementById("<%=GridView1.ClientID %>");
var eleName = document.getElementById("<%=txtName.ClientID %>");
for (var i = 1; i < tb.rows.length - 1; i++) {
var chk = tb.rows[i].cells[0].firstChild;
var id = tb.rows[i].cells[2].innerText;
eleName.value = eleName.value.replace(id + ",", "");
if (chk.checked) {
eleName.value = eleName.value + id + ",";
}
}
}
function getCheckValue() {
var cks = document.getElementsByName("ckName");
for (var i = 0; i < cks.length; i++) {
if (cks[i].checked) {
alert(cks[i].id);
}
else {
alert("没有选中的check标签");
}
}
}
function getCheckValue(e) {
if (e.checked) {
alert(e.test);
}
}
function getVal() {
var grid = document.getElementById("<%=GridView1.ClientID %>");
var inputs = grid.getElementsByTagName("input");
alert(inputs[1].id);
alert(inputs[2].id);
alert(inputs[3].id);
alert(inputs[4].id);
alert(inputs[5].id);
alert(inputs[6].id);
}
</script>
</head>
<body onload="getCheckValue()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div style="width: 70%;">
<table style="width: 100%;">
<tr>
<td align="left" style="background-color: Blue;">
部门管理
</td>
</tr>
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="3" PageSize="4" Width="100%"
AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated" AllowPaging="True" DataKeyNames="UserID"
OnPageIndexChanging="GridView1_PageIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="用户ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("UserID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<center>
<asp:Label ID="LabID" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>
</center>
</ItemTemplate>
<ControlStyle Width="25%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="用户名">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<center>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</center>
</ItemTemplate>
<ControlStyle Width="25%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="密码">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<center>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
</center>
</ItemTemplate>
<ControlStyle Width="25%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<EditItemTemplate>
<%--<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>--%>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<center>
<%--<asp:LinkButton ID="LinkButton1" CommandName="Delete" OnClientClick="alert('确定要删除吗?')" runat="server">删除</asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton1" CommandName="Delete" runat="server">删除</asp:LinkButton>
<%--<asp:Label ID="Label2" runat="server">更新</asp:Label>--%>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Edit">更新</asp:LinkButton>
</center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="是否选中">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Ischeck") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Ischeck") %>' Enabled="false"
onclick="checkbox_onchange()" />
选中2<input type="checkbox" id="myCk" name="ckName" test='<%# Eval("Password") %>' onclick="getCheckValue(this)" />
<input type="button" value="选中值" onclick="getVal()" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td colspan="5">
用户名:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
密 码:
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnAdd" runat="server" Text="添加" Width="78px" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
</div>
<div style="width: 30%;">
<table style="width: 20%;">
<tr>
<td>
测试1
</td>
<td>
测试2
</td>
</tr>
</table>
</div>
填写用户ID:<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
<br />
获得用户名:<asp:TextBox ID="txtGetUserName" runat="server"></asp:TextBox>
   
<asp:Button ID="btnGetUserName" runat="server" Text="获得用户名" OnClick="btnGetUserName_Click" />
    当点击文本框时进行页面跳转:<input type="text" id="myTest" onclick="getToPage()" />
<asp:Button ID="btnRegister" runat="server" OnClick="btnRegister_Click" Text="用于注册脚本的按钮" />
</form>
</body>
<script language="javascript" type="text/javascript">
SetCheckBoxStatus();
</script>
</html>
//后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Data;
using System.Collections;
namespace WebApplication
{
public partial class GridViewTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//如果不是数据回传的话
{
bind();//实现数据绑定的方法
}
//ScriptManager.RegisterClientScriptBlock(btnRegister, GetType(), "click", "alert('hello')", true);//用于为控件在特定的时机注册脚本的语法
TextBox1.Text = "test";
}
//实现数据绑定的方法
protected void bind()
{
GridView1.DataSource = BLL.UserManage.GetAllUsers();
GridView1.DataBind();
}
/// <summary>
/// 实现添加的功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
Users user = new Users();
user.UserName = txtName.Text.Trim();
user.Password = txtPwd.Text.Trim();
bool b= BLL.UserManage.Add(user);//得到添加方法的返回值
if (b)//如果添加成功的话
{
ClientScript.RegisterClientScriptBlock(GetType(),"onload","alert('添加成功')",true);//显示添加成功
//Response.Redirect("~/Login.aspx");//跳转到测试页面
bind();
}
}
//删除的方法
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//int UserID = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text);
int UserID = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("LabID") as Label).Text);//需要拿到GridView中用户编号的字段
bool bol = BLL.UserManage.Delete(UserID);
if (bol)
{
bind();
}
else
{
//Response.Write("<script>alert('删除失败');location.href=Default.aspx;</script>");
ClientScript.RegisterClientScriptBlock(GetType(), "onload", "alert('删除成功')", true);//删除成功
}
}
//修改的方法
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Users user = new Users();
//这里的控件取的都是编辑模板中控件
user.UserID = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text);
user.UserName = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text.ToString();
user.Password = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text.ToString();
bool bol = BLL.UserManage.Modify(user);
if (bol)
{
//Response.Write("<script>alert('修改成功');</script>");
ClientScript.RegisterClientScriptBlock(GetType(), "onload", "alert('修改成功')", true);
GridView1.EditIndex = -1;
bind();
}
else
{
//Response.Write("<script>alert('修改失败');</script>");
ClientScript.RegisterClientScriptBlock(GetType(), "onload", "alert('修改失败')", true);
}
}
//修改状态
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//获得用户名
protected void btnGetUserName_Click(object sender, EventArgs e)
{
string userID=txtUserID.Text;//得到用户主键ID
if (string.IsNullOrEmpty(userID))
{
ClientScript.RegisterClientScriptBlock(GetType(),"onload","alert('用户编号不能为空')",true);
return;//跳出当前函数
}
if (CommonHelper.IsNumeric(userID))
{
DataTable table = BLL.UserManage.getNameById(Convert.ToInt32(userID));
if (table.Rows.Count<=0)
{
ClientScript.RegisterClientScriptBlock(GetType(),"onload","alert('不存在输入的用户编号')",true);
return;//跳出当前循环
}
txtGetUserName.Text = table.Rows[0]["Fusername"].ToString();
}
else
{
ClientScript.RegisterClientScriptBlock(GetType(),"onload","alert('输入的用户编号不正确')",true);
}
}
//删除弹出确认对话框的方法
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//点击表格打开
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
/*
((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
*/
/*
((LinkButton)e.Row.Cells[3].FindControl("LinkButton1")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
*/
((LinkButton)e.Row.Cells[3].FindControl("LinkButton1")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
}
}
//方法一:
/*
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++) {
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow) {
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
*/
}
//鼠标移动时显示颜色方法二:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentColor = this.style.backgroundColor;this.style.backgroundColor='#CCC';");
e.Row.Attributes.Add("onmouseout", " this.style.backgroundColor=currentColor;");
}
}
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
myCheckBox.Checked = true;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
GridView1.PageIndex = e.NewPageIndex;
bind();
RePopulateValues();
}
//用于注册脚本的按钮
protected void btnRegister_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "hello";
}
}
}
- GridView操作
- GridView 操作
- GridView 操作
- GridView操作
- GridView操作
- GridView 操作
- GridView操作
- GridView操作
- GridView的常用操作
- GridView 18种操作
- GridView 18种操作
- GridView的几个操作
- GridView 18种操作
- GridView 18种操作
- GridView操作初步
- GridView中的DropDownList操作
- GridView 操作汇总
- GridView的操作大全
- js判断背景图片是否加载成功
- python 实现的简单的BloomFilter
- 面试八十题(一)
- Laravel swiftmailer(messages) 发邮件,基于smtp或linux sendmail
- Cocos2d-x里面如何实现MVC(四)
- GridView操作
- 数据库面试题目
- ORACLE关于DELETE后空间不释放问题
- delphi7中删除掉 File->reopen中的选项
- Hibernate学习资料整理-1
- DXUT框架剖析(1)
- Cocos2d-x里面如何实现MVC(三)
- Embedding Tomcat Into Java Applications
- C# 中的委托和事件