這兩天做的作業都得用到visual studio 越來越發現其功能真心強大
前幾天Asp.Net做了個界面增刪查改的作業(連接數據庫),用到了個組件GridView,感覺很強大
在這裏小結一下(這裏主要說下字段和事件):
字段,BoundField 顯示數據源中某個字段的值。這是 GridView控件的默認列類型。
ButtonField 爲 GridView控件中的每個項顯示一個命令按鈕。可以創建一列自定義按鈕控件,如“添加”按鈕或“移除”按鈕。
CheckBoxField 爲 GridView控件中的每一項顯示一個複選框。此列字段類型通常用於顯示具有布爾值的字段。
CommandField 顯示用來執行選擇、編輯或刪除操作的預定義命令按鈕。
HyperLinkField 將數據源中某個字段的值顯示爲超鏈接。此列字段類型允許您將另一個字段綁定到超鏈接的 URL。
ImageField 爲 GridView控件中的每一項顯示一個圖像。
TemplateField 根據指定的模板爲 GridView控件中的每一項顯示用戶定義的內容。此列字段類型允許您創建自定義的列字段。
事件,
RowCancelingEdit 在一個處於編輯模式的行的Cancel按鈕被單擊,但是在該行退出編輯模式之前發生。
RowCommand 單擊一個按鈕時發生。
RowCreated 創建一行時發生。
RowDataBound 一個數據行綁定到數據時發生。
RowDeleting, RowDeleted 這兩個事件都是在一行的Delete按鈕被單擊時發生。它們分別在該網格控件刪除
該行之前和之後激發。
RowEditing 當一行的Edit按鈕被單擊時,但是在該控件進入編輯模式之前發生。
RowUpdating,RowUpdated 這兩個事件都是在一行的Update按鈕被單擊時發生。它們分別在該網格控件更
新該行之前和之後激發。
SelectedIndexChanging, SelectedIndexChanged這兩個事件都是在一行的Select按鈕被單擊時發生。
它們分別在該網格控件處理選擇操作之前和之後激發。
Sorting, Sorted 這兩個事件都是在對一個列進行排序的超鏈接被單擊時發生。
它們分別在網格控件處理排序操作之前和之後激發
接下來把作業拿出來分析幾個事件:
之前我已說過如何連接Mysql數據庫,這個界面增刪查改作業也是基於那個基礎,當然也是之前那個界面
我把添加頁面寫在同一個界面上,理解就行,代碼:
界面,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DataRefresh.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>界面增刪查改</title>
</head>
<body style="height: 34px">
<form id="form1" runat="server">
<div id="container">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="591px" CellPadding="4" ForeColor="#333333" GridLines="None"
OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="s_no" HeaderText="學號" />
<asp:BoundField DataField="s_name" HeaderText="名字" />
<asp:BoundField DataField="s_age" HeaderText="年齡" />
<asp:BoundField DataField="s_sex" HeaderText="性別" />
<asp:CommandField HeaderText="編輯" ShowEditButton="true"/>
<asp:CommandField HeaderText="刪除" ShowDeleteButton="true">
</asp:CommandField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<br />
學號:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
名字:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
年齡:
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
性別:
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<p>
<asp:Button ID="Btn_add" runat="server" Text="添加" OnClick="Btn_add_Click" />
</p>
</div>
</form>
</body>
</html>
實現類,
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace DataRefresh
{
public partial class WebForm1 : System.Web.UI.Page
{
MySqlConnection mySqlConn;//mysql連接對象
protected void Page_Load(object sender, EventArgs e)
{
string connStr = "Database=myschool;Data Source=localhost;User Id=root;Password=123";//連接字符串
mySqlConn = new MySqlConnection(connStr);
mySqlConn.Open();
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
//創建MySqlDataAdapter對象執行查詢
MySqlDataAdapter DataAdapter = new MySqlDataAdapter("select * from student", mySqlConn);
DataSet dataset = new DataSet();
// 填充DataSet對象
DataAdapter.Fill(dataset, "student");
//將數據顯示在gridview中
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new string[] { "s_no" };//主鍵
GridView1.DataBind();
}
//刪除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlStr = "delete from student where s_no=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + ";";
//System.Diagnostics.Debug.Write(sqlStr);
MySqlCommand mySqlCmd = new MySqlCommand(sqlStr,mySqlConn);
mySqlCmd.ExecuteNonQuery();
bind();
}
//gridview改變的時候設置每一行的id
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());
}
//獲取事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//System.Diagnostics.Debug.Write(e.CommandName);
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow gvr = GridView1.Rows[e.RowIndex];
string s_no = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
string s_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string s_age = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string s_sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string sql = "update student set s_no='" + s_no + "',s_name='" + s_name + "',s_age='" + s_age + "',s_sex='" + s_sex + "' where s_no=" + ID + ";";
System.Diagnostics.Debug.Write(sql);
MySqlCommand mySqlCmd = new MySqlCommand(sql, mySqlConn);
mySqlCmd.ExecuteNonQuery();
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//編輯
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//int index = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//添加
protected void Btn_add_Click(object sender, EventArgs e)
{
//Response.Redirect("WebForm2.aspx"); 跳轉
int sno = int.Parse(TextBox1.Text);
int age = int.Parse(TextBox3.Text);
string sql = "insert into student(s_no,s_name,s_age,s_sex) values('"+sno+"' ,'" + TextBox2.Text + "','" + age + "','" + TextBox4.Text + "');";
//System.Diagnostics.Debug.Write(sql);
MySqlCommand mySqlCmd = new MySqlCommand(sql, mySqlConn);
mySqlCmd.ExecuteNonQuery();
bind();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
}
}
效果,
這個簡單地界面中實現了增(Btn_add_Click),刪(GridView1_RowDeleting),編輯(GridView1_RowUpdating GridView1_RowEditing GridView1_RowCancelingEdit);其中用到了5個事件,實現了刪除和編輯。這些事件只需
要在GridView(界面)中註冊然後在代碼裏實現就可以了,GridView的強大之處還在於它能適用於各種數據庫!
覺得幾點重要的地方:
1. bind()方法,實現了數據與GridView的綁定,之後主要起刷新作用
2. 在Page_Load()中調用bind()方法時必須加判斷if (!IsPostBack),作用是看GridView中的數據是否發生改變
3. 在web項目中輸出語句得用 System.Diagnostics.Debug.WriteLine();