做Asp.Net 1.X是经常会碰到处理列表控件的提示情况,现在开始学习Asp.Net2.0了, 用GridView完成相似的事情,更新和删除显示提示信息,确认后使用SqlDataSource 完成数据库操作的
<%@ Page Language="C#" %>
<script runat="server">
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "edit":
GridView1.Columns[0].Visible = false;
GridView1.Columns[1].Visible = true;
break;
case "update":
case "delete":
case "cancel":
GridView1.Columns[0].Visible = true;
GridView1.Columns[1].Visible = false;
break;
default:
// Do nothing.
break;
}
}
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
TextBox firstNameTextBox = (TextBox)row.Cells[1].FindControl("FirstNameTextBox");
String firstName = "";
if (firstNameTextBox != null)
{
firstName = firstNameTextBox.Text;
}
TextBox lastNameTextBox = (TextBox)row.Cells[2].FindControl("LastNameTextBox");
String lastName = "";
if (lastNameTextBox != null)
{
lastName = lastNameTextBox.Text;
}
Parameter lastNameParameter = new Parameter("au_lname", TypeCode.String, lastName);
Parameter firstNameParameter = new Parameter("au_fname", TypeCode.String, firstName);
SqlDataSource1.UpdateParameters.Clear();
SqlDataSource1.UpdateParameters.Add(lastNameParameter);
SqlDataSource1.UpdateParameters.Add(firstNameParameter);
}
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlDataSource1.DeleteParameters.Clear();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView更新和删除显示提示信息- 王智-2006</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="au_id" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:LinkButton ID="Update" runat="server" CommandName="Update" OnClientClick="return confirm('确定要更新?');">更新</asp:LinkButton>
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete" OnClientClick="return confirm('确定要删除');">删除</asp:LinkButton>
<asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel">取消</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:templatefield headertext="Last Name">
<itemtemplate>
<%#Eval("au_lname") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="LastNameTextBox" text='<%#Eval("au_lname") %>'
width="175" runat="server"/>
<br/>
<asp:requiredfieldvalidator id="LastNameRequiredValidator" controltovalidate="LastNameTextBox"
errormessage="Please enter a last name." validationgroup="NameGroup" runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="First Name">
<itemtemplate>
<%#Eval("au_fname") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="FirstNameTextBox" text='<%#Eval("au_fname") %>'
width="175" runat="server"/>
<br/>
<asp:requiredfieldvalidator id="FirstNameRequiredValidator" controltovalidate="FirstNameTextBox"
errormessage="Please enter a first name." validationgroup="NameGroup" runat="server"/>
</edititemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Pubs2000 %>"
ProviderName="<%$ ConnectionStrings:Pubs2000.ProviderName %>" SelectCommand="SELECT au_id, au_lname, au_fname FROM authors"
UpdateCommand="UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE (au_id = @au_id)"
DeleteCommand="DELETE FROM authors WHERE (au_id = @au_id)">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
DataBase db = new DataBase();
string strsql = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
TextBox1.Focus();
}
public void databind()
{
strsql = "select * from a_yao";
DataSet ds = new DataSet();
db.GetDataSet(strsql, ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
databind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
databind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
string no = GridView1.DataKeys[index][0].ToString();
string drng_name = ((TextBox)GridView1.Rows[index].FindControl("tb_drng_name")).Text;
string long_name = ((TextBox)GridView1.Rows[index].FindControl("tb_long_name")).Text;
string unit = ((TextBox)GridView1.Rows[index].FindControl("tb_unit")).Text;
if (unit == "")
{
Response.Write("<script>alert('unit is null!');</script>");
return;
}
strsql = "UPDATE a_yao SET drng_name = '" + drng_name + "',long_name='" + long_name + "',unit='" + unit + "' where no='" + no + "'";
db.RunExecuteNonQuery(strsql);
GridView1.EditIndex = -1;
databind();
return;
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = e.RowIndex;
string no = GridView1.DataKeys[index][0].ToString();
strsql = "delete from a_yao where no = '" + no + "'";
db.RunExecuteNonQuery(strsql);
Response.Write("<script>alert('delete done!');</script>");
databind();
}
protected void s_button_Click(object sender, EventArgs e)
{
string drng_name = TextBox1.Text;
if (drng_name == "")
{
Response.Write("<script>alert('没有输入查询条件,默认显示所有记录!');</script>");
}
else
{
strsql = "select * from a_yao where drng_name like '%" + drng_name + "%'";
db.RunExecuteNonQuery(strsql);
DataSet ds = new DataSet();
db.GetDataSet(strsql, ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
if (GridView1.Rows.Count == 0)
{
Response.Write("<script>alert('对不起,没有查询到符合的记录!')</script>");
databind();
}
return;
}
}
}