📄 editorgridview.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
//该源码首发自www.51aspx.com(51aspx.com)
public partial class editorGridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["userID"] == null || Session["userID"].ToString() == "")
{
Page.Response.Redirect("default.aspx");
}
else
{
dataBind();
}
if (GVuseage.Rows.Count == 0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(),"script","alert('没有数据!');",true);
}
}
}
private void dataBind()
{
string ID = Session["userId"].ToString();
string sql2 = "select * from addresslist where userId='" + ID + "' order by id desc";
DataBase DB2 = new DataBase();
DataSet ds2 = DB2.DataAll(sql2);
GVuseage.DataSource = ds2;
GVuseage.DataKeyNames = new string[] { "id" };
GVuseage.DataBind();
return;
}
protected void GVuseage_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
int indexid = (int)GVuseage.DataKeys[e.RowIndex].Value;
string sql = "delete from AddressList where id=" + indexid;
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
dataBind();
conn.Close();
}
protected void GVuseage_RowEditing(object sender, GridViewEditEventArgs e)
{
GVuseage.EditIndex = e.NewEditIndex;
dataBind();
}
protected void GVuseage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVuseage.EditIndex = -1;
dataBind();
}
protected void GVuseage_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string uid = GVuseage.DataKeys[e.RowIndex].Values[0].ToString();
string newfrdName = ((TextBox)(GVuseage.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
string newfrdPhone = ((TextBox)GVuseage.Rows[e.RowIndex].FindControl("txtphone")).Text.Trim();
string newfrdMobilePhone = ((TextBox)GVuseage.Rows[e.RowIndex].FindControl("txtMobilePhone")).Text.Trim();
string newfrdAddress = ((TextBox)(GVuseage.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
string newfrdQQ = ((TextBox)(GVuseage.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
string newfrdEmail = ((TextBox)GVuseage.Rows[e.RowIndex].FindControl("txtEmail")).Text.Trim();
string sql2 = "update addresslist set frdName='" + newfrdName + "',frdPhone='" + newfrdPhone + "',frdMobilePhone='" + newfrdMobilePhone + "',frdAddress='" + newfrdAddress + "',frdQQ='" + newfrdQQ + "',frdEmail='" + newfrdEmail + "' where id=" + uid;
SqlCommand cmd = new SqlCommand(sql2, conn1);
conn1.Open();
try
{
cmd.ExecuteNonQuery();
GVuseage.EditIndex = -1;
lblmsg.Text = "更新成功!";
lblmsg.Style["color"] = "red";
}
catch(SqlException)
{
lblmsg.Text = "更新失败!";
lblmsg.Style["color"] = "red";
}
conn1.Close();
dataBind();
}
protected void Btnfirst_Click(object sender, EventArgs e)
{
GVuseage.PageIndex = 0;
GVuseage.EditIndex = -1;
dataBind();
lblmsg.Text = "共计" + GVuseage.PageCount + "页,现在已经是首页了";
}
protected void Btndescend_Click(object sender, EventArgs e)
{
if (GVuseage.PageIndex != 0)
{
GVuseage.PageIndex--;
GVuseage.EditIndex = -1;
lblmsg.Text = "共计" + GVuseage.PageCount + "页";
}
else
{
GVuseage.PageIndex = 0;
GVuseage.EditIndex = -1;
lblmsg.Text = "共计" + GVuseage.PageCount + "页,现在已经是首页了";
}
dataBind();
}
protected void Btnretrusive_Click(object sender, EventArgs e)
{
if (GVuseage.PageIndex != GVuseage.PageCount - 1)
{
GVuseage.PageIndex++;
GVuseage.EditIndex = -1;
lblmsg.Text = "共计" + GVuseage.PageCount + "页";
}
else
{
GVuseage.PageIndex = GVuseage.PageCount - 1;
GVuseage.EditIndex = -1;
lblmsg.Text = "共计" + GVuseage.PageCount + "页,现在已经是尾页了";
}
dataBind();
}
protected void Btnfinally_Click(object sender, EventArgs e)
{
if (GVuseage.Rows.Count == 0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", "alert('没有数据!');", true);
}
else
{
GVuseage.PageIndex = GVuseage.PageCount - 1;
GVuseage.EditIndex = -1;
dataBind();
lblmsg.Text = "共计" + GVuseage.PageCount + "页,现在已经是尾页了";
}
}
protected void GVuseage_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVuseage.PageIndex = e.NewPageIndex;
GVuseage.EditIndex = -1;
dataBind();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -