ASP.Net 2.0 中直接GridView加SqlDataSource 实现增删改的文章网上非常多了,这里就不重复了。
GridView + SqlDataSource 分页也很简单,直接点GridView的智能标示,然后启用分页。
加上搜索条件的话其实也不难,主要是要在GridView生成之前先设置好数据源。
否则就会导致点击其他分页又重新绑定了原始数据。
很简单,在这里我是利用GridView的PreRender事件,这个事件是在控件生成之前触发的,在这个事件里设置好SQL语句即可。
假设以Northwind为示例
页面源码
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
CellPadding
=
"
4
"
DataKeyNames
=
"
ProductID
"
DataSourceID
=
"
SqlDataSource1
"
ForeColor
=
"
#333333
"
GridLines
=
"
None
"
AllowPaging
=
"
True
"
onprerender
=
"
GridView1_PreRender
"
>
<
FooterStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
RowStyle BackColor
=
"
#EFF3FB
"
/>
<
Columns
>
<
asp:BoundField DataField
=
"
ProductName
"
HeaderText
=
"
产品名
"
SortExpression
=
"
ProductName
"
/>
<
asp:BoundField DataField
=
"
SupplierID
"
HeaderText
=
"
SupplierID
"
SortExpression
=
"
SupplierID
"
/>
<
asp:BoundField DataField
=
"
CategoryID
"
HeaderText
=
"
CategoryID
"
SortExpression
=
"
CategoryID
"
/>
<
asp:BoundField DataField
=
"
UnitPrice
"
HeaderText
=
"
UnitPrice
"
SortExpression
=
"
UnitPrice
"
/>
<
asp:BoundField DataField
=
"
UnitsInStock
"
HeaderText
=
"
UnitsInStock
"
SortExpression
=
"
UnitsInStock
"
/>
<
asp:BoundField DataField
=
"
UnitsOnOrder
"
HeaderText
=
"
UnitsOnOrder
"
SortExpression
=
"
UnitsOnOrder
"
/>
<
asp:BoundField DataField
=
"
ReorderLevel
"
HeaderText
=
"
ReorderLevel
"
SortExpression
=
"
ReorderLevel
"
/>
<
asp:CheckBoxField DataField
=
"
Discontinued
"
HeaderText
=
"
Discontinued
"
SortExpression
=
"
Discontinued
"
/>
<
asp:CommandField ShowDeleteButton
=
"
True
"
ShowEditButton
=
"
True
"
/>
</
Columns
>
<
PagerStyle BackColor
=
"
#2461BF
"
ForeColor
=
"
White
"
HorizontalAlign
=
"
Center
"
/>
<
SelectedRowStyle BackColor
=
"
#D1DDF1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
#333333
"
/>
<
HeaderStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
EditRowStyle BackColor
=
"
#2461BF
"
/>
<
AlternatingRowStyle BackColor
=
"
White
"
/>
</
asp:GridView
>
<
asp:SqlDataSource ID
=
"
SqlDataSource1
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:NorthwindConnectionString %>
"
DeleteCommand
=
"
DELETE FROM [Products] WHERE [ProductID] = @ProductID
"
InsertCommand
=
"
INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)
"
SelectCommand
=
"
select * from Products
"
UpdateCommand
=
"
UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID
"
>
<
DeleteParameters
>
<
asp:Parameter Name
=
"
ProductID
"
Type
=
"
Int32
"
/>
</
DeleteParameters
>
<
UpdateParameters
>
<
asp:Parameter Name
=
"
ProductName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
SupplierID
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
CategoryID
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
QuantityPerUnit
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
UnitPrice
"
Type
=
"
Decimal
"
/>
<
asp:Parameter Name
=
"
UnitsInStock
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
UnitsOnOrder
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
ReorderLevel
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
Discontinued
"
Type
=
"
Boolean
"
/>
<
asp:Parameter Name
=
"
ProductID
"
Type
=
"
Int32
"
/>
</
UpdateParameters
>
<
InsertParameters
>
<
asp:Parameter Name
=
"
ProductName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
SupplierID
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
CategoryID
"
Type
=
"
Int32
"
/>
<
asp:Parameter Name
=
"
QuantityPerUnit
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
UnitPrice
"
Type
=
"
Decimal
"
/>
<
asp:Parameter Name
=
"
UnitsInStock
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
UnitsOnOrder
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
ReorderLevel
"
Type
=
"
Int16
"
/>
<
asp:Parameter Name
=
"
Discontinued
"
Type
=
"
Boolean
"
/>
</
InsertParameters
>
</
asp:SqlDataSource
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
onclick
=
"
Button1_Click
"
Text
=
"
Button
"
/>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
></
asp:TextBox
>
CS代码
protected
void
Button1_Click(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
ViewState["search"] = this.TextBox1.Text;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
protected
void
GridView1_PreRender(
object
sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
this.SqlDataSource1.SelectCommand = "select * from Products where ProductName like '%" + ViewState["search"] + "%' ";
}
.Net2.0 如果想实现更复杂的操作,还是建议强类型化。
如果是.Net 3.0/3.5 的话 使用Linq 会更加方便,更适合做小项目,不必要考虑太多性能因素,控制好性能就足够了。