GridView自定義列資料繫結,和自定義頒功能
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.Common;
using System.Configuration;
using System.Data.SqlClient;
public partial class Delete_hope : System.Web.UI.Page
{
// string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
private int pagesize = 10;
private int pagecount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Server.Transfer("logo.aspx");
}
if (!Page.IsPostBack)
{
Init();
DataLoad();
}
}
private void Init()
{
int pagecon = Getcount();
if (pagecon % pagesize == 0)
{
pagecount = pagecon / pagesize;
}
else
{
pagecount = pagecon / pagesize + 1;
}
ViewState["PageCount"] = pagecount;
LinkButton5.Enabled = false;
}
private void DataLoad()
{
this.GridView1.DataSource = GetContents("", Convert.ToInt32(txtpage.Text), pagesize);
this.GridView1.DataBind();
}
protected void btnfirst_Click(object sender, EventArgs e)
{
this.txtpage.Text = "1";
LinkButton5.Enabled = false;
DataLoad();
}
protected void btnlast_Click(object sender, EventArgs e)
{
//最後一頁
LinkButton5.Enabled = true;
this.txtpage.Text = ViewState["PageCount"].ToString();
LinkButton6.Enabled = false;
DataLoad();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
//上頁
int i = Convert.ToInt32(txtpage.Text);
if (0 == (i-2))
{
LinkButton5.Enabled = false;
}
LinkButton6.Enabled = true;
txtpage.Text = Convert.ToString(i - 1);
DataLoad();
}
protected void LinkButton6_Click(object sender, EventArgs e)
{
//下頁
int i = Convert.ToInt32(txtpage.Text);
if (Convert.ToInt32(ViewState["PageCount"])-1 == i)
{
LinkButton6.Enabled = false;
}
LinkButton5.Enabled = true;
txtpage.Text = Convert.ToString(i + 1);
DataLoad();
}
protected void Button2_Click(object sender, EventArgs e)
{
LinkButton5.Enabled = false;
LinkButton6.Enabled = false;
if (Convert.ToInt32(ViewState["PageCount"])>=Convert.ToInt32(txtpage.Text))
DataLoad();
}
public int Getcount()
{
int i = 0;
string s = "select count(*) from hopeperson";
DataTable dt = ExecuSQLtable(s);
i = Convert.ToInt32(dt.Rows[0][0]);
return i;
}
string cmdTxt = "";
public DataTable GetContents(string orderFields, int pagenum, int pagesize)
{
cmdTxt = "Select Row_Number() Over (Order By ID) as RowNum,* From hopeperson ";
cmdTxt = "Select * From (" + cmdTxt + ") as T Where T.RowNum>" + (pagenum - 1) * pagesize + " And T.RowNum<=" + pagenum * pagesize;
DataTable dt = new DataTable();
dt = ExecuSQLtable(cmdTxt);
return dt;
}
public DataTable ExecuSQLtable(string cmdtxt)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(Class1.cnnstring))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdtxt;
DbDataAdapter myadp = new SqlDataAdapter(cmd);
myadp.Fill(dt);
}
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Response.Write(e.Row.Cells[0].Text);
//Response.Write((FindControl("Button3") as Button).Text);
if (e.Row.RowType != DataControlRowType.DataRow)
return;
if (e.Row.Cells[3].FindControl("HyperLink2")!=null)
{
HyperLink btn = e.Row.Cells[3].FindControl("HyperLink2") as HyperLink;
if (btn != null)
{
btn.NavigateUrl = "delte_hope.aspx?id=" + e.Row.Cells[0].Text;
}
}
}
void Delete_hope_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandName.ToString());
//throw new NotImplementedException();
}
void btn1_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandName.ToString());
}
protected void Button4_Click(object sender, EventArgs e)
{
Init();
DataLoad();
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
cmdTxt = "Select Row_Number() Over (Order By ID) as RowNum,* From hopeperson where
hopeid=@name";
DataTable dt = Class1.exetable(cmdTxt, new SqlParameter("@name", TextBox1.Text));
if (dt.Rows.Count != 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
else
{
Page.RegisterClientScriptBlock("aa", "<script language='javascript'>alert('查詢框不能為空!');</script>");
}
}
}
相關文章
- 深入解析 WezTerm 的自定義功能:鍵繫結和滑鼠繫結
- 自定義GridViewView
- 繫結自定義事件事件
- GridView 自定義模版自定義修改,刪除,分頁View
- 080 元件自定義事件-繫結元件事件
- 自定義列舉
- 自定義響應資料結構資料結構
- WPF快速指導4:資料繫結之繫結方向與自定義轉換器
- 1.22 JQuery5:繫結自定義事件jQuery事件
- 自定義GridView實現分割線解析View
- 08.Django自定義模板,自定義標籤和自定義過濾器Django過濾器
- 自定義資料型別資料型別
- @ConfigurationProperties實現自定義配置繫結
- 自定義SAP功能表
- js:陣列自定義排序JS陣列排序
- nodejs 命令列、自定義NodeJS命令列
- 自定義View:自定義屬性(自定義按鈕實現)View
- 自定義view總結View
- DM自定義資料型別資料型別
- PyTorch 自定義資料集PyTorch
- 【asp.net core】自定義模型繫結及其驗證ASP.NET模型
- 自定義限速功能實踐——Caffeine
- MySQL使用之五_自定義函式和自定義過程MySql函式
- MD04 增加自定義列
- 自定義控制元件總結和思考控制元件
- VC自定義資源
- iOS自定義控制元件:自定義TableView、CollectionView空資料佔點陣圖iOS控制元件View
- 聊聊如何自定義資料脫敏
- 自定義react資料驗證元件React元件
- echarts自定義提示框資料Echarts
- Datazen地圖Chart自定義資料地圖
- python 自定義資料分頁Python
- 自定義ImageView完成圓形頭像自定義View
- 解鎖自定義分享功能新姿勢
- WebView自定義長按圖片功能WebView
- uniapp增加自定義埋點功能APP
- 應用PPT的自定義放映功能
- 自定義限速功能實踐——Map 版本