一个dropdownlist给人选择部门,下面用一个DETAILVIEW来显示该部门下的员工信息,注意将detailview 放在
updatepannel中,其triggers属性设定为部门下拉框的selectedIndexchanged事件引发的ASYNCPOSTBACKTRIGGERS触发器
前端:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<table>
<tr>
<td style="width: 443px">
请选择一个部门:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="DepartmentList"
DataTextField="部门" DataValueField="部门" Width="216px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList><asp:SqlDataSource ID="DepartmentList" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
DataSourceMode="DataReader" SelectCommand="SELECT DISTINCT [部门] FROM [章立民工作室]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td style="width: 443px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="员工号码" DataSourceID="LimingStudio" ForeColor="#333333"
GridLines="None" Height="50px" Width="488px" OnItemDeleted="DetailsView1_ItemDeleted"
OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated"
OnPageIndexChanging="DetailsView1_PageIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle Font-Bold="True" BackColor="#D1DDF1" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="员工号码" HeaderText="员工号码:" InsertVisible="False" ReadOnly="True"
SortExpression="员工号码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="身份证号码" HeaderText="身份证号码:" SortExpression="身份证号码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="姓名" HeaderText="姓名:" SortExpression="姓名">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="性别:" SortExpression="性别">
<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" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("性别") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="地址:" SortExpression="地址">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="312px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("地址") %>' Width="312px"></asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("地址") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码:" SortExpression="邮政编码">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="出生日期" DataFormatString="{0:d}"
HeaderText="出生日期:" HtmlEncode="False" SortExpression="出生日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="婚姻状况:" SortExpression="婚姻状况">
<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" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("婚姻状况") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="到职日期" DataFormatString="{0:d}"
HeaderText="到职日期:" HtmlEncode="False" SortExpression="到职日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="起薪" HeaderText="起薪:" SortExpression="起薪">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="目前薪资" HeaderText="目前薪资:" SortExpression="目前薪资">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:BoundField ApplyFormatInEditMode="True" DataField="加薪日期" DataFormatString="{0:d}"
HeaderText="加薪日期:" HtmlEncode="False" SortExpression="加薪日期">
<HeaderStyle HorizontalAlign="Right" Width="130px" />
</asp:BoundField>
<asp:TemplateField HeaderText="部门:" SortExpression="部门">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="DepartmentList"
DataTextField="部门" DataValueField="部门" SelectedValue='<%# Bind("部门") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" DataSourceID="DepartmentList"
DataTextField="部门" DataValueField="部门" SelectedValue='<%# Bind("部门") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Right" Width="130px" />
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("部门") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新" BackColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消" BackColor="White"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert"
Text="插入" BackColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消" BackColor="White"></asp:LinkButton>
</InsertItemTemplate>
<ControlStyle ForeColor="#C00000" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="New"
Text="添加"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('您确定要删除此笔数据纪录吗?');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" VerticalAlign="Top" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings FirstPageImageUrl="~/Images/First.gif" LastPageImageUrl="~/Images/Last.gif"
Mode="NextPreviousFirstLast" NextPageImageUrl="~/Images/Next.gif" PreviousPageImageUrl="~/Images/Previous.gif" />
</asp:DetailsView>
<asp:SqlDataSource ID="LimingStudio" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
DeleteCommand="DELETE FROM [章立民工作室] WHERE [员工号码] = @员工号码" InsertCommand="INSERT INTO [章立民工作室] ([身份证号码], [姓名], [性别], [地址], [邮政编码], [出生日期], [婚姻状况], [到职日期], [起薪], [目前薪资], [加薪日期], [部门]) VALUES (@身份证号码, @姓名, @性别, @地址, @邮政编码, @出生日期, @婚姻状况, @到职日期, @起薪, @目前薪资, @加薪日期, @部门)"
SelectCommand="SELECT * FROM [章立民工作室] WHERE ([部门] = @部门)" UpdateCommand="UPDATE [章立民工作室] SET [身份证号码] = @身份证号码, [姓名] = @姓名, [性别] = @性别, [地址] = @地址, [邮政编码] = @邮政编码, [出生日期] = @出生日期, [婚姻状况] = @婚姻状况, [到职日期] = @到职日期, [起薪] = @起薪, [目前薪资] = @目前薪资, [加薪日期] = @加薪日期, [部门] = @部门 WHERE [员工号码] = @员工号码">
<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="DropDownList1" Name="部门" PropertyName="SelectedValue"
Type="String" />
</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>
<asp:Label ID="ErrorMessageLabel" runat="server" ForeColor="Red" Width="480px"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</form>
后台(注意detailview中的增,删,改触发的事件里,要判断其异常)
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// 在选取了不同的部门之后,就将 DetailsView 控件切换至只读模式,也就是一般的显示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
// 在 DetailsView 控件中切换至其它页面后,
// 就将 DetailsView 控件切换至只读模式,也就是一般的显示模式。
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在编辑数据时发生错误,请确认所输入之数据的格式是否正确。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功编辑数据...";
}
}
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "因为数据表上拥有外部索引键条件约束,因此无法删除。您只能删除没有关联数据记录的数据记录。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功删除资料...";
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception != null || e.AffectedRows <= 0)
{
ErrorMessageLabel.Text = "在添加数据时发生错误,请确认所输入之数据的格式是否正确。";
e.ExceptionHandled = true;
}
else
{
ErrorMessageLabel.Text = "已经成功添加资料...";
}
}