今天介绍.net中ObjectDataSource这只沉默的羔羊,其实这里,我运用它只是过度一下,用它做一下和数据读取层的挂接,看这只羊也可以在沉默中爆发。:)
1.页面代码:(代码片段)
<
asp:Label
ID
="labNew"
Runat
="server"
>
</asp:Label>
<
asp:DropDownList
ID
="DropDownList2"
runat
="server" DataSource=<%# BraTB %
> Width="100px" DataTextField="SimpleCode" DataValueField="BranchId" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</asp:DropDownList>
<
asp:GridView
ID
="NewStaffView"
Runat
="server"
Width
="100%"
AutoGenerateColumns
="False"
DataSourceID
="DataSourceNew"
OnDataBound
="NewStaffView_DataBound"
CellPadding
="0"
>
< HeaderStyle CssClass ="Table_header" />
< Columns >
< asp:BoundField HeaderText ="部门" DataField ="depName" >
< ItemStyle Width ="30%" > </ItemStyle>
</asp:BoundField>
< asp:BoundField HeaderText ="入职时间" DataField ="Enter" >
< HeaderStyle CssClass ="Table_header" />
< Columns >
< asp:BoundField HeaderText ="部门" DataField ="depName" >
< ItemStyle Width ="30%" > </ItemStyle>
</asp:BoundField>
< asp:BoundField HeaderText ="入职时间" DataField ="Enter" >
</asp:BoundField>
</Columns>
</asp:GridView>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="DataSourceNew" Runat="server" TypeName="Zznode.OA.DA.MrBaseInf"
SelectMethod="GetRecentEmpData2">
<SelectParameters>
<asp:Parameter Type="Int32" DefaultValue="0" Name="branchid"></asp:Parameter>
</SelectParameters>
</asp:ObjectDataSource>
SelectMethod="GetRecentEmpData2">
<SelectParameters>
<asp:Parameter Type="Int32" DefaultValue="0" Name="branchid"></asp:Parameter>
</SelectParameters>
</asp:ObjectDataSource>
说明:上面是显示数据的GridView控件,通过DataSourceID来和下面的ObjectDataSource关联,GV只负责显示,真正的数据来源是ObjectDataSource。ObjectDataSource中的TypeName后面的就是引用的整个工程中的一个类,SelectMethod就是调用的该类中的方法。注意下面红色部分的代码,就是这个方法中的参数,方法里面如果有更多,那么就添加一行<asp:Parameter ,这里的参数是由dropdownlist传下来的,因为我要根据下拉的选项来绑定gridview中的数据,大家明白了吧,那么label呢,就是来统计表中的数量。
2.后台代码片段:
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
说明:这里后台代码很简单,但是关联性很强,要和页面中控件的属性相联系起来。
3.mrBaseInf类中的GetRecentEmpData2的代码:
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
![InBlock.gif](https://s1.51cto.com/images/editer/InBlock.gif)
说明:方法中有一个参数,我用的数据库方法GetRecentEmp2中要传递进去两个,外面定义一个静态变量就OK了,那么最后表中显示的数据就会随这dropdownlist的变化而变化了。
总结:dropdownlist--->GridView--->ObjectDataSource--->数据访问类--->数据库。这是一个执行的过程,代码中,dropdown下拉选项会直接传递到ObjectDataSource,GV一直是显示控制的一个东西,设计的时候也可以把它单独拿出来设计。再有注意的就是ObjectDataSource中的参数设置,可以直接访问类中的方法,方法中的参数也可以自有定义。那么这种从页面直接传递到数据访问层的方法也会经常用到,后台中搭建起他们传递数据的代码。
转载于:https://blog.51cto.com/mixangel/57803