沉默的“羔羊”----.Net中的ObjectDataSource
2008-01-03 12:05:22
今天介绍.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: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"> </asp:BoundField>
</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> 说明:上面是显示数据的GridView控件,通过DataSourceID来和下面的ObjectDataSource关联,GV只负责显示,真正的数据来源是ObjectDataSource。ObjectDataSource中的TypeName后面的就是引用的整个工程中的一个类,SelectMethod就是调用的该类中的方法。注意下面红色部分的代码,就是这个方法中的参数,方法里面如果有更多,那么就添加一行<asp:Parameter ,这里的参数是由dropdownlist传下来的,因为我要根据下拉的选项来绑定gridview中的数据,大家明白了吧,那么label呢,就是来统计表中的数量。
2.后台代码片段:
public DataTable BraTB { get { return (new DepSystem()).GetBraTB(); } } protected void Page_Load(object sender, EventArgs e) { DropDownList2.DataBind(); DropDownList2.SelectedIndex = 0; DropDownList2_SelectedIndexChanged(null, null); } protected void NewStaffView_DataBound(object sender, EventArgs e) { labNew.Text = NewStaffView.Rows.Count.ToString(); //统计 } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { this.DataSourceNew.SelectParameters[0].DefaultValue = DropDownList2.SelectedValue; //这里就是传到方法中的参数 this.DataSourceNew.Select(); }说明:这里后台代码很简单,但是关联性很强,要和页面中控件的属性相联系起来。
3.mrBaseInf类中的GetRecentEmpData2的代码:
private static int iMonth = 1; public DataSet GetRecentEmpData2(int branchid) { SqlCommand command = new SqlCommand(); command.CommandText = "GetRecentEmp2"; command.CommandType = CommandType.StoredProcedure; command.Connection = con; SqlParameter param = new SqlParameter("@iMonth", SqlDbType.Int); param.Value = iMonth; command.Parameters.Add(param); param = new SqlParameter(paramChg("branchid"), SqlDbType.Int); param.Value = branchid; command.Parameters.Add(param); commandAdp.SelectCommand = command; DataSet data = new DataSet(); try { commandAdp.Fill(data); } catch (Exception ex) { throw new ApplicationException(ex.ToString()); } con.Close(); return data; }说明:方法中有一个参数,我用的数据库方法GetRecentEmp2中要传递进去两个,外面定义一个静态变量就OK了,那么最后表中显示的数据就会随这dropdownlist的变化而变化了。
总结:dropdownlist--->GridView--->ObjectDataSource--->数据访问类--->数据库。这是一个执行的过程,代码中,dropdown下拉选项会直接传递到ObjectDataSource,GV一直是显示控制的一个东西,设计的时候也可以把它单独拿出来设计。再有注意的就是ObjectDataSource中的参数设置,可以直接访问类中的方法,方法中的参数也可以自有定义。那么这种从页面直接传递到数据访问层的方法也会经常用到,后台中搭建起他们传递数据的代码。 |



public DataTable BraTB
L.net
博客统计信息
热门文章
最新评论
友情链接
