在detailview中,還可以編輯,刪除具體的信息,下面全部摘錄之
前臺:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table border="0" width="850" id="Table2">
<tr>
<td colspan="2">
請選擇一個部門:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
DataTextField="部門" DataValueField="部門" Width="168px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
DataSourceMode="DataReader" SelectCommand="SELECT DISTINCT [部門] FROM [章立民工作室]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td valign="top" style="width: 239px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"><ContentTemplate>
<asp:GridView id="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="384px" DataSourceID="SqlDataSource2" OnSorted="GridView1_Sorted" OnRowDeleted="GridView1_RowDeleted" OnPageIndexChanged="GridView1_PageIndexChanged" ShowFooter="True" SelectedIndex="0" GridLines="None" ForeColor="#333333" Font-Size="Smaller" CellPadding="4" DataKeyNames="員工號碼" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
Text="選取"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('您確定要刪除此筆數(shù)據(jù)紀錄嗎?');" Text="刪除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="員工號碼" HeaderText="員工號碼" InsertVisible="False" ReadOnly="True"
SortExpression="員工號碼" />
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="部門" HeaderText="部門" SortExpression="部門" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView> <asp:SqlDataSource id="SqlDataSource2" runat="server" SelectCommand="SELECT 員工號碼, 姓名, 部門 FROM 章立民工作室 WHERE 部門 = @部門" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>" DeleteCommand="DELETE FROM 章立民工作室 WHERE 員工號碼 = @員工號碼">
<DeleteParameters>
<asp:Parameter Name="員工號碼" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="部門" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource> <asp:Label id="ErrorMessageLabel" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</td>
<td valign="top" style="width: 280px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server"><ContentTemplate>
<asp:DetailsView id="DetailsView1" runat="server" Width="472px" DataSourceID="SqlDataSource3" OnDataBound="DetailsView1_DataBound" GridLines="None" ForeColor="#333333" Font-Size="Smaller" CellPadding="4" DataKeyNames="員工號碼" OnItemUpdated="DetailsView1_ItemUpdated" OnItemInserted="DetailsView1_ItemInserted" Height="50px" AutoGenerateRows="False">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True"></FooterStyle>
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True"></CommandRowStyle>
<EditRowStyle BackColor="#2461BF"></EditRowStyle>
<RowStyle BackColor="#EFF3FB"></RowStyle>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"></PagerStyle>
<Fields>
<asp:BoundField ReadOnly="True" DataField="員工號碼" InsertVisible="False" SortExpression="員工號碼" HeaderText="員工號碼:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="身份證號碼" SortExpression="身份證號碼" HeaderText="身份證號碼:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="姓名" SortExpression="姓名" HeaderText="姓名:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField SortExpression="性別" HeaderText="性別:"><EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("性別") %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" SelectedValue='<%# Bind("性別") %>'>
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# Bind("性別") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="地址" HeaderText="地址:"><EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="328px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="328px"></asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("地址") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="郵政編碼" SortExpression="郵政編碼" HeaderText="郵政編碼:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HtmlEncode="False" DataFormatString="{0:d}" DataField="出生日期" SortExpression="出生日期" HeaderText="出生日期:" ApplyFormatInEditMode="True">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField SortExpression="婚姻狀況" HeaderText="婚姻狀況:"><EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" SelectedValue='<%# Bind("婚姻狀況") %>'>
<asp:ListItem>已婚</asp:ListItem>
<asp:ListItem>未婚</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" SelectedValue='<%# Bind("婚姻狀況") %>'>
<asp:ListItem>已婚</asp:ListItem>
<asp:ListItem>未婚</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("婚姻狀況") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HtmlEncode="False" DataFormatString="{0:d}" DataField="到職日期" SortExpression="到職日期" HeaderText="到職日期:" ApplyFormatInEditMode="True">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="起薪" SortExpression="起薪" HeaderText="起薪:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="目前薪資" SortExpression="目前薪資" HeaderText="目前薪資:">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:BoundField HtmlEncode="False" DataFormatString="{0:d}" DataField="加薪日期" SortExpression="加薪日期" HeaderText="加薪日期:" ApplyFormatInEditMode="True">
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField SortExpression="部門" HeaderText="部門:"><EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="DepartmentList"
DataTextField="部門" DataValueField="部門" SelectedValue='<%# Bind("部門") %>' Width="160px">
</asp:DropDownList><asp:SqlDataSource ID="DepartmentList" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT DISTINCT [部門] FROM [章立民工作室]"></asp:SqlDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtDepartment" runat="server" Text='<%# Bind("部門") %>'></asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("部門") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" ShowEditButton="True">
<ItemStyle ForeColor="#C00000" Font-Bold="True"></ItemStyle>
</asp:CommandField>
</Fields>
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True"></FieldHeaderStyle>
<HeaderStyle BackColor="#507CD1" ForeColor="White" HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
<HeaderTemplate>
明細資料
</HeaderTemplate>
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
</asp:DetailsView> <asp:SqlDataSource id="SqlDataSource3" runat="server" SelectCommand="SELECT * FROM [章立民工作室] WHERE ([員工號碼] = @員工號碼)" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>" DeleteCommand="DELETE FROM [章立民工作室] WHERE [員工號碼] = @員工號碼" UpdateCommand="UPDATE [章立民工作室] SET [身份證號碼] = @身份證號碼, [姓名] = @姓名, [性別] = @性別, [地址] = @地址, [郵政編碼] = @郵政編碼, [出生日期] = @出生日期, [婚姻狀況] = @婚姻狀況, [到職日期] = @到職日期, [起薪] = @起薪, [目前薪資] = @目前薪資, [加薪日期] = @加薪日期, [部門] = @部門 WHERE [員工號碼] = @員工號碼" InsertCommand="INSERT INTO [章立民工作室] ([身份證號碼], [姓名], [性別], [地址], [郵政編碼], [出生日期], [婚姻狀況], [到職日期], [起薪], [目前薪資], [加薪日期], [部門]) VALUES (@身份證號碼, @姓名, @性別, @地址, @郵政編碼, @出生日期, @婚姻狀況, @到職日期, @起薪, @目前薪資, @加薪日期, @部門)">
<DeleteParameters>
<asp:Parameter Name="員工號碼" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="身份證號碼" Type="String" />
<asp:Parameter Name="姓名" Type="String" />
<asp:Parameter Name="性別" Type="String" />
<asp:Parameter Name="地址" Type="String" />
<asp:Parameter Name="郵政編碼" Type="String" />
<asp:Parameter Name="出生日期" Type="DateTime" />
<asp:Parameter Name="婚姻狀況" Type="String" />
<asp:Parameter Name="到職日期" Type="DateTime" />
<asp:Parameter Name="起薪" Type="Decimal" />
<asp:Parameter Name="目前薪資" Type="Decimal" />
<asp:Parameter Name="加薪日期" Type="DateTime" />
<asp:Parameter Name="部門" Type="String" />
<asp:Parameter Name="員工號碼" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="員工號碼" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="身份證號碼" Type="String" />
<asp:Parameter Name="姓名" Type="String" />
<asp:Parameter Name="性別" Type="String" />
<asp:Parameter Name="地址" Type="String" />
<asp:Parameter Name="郵政編碼" Type="String" />
<asp:Parameter Name="出生日期" Type="DateTime" />
<asp:Parameter Name="婚姻狀況" Type="String" />
<asp:Parameter Name="到職日期" Type="DateTime" />
<asp:Parameter Name="起薪" Type="Decimal" />
<asp:Parameter Name="目前薪資" Type="Decimal" />
<asp:Parameter Name="加薪日期" Type="DateTime" />
<asp:Parameter Name="部門" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</form>
后臺
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// 在選取了不同的部門之后,就將 DetailsView 控件切換至只讀模式,也就是一般的顯示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
this.GridView1.SelectedIndex = 0;
// 在 GridView 控件中按下數(shù)字按鈕來切換至其它頁面后,
// 就將 DetailsView 控件切換至只讀模式,也就是一般的顯示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// 在 GridView 控件中按下"選取"按鈕來選取某一列數(shù)據(jù)之后,
// 就將 DetailsView 控件切換至只讀模式,也就是一般的顯示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void GridView1_Sorted(object sender, EventArgs e)
{
// 在 GridView 控件中按下數(shù)據(jù)行的標題超級鏈接來排序之后,
// 就將 DetailsView 控件切換至只讀模式,也就是一般的顯示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
// 在 DetailsView 控件綁定至數(shù)據(jù)來源之后,檢查 DetailsView 控件是否處于
// 添加模式(也就是可以讓用戶將數(shù)據(jù)添加至數(shù)據(jù)來源)。如果是的話,將用來輸
// 入"員工號碼"字段的 TextBox 控件設定成停用狀態(tài)(因為"員工號碼"字段是
// 一個自動編號字段,不需要用戶輸入),另外,還將"部門"字段之 TextBox 控
// 制項的值設定成當前所選取的部門,然后再將其設定成停用狀態(tài)。
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
TextBox IdTextBox = (TextBox)(DetailsView1.Rows[0].Cells[1].Controls[0]);
IdTextBox.Enabled = false;
TextBox DepartmentTextBox = (TextBox)(DetailsView1.Rows[12].FindControl("txtDepartment"));
DepartmentTextBox.Text = DropDownList1.SelectedValue;
DepartmentTextBox.Enabled = false;
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在添加數(shù)據(jù)時發(fā)生錯誤,請確認所輸入之數(shù)據(jù)的格式是否正確。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已經(jīng)成功添加資料...";
}
GridView1.DataBind();
this.UpdatePanel1.Update();
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在編輯數(shù)據(jù)時發(fā)生錯誤,請確認所輸入之數(shù)據(jù)的格式是否正確。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已經(jīng)成功編輯數(shù)據(jù)...";
}
GridView1.DataBind();
this.UpdatePanel1.Update();
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "因為數(shù)據(jù)表上擁有外部索引鍵條件約束,因此無法刪除。您只能刪除沒有關(guān)聯(lián)數(shù)據(jù)記錄的數(shù)據(jù)記錄。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已經(jīng)成功刪除資料...";
}
GridView1.DataBind();
this.UpdatePanel1.Update();
}