今天介绍.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.后台代码片段:
InBlock.gif public DataTable BraTB
InBlock.gif    {
InBlock.gif        get
InBlock.gif        {
InBlock.gif             return ( new DepSystem()).GetBraTB();
InBlock.gif        }
InBlock.gif    }
InBlock.gif     protected void Page_Load( object sender, EventArgs e)
InBlock.gif    {
InBlock.gifDropDownList2.DataBind();
InBlock.gif        DropDownList2.SelectedIndex = 0;
InBlock.gif        DropDownList2_SelectedIndexChanged( null, null);
InBlock.gif}
InBlock.gif protected void NewStaffView_DataBound( object sender, EventArgs e)
InBlock.gif    {
InBlock.gif        labNew.Text = NewStaffView.Rows.Count.ToString(); //统计
InBlock.gif    }
InBlock.gif     protected void DropDownList2_SelectedIndexChanged( object sender, EventArgs e)
InBlock.gif    {
InBlock.gif         this.DataSourceNew.SelectParameters[0].DefaultValue = DropDownList2.SelectedValue; //这里就是传到方法中的参数
InBlock.gif         this.DataSourceNew.Select();
InBlock.gif    }
说明:这里后台代码很简单,但是关联性很强,要和页面中控件的属性相联系起来。
 
3.mrBaseInf类中的GetRecentEmpData2的代码:
InBlock.gif private static int iMonth = 1;
InBlock.gif         public DataSet GetRecentEmpData2( int branchid)
InBlock.gif        {
InBlock.gif            SqlCommand command = new SqlCommand();
InBlock.gif
InBlock.gif            command.CommandText = "GetRecentEmp2";
InBlock.gif            command.CommandType = CommandType.StoredProcedure;
InBlock.gif            command.Connection = con;
InBlock.gif            SqlParameter param = new SqlParameter( "@iMonth", SqlDbType.Int);
InBlock.gif            param.Value = iMonth;
InBlock.gif            command.Parameters.Add(param);
InBlock.gif            param = new SqlParameter(paramChg( "branchid"), SqlDbType.Int);
InBlock.gif            param.Value = branchid;
InBlock.gif            command.Parameters.Add(param);
InBlock.gif
InBlock.gif            commandAdp.SelectCommand = command;
InBlock.gif
InBlock.gif            DataSet data = new DataSet();
InBlock.gif             try
InBlock.gif            {
InBlock.gif                commandAdp.Fill(data);
InBlock.gif            }
InBlock.gif             catch (Exception ex)
InBlock.gif            {
InBlock.gif                 throw new ApplicationException(ex.ToString());
InBlock.gif            }
InBlock.gif            con.Close();
InBlock.gif             return data;
InBlock.gif
InBlock.gif        }
说明:方法中有一个参数,我用的数据库方法GetRecentEmp2中要传递进去两个,外面定义一个静态变量就OK了,那么最后表中显示的数据就会随这dropdownlist的变化而变化了。
 
总结:dropdownlist--->GridView--->ObjectDataSource--->数据访问类--->数据库。这是一个执行的过程,代码中,dropdown下拉选项会直接传递到ObjectDataSource,GV一直是显示控制的一个东西,设计的时候也可以把它单独拿出来设计。再有注意的就是ObjectDataSource中的参数设置,可以直接访问类中的方法,方法中的参数也可以自有定义。那么这种从页面直接传递到数据访问层的方法也会经常用到,后台中搭建起他们传递数据的代码。