以前做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>
浙公网安备 33010602011771号