//绑定控件 private void BindGridView() { DataSet ds = Getnews(); NewsGridView.DataSource = ds.Tables["aa"].DefaultView; NewsGridView.DataBind(); }
protected void Delete_Click(object sender, EventArgs e) { for (int i = 0; i < NewsGridView.Rows.Count; i++) { CheckBox chkSelect = (CheckBox)NewsGridView.Rows[i].FindControl("chkSelect"); if (chkSelect.Checked) { bll.DeleteNews(int.Parse(NewsGridView.DataKeys[i].Value.ToString())); } } NewsGridView.DataSource = bll.ShowAllNews(); NewsGridView.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { NewsGridView.DataSource = ParseRssFile(); NewsGridView.DataBind(); } catch (Exception ex) { //Serilog Logger to save error in text file location D:\\ SerilogLogger.WriteErrorLog(ex.Message); } } }
protected void Page_Load(object sender, EventArgs e) { user = (UserModel)Session["User"]; if (Session["User"] == null || Session["User"].ToString() == "" || user.UserType != 1) { Response.Redirect("../Login.aspx"); } else { if (!IsPostBack) { NewsGridView.DataSource = bll.ShowAllNews(); NewsGridView.DataBind(); } } }
private void showData() { string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(strConn); string code = $"WITH CTE AS (SELECT ROW_NUMBER() OVER ( ORDER BY Sticky DESC, id DESC) RowNumber, id, CONVERT(char(10), NewsDate, 20)NewsDate, NewsTitle, NewsContent, Sticky, PrePhotoName, Introduction FROM News ) SELECT * FROM CTE where ROWNUMBER >=((@page - 1) * 5 + 1) and ROWNUMBER <=(@page * 5)"; SqlCommand command = new SqlCommand(code, connection); command.Parameters.Add("@page", SqlDbType.Int); command.Parameters["@page"].Value = Convert.ToInt32(Request.QueryString["page"] ?? "1"); connection.Open(); SqlDataReader dataReader = command.ExecuteReader(); NewsGridView.DataSource = dataReader; //把這個資料表跟資料(reader)作雙向繫結 NewsGridView.DataBind(); connection.Close(); }
private void getGridView() { string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //連接資料庫 SqlConnection Conn = new SqlConnection(ConnectionString); Conn.Open(); // SqlDataAdapter 執行SQL命令抓取資料 SqlDataAdapter Adapter = new SqlDataAdapter("SELECT id, CONVERT(char(10), NewsDate, 20)NewsDate, NewsTitle, Sticky, PrePhotoName, Introduction FROM News ORDER BY Sticky DESC, id DESC", Conn); //using System.Data; //宣告DataTable 將SqlDataAdapter匯入,DataBind將資料來源繫結至 GridView DataTable dataTable = new DataTable(); Adapter.Fill(dataTable); NewsGridView.DataSource = dataTable; NewsGridView.DataBind(); //關閉SQL連接 Conn.Close(); }
protected void Search_Click(object sender, EventArgs e) { Hashtable htabel = new Hashtable(); if (chkNoticeTitle.Checked) { htabel.Add("Title", txtNoticeTitle.Text.Trim()); } if (chkPerson.Checked) { htabel.Add("NoticePerson", txtPerson.Text.Trim()); } string strsql = Ultility.GetConditionClause(htabel, chkExact.Checked); if (chkNoticeDate.Checked) { strsql += " and NoticeDate between '" + txtBeginDate.Value.Trim() + "' and '" + txtEndDate.Value.Trim() + "'"; } NewsGridView.DataSource = bll.GetNewsByCondition(strsql); NewsGridView.DataBind(); }
private void getGridView() { string sql = Session["keyword"].ToString(); string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //連接資料庫 SqlConnection Conn = new SqlConnection(ConnectionString); Conn.Open(); // SqlDataAdapter 執行SQL命令抓取資料 SqlDataAdapter Adapter = new SqlDataAdapter(sql, Conn); //using System.Data; //宣告DataTable 將SqlDataAdapter匯入,DataBind將資料來源繫結至 GridView DataTable dataTable = new DataTable(); Adapter.Fill(dataTable); NewsGridView.DataSource = dataTable; NewsGridView.DataBind(); if (dataTable.Rows.Count == 0) { lblError.Visible = true; } //關閉SQL連接 Conn.Close(); }
protected void Show_Click(object sender, EventArgs e) { NewsGridView.DataSource = bll.ShowAllNews(); NewsGridView.DataBind(); }