zoukankan      html  css  js  c++  java
  • 把aspx绑定的数据搬至aspx.cs页面中去

    或许你有做过,接过美工做好生成的Html的前台网页,开始写程序,你会知道有些简单的数据源绑定已经被美工做好了。如下:

    View Code
    <asp:DropDownList ID="DropDownList1" runat="server">
                
    <asp:ListItem Text="求购"></asp:ListItem>
                
    <asp:ListItem Text="出租"></asp:ListItem>
                
    <asp:ListItem Text="家政"></asp:ListItem>
                
    <asp:ListItem Text="培训"></asp:ListItem>
            
    </asp:DropDownList>

    你接过手之后,也许不会置之不理,而是把这些数据源搬至.aspx.cs页面中去。你会创建一个ArrayList来存储这些数据源,然后再绑定到这个DropDownList,重构如下:

    <asp:DropDownList ID="DropDownList1" runat="server">           
            
    </asp:DropDownList>

    .aspx.cs:

    View Code
     protected void Page_Load(object sender, EventArgs e)
        {
            
    if (!IsPostBack)
            {
                
    this.DropDownList1.DataSource = GetServices;
                
    this.DropDownList1.DataBind();
            }
        }

        
    public ArrayList GetServices
        {
            
    get
            {
                ArrayList arrayList 
    = new ArrayList ();
                arrayList.Add(
    "求购");
                arrayList.Add(
    "出售");
                arrayList.Add(
    "家教");
                arrayList.Add(
    "培训");
                
    return arrayList;
            }       
        }

    以后想加减服务类别,直接修改GetServices属性即可,这样做,只是一个较简单的做法,当然你还可以把这些存取入数据库中,这样就复杂多了。

  • 相关阅读:
    09-异常处理-成绩判断异常
    继承与多态———动手动脑
    课下作业04-2String的使用方法
    课下作业04-1字符串加密
    课下作业03-2动手动脑及验证
    课下作业03-1请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?
    课下作业02-动手动脑
    Myschool试题
    使用ADO.NET
    模糊查询和聚合函数
  • 原文地址:https://www.cnblogs.com/insus/p/2056599.html
Copyright © 2011-2022 走看看