昨晚总结了一篇关于GridView中的事件的文章,末尾留了一点“下回分解”,有那么点点味道就对了。想想今天又是个热天气,索性把这个例子做出来~....记得刚开始,一言难尽。。算了,反正做出来就好。
注意,这里只是实现功能,没有做成类..废话out!
首先建立一个ASPX文件,打开工具箱,“放”出GridView控件,然后呢...看下面
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="登录名">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("admin") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("admin") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="密码">
<ItemTemplate>
<%# Eval("UserPassword")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserPassword") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:BoundField HeaderText="最后登录时间" DataField="logintime" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
</Columns>
<PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
<RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
可能很奇怪,都没有数据源,怎么还能DataBind数据?....嗯.....做程序,适当忽悠一下IDE,死不了的。当然,这里绑定的数据,跟数据库字段是一致的,忽悠也得有个限度~这也是算厚道吧(Hoho~~~),在下来,就真正实现里面的
增删了
之后,在cs文件里面实现增删改内容~
SqlConnection conn
=
null
;
string connstr = System.Configuration.ConfigurationManager.AppSettings[ " Connstring " ]; // 在Config中自己搞,不会的看书
protected void Page_Load( object sender, EventArgs e)
... {
if (!IsPostBack)//这句不会,去S吧
...{
GridViewBind();//做了一个绑定数据的方法.
}
}
private void GridViewBind()
... {
string SqlStr = "Select * FROM admin";
DataSet ds = new DataSet();
try
...{
conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "AdminInfo");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
... {
GridView1.EditIndex = e.NewEditIndex;//获取当前行
GridViewBind();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
... {
GridView1.EditIndex = -1;//取消更新
GridViewBind();
}
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
... {
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
... {
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();//获取当前行ID
string admin = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;//强奸转化
string SqlStr = "update admin set admin='" + admin + "'where id=" + id;
try
...{
SqlConnection conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
...{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
... {
//我不想写了...因为剩下的都...
//类似更新操作,改一下SQL语句就可以了
}
string connstr = System.Configuration.ConfigurationManager.AppSettings[ " Connstring " ]; // 在Config中自己搞,不会的看书
protected void Page_Load( object sender, EventArgs e)
... {
if (!IsPostBack)//这句不会,去S吧
...{
GridViewBind();//做了一个绑定数据的方法.
}
}
private void GridViewBind()
... {
string SqlStr = "Select * FROM admin";
DataSet ds = new DataSet();
try
...{
conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "AdminInfo");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
... {
GridView1.EditIndex = e.NewEditIndex;//获取当前行
GridViewBind();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
... {
GridView1.EditIndex = -1;//取消更新
GridViewBind();
}
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
... {
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
... {
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();//获取当前行ID
string admin = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;//强奸转化
string SqlStr = "update admin set admin='" + admin + "'where id=" + id;
try
...{
SqlConnection conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
...{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
... {
//我不想写了...因为剩下的都...
//类似更新操作,改一下SQL语句就可以了
}
好了!本地调试成功了.....