<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:ScriptManager ID
=
"
ScriptManager1
"
runat
=
"
server
"
>
</
asp:ScriptManager
>
&
nbsp;
&
nbsp;
&
nbsp;
&
nbsp;
<
asp:Panel ID
=
"
Panel1
"
runat
=
"
server
"
Height
=
"
50px
"
Width
=
"
125px
"
>
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
DataSourceID
=
"
SqlDataSource1
"
OnDataBinding
=
"
GridView1_DataBinding
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
DataKeyNames
=
"
DepId
"
OnRowUpdated
=
"
GridView1_RowUpdated
"
AllowSorting
=
"
True
"
OnRowCancelingEdit
=
"
GridView1_RowCancelingEdit
"
OnSelectedIndexChanged
=
"
GridView1_SelectedIndexChanged
"
AutoGenerateColumns
=
"
False
"
>
<
Columns
>
<
asp:CommandField ShowSelectButton
=
"
True
"
/>
<
asp:BoundField DataField
=
"
DepId
"
HeaderText
=
"
DepId
"
InsertVisible
=
"
False
"
ReadOnly
=
"
True
"
SortExpression
=
"
DepId
"
/>
<
asp:BoundField DataField
=
"
DepName
"
HeaderText
=
"
DepName
"
SortExpression
=
"
DepName
"
/>
<
asp:BoundField DataField
=
"
DepTel
"
HeaderText
=
"
DepTel
"
SortExpression
=
"
DepTel
"
/>
<
asp:BoundField DataField
=
"
DepMan
"
HeaderText
=
"
DepMan
"
SortExpression
=
"
DepMan
"
/>
<
asp:BoundField DataField
=
"
DepManTel
"
HeaderText
=
"
DepManTel
"
SortExpression
=
"
DepManTel
"
/>
<
asp:BoundField DataField
=
"
DepTypeId
"
HeaderText
=
"
DepTypeId
"
SortExpression
=
"
DepTypeId
"
/>
</
Columns
>
</
asp:GridView
>
<
asp:SqlDataSource ID
=
"
SqlDataSource1
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:graduateConnectionString %>
"
DeleteCommand
=
"
DELETE FROM [TB_Depart] WHERE [DepId] = @DepId
"
InsertCommand
=
"
INSERT INTO [TB_Depart] ([DepName], [BusiId], [fid], [DepTel], [DepMan], [DepManTel], [DepTypeId]) VALUES (@DepName, @BusiId, @fid, @DepTel, @DepMan, @DepManTel, @DepTypeId)
"
SelectCommand
=
"
SELECT * FROM [TB_Depart]
"
UpdateCommand
=
"
UPDATE [TB_Depart] SET [DepName] = @DepName, [BusiId] = @BusiId, [fid] = @fid, [DepTel] = @DepTel, [DepMan] = @DepMan, [DepManTel] = @DepManTel, [DepTypeId] = @DepTypeId WHERE [DepId] = @DepId
"
OnSelecting
=
"
SqlDataSource1_Selecting
"
>
<
DeleteParameters
>
<
asp:Parameter Name
=
"
DepId
"
Type
=
"
Int32
"
/>
</
DeleteParameters
>
<
UpdateParameters
>
<
asp:Parameter Name
=
"
DepName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
BusiId
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
fid
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepMan
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepManTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepTypeId
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepId
"
Type
=
"
Int32
"
/>
</
UpdateParameters
>
<
InsertParameters
>
<
asp:Parameter Name
=
"
DepName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
BusiId
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
fid
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepMan
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepManTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepTypeId
"
Type
=
"
Int32
"
/>
</
InsertParameters
>
</
asp:SqlDataSource
>
<
br
/>
<
asp:DetailsView ID
=
"
DetailsView1
"
runat
=
"
server
"
AutoGenerateRows
=
"
False
"
DataKeyNames
=
"
DepId
"
DataSourceID
=
"
SqlDataSource3
"
Height
=
"
50px
"
OnDataBound
=
"
DetailsView1_DataBound
"
OnItemUpdating
=
"
DetailsView1_ItemUpdating
"
Width
=
"
125px
"
>
<
Fields
>
<
asp:TemplateField HeaderText
=
"
DepId
"
InsertVisible
=
"
False
"
SortExpression
=
"
DepId
"
>
<
EditItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# Eval("DepId") %>
'
></
asp:Label
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label2
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepId") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField DataField
=
"
DepName
"
HeaderText
=
"
DepName
"
SortExpression
=
"
DepName
"
/>
<
asp:TemplateField HeaderText
=
"
DepTel
"
SortExpression
=
"
DepTel
"
>
<
EditItemTemplate
>
<
asp:TextBox ID
=
"
TextBox2
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTel") %>
'
></
asp:TextBox
>
</
EditItemTemplate
>
<
InsertItemTemplate
>
<
asp:TextBox ID
=
"
TextBox2
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTel") %>
'
></
asp:TextBox
>
</
InsertItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label3
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTel") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField DataField
=
"
DepMan
"
HeaderText
=
"
DepMan
"
SortExpression
=
"
DepMan
"
/>
<
asp:BoundField DataField
=
"
DepManTel
"
HeaderText
=
"
DepManTel
"
SortExpression
=
"
DepManTel
"
/>
<
asp:TemplateField HeaderText
=
"
DepTypeId
"
SortExpression
=
"
DepTypeId
"
>
<
EditItemTemplate
>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTypeId") %>
'
></
asp:TextBox
><
br
/>
<
asp:DropDownList ID
=
"
DropDownList2
"
runat
=
"
server
"
DataSourceID
=
"
SqlDataSource2
"
DataTextField
=
"
DepTypeId
"
DataValueField
=
"
DepTypeId
"
OnDataBound
=
"
DropDownList2_DataBound
"
SelectedValue
=
'
<%# Bind("DepTypeId") %>
'
>
</
asp:DropDownList
>
<
asp:HiddenField ID
=
"
HiddenField2
"
runat
=
"
server
"
Value
=
'
<%# Eval("DepTypeId") %>
'
/>
</
EditItemTemplate
>
<
InsertItemTemplate
>
<
asp:DropDownList ID
=
"
DropDownList3
"
runat
=
"
server
"
DataSourceID
=
"
SqlDataSource2
"
DataTextField
=
"
DepTypeId
"
DataValueField
=
"
DepTypeId
"
>
</
asp:DropDownList
>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTypeId") %>
'
></
asp:TextBox
>
<
asp:HiddenField ID
=
"
HiddenField3
"
runat
=
"
server
"
Value
=
'
<%# Eval("DepTypeId") %>
'
/>
</
InsertItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# Bind("DepTypeId") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:CommandField ShowEditButton
=
"
True
"
/>
</
Fields
>
</
asp:DetailsView
>
<
asp:SqlDataSource ID
=
"
SqlDataSource2
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:graduateConnectionString %>
"
SelectCommand
=
"
SELECT * FROM [DepType]
"
></
asp:SqlDataSource
>
<
asp:SqlDataSource ID
=
"
SqlDataSource3
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:graduateConnectionString %>
"
DeleteCommand
=
"
DELETE FROM [TB_Depart] WHERE [DepId] = @DepId
"
InsertCommand
=
"
INSERT INTO [TB_Depart] ([DepName], [BusiId], [fid], [DepTel], [DepMan], [DepManTel], [DepTypeId]) VALUES (@DepName, @BusiId, @fid, @DepTel, @DepMan, @DepManTel, @DepTypeId)
"
SelectCommand
=
"
SELECT * FROM [TB_Depart] WHERE ([DepId] = @DepId)
"
UpdateCommand
=
"
UPDATE [TB_Depart] SET [DepName] = @DepName, [BusiId] = @BusiId, [fid] = @fid, [DepTel] = @DepTel, [DepMan] = @DepMan, [DepManTel] = @DepManTel, [DepTypeId] = @DepTypeId WHERE [DepId] = @DepId
"
>
<
SelectParameters
>
<
asp:ControlParameter ControlID
=
"
GridView1
"
Name
=
"
DepId
"
PropertyName
=
"
SelectedValue
"
Type
=
"
Int32
"
/>
</
SelectParameters
>
<
DeleteParameters
>
<
asp:Parameter Name
=
"
DepId
"
Type
=
"
Int32
"
/>
</
DeleteParameters
>
<
UpdateParameters
>
<
asp:Parameter Name
=
"
DepName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
BusiId
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
fid
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepMan
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepManTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepTypeId
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepId
"
Type
=
"
Int32
"
/>
</
UpdateParameters
>
<
InsertParameters
>
<
asp:Parameter Name
=
"
DepName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
BusiId
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
fid
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
DepTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepMan
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepManTel
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
DepTypeId
"
Type
=
"
Int32
"
/>
</
InsertParameters
>
</
asp:SqlDataSource
>
</
asp:Panel
>
<
br
/>
</
form
>
</
body
>
基本思想就是利用视图进行可视化编辑、删除,但是dropdownlist的下拉菜单需要后台代码,如下
protected
void
DetailsView1_DataBound(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (((DropDownList)DetailsView1.FindControl("DropDownList2")) != null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DropDownList drp = ((DropDownList)DetailsView1.FindControl("DropDownList2"));
drp.SelectedValue = ((HiddenField)DetailsView1.FindControl("HiddenField2")).Value.ToString();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
搞定,有什么意见请发我email:ivan3619@163.com