《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定...

aspx

<script type="text/javascript">
     function CheckAll(Obj) {
         var AllObj = document.all;
         if (Obj.checked)//全选
         {
             for (var i = 0; i < AllObj.length; i++) {
                 if (AllObj[i].type == "checkbox") {
                     AllObj[i].checked = true;
                 }
             }
         }
         else//反选
         {
             for (var i = 0; i < AllObj.length; i++) {
                 if (AllObj[i].type == "checkbox") {
                     AllObj[i].checked = false;
                 }
             }
         }
     }
     
     </script>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Width="465px" 
            onitemcommand="DataList1_ItemCommand" DataKeyField="id"> 
            <HeaderTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" οnclick="return CheckAll(this)" />
                <asp:Label ID="Label3"  Width="50px" runat="server" Text="ID"></asp:Label>
                <asp:Label ID="Label4" Width="50px" runat="server" Text="姓名"></asp:Label>
                 <asp:Label ID="Label5" Width="50px" runat="server" Text="性别"></asp:Label>
                 <asp:Label ID="Label7" Width="100px" runat="server" Text="电话"></asp:Label>
                <asp:Label ID="Label6" Width="50px" runat="server" Text="操作:"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
               <asp:Label ID="Label1" Width="50px" runat="server" Text='<%# Eval( "id") %>'></asp:Label>
              <asp:Label ID="Label2" Width="50px" runat="server" Text='<%# Eval( "StuName") %>'></asp:Label>
              <asp:DropDownList ID="ddlSex" Width="50px" runat="server">
                <asp:ListItem Value="0" > 男 </asp:ListItem>
                  <asp:ListItem Value="1"> 女 </asp:ListItem>
              </asp:DropDownList>
              <asp:Label ID="Label8" Width="100px" runat="server" Text='<%# Eval( "StuPhone") %>'></asp:Label>
              <asp:Button ID="Button1" Width="50px" runat="server" CommandName="singleDelete" Text="delete" />
            </ItemTemplate>
             <FooterTemplate>
                <asp:Button ID="Button2" runat="server"  CommandName="mutlDelete" Text="deleteAll" />
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
View Code

 

 

aspc.cs

public partial class _238DllInGridView : System.Web.UI.Page

    {
        ShowImageBll showBll = new ShowImageBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
                sexBind();
            }
        }

        public void BindDataList() {
            DataSet ds = showBll.GetStudentList();
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }

        public void sexBind()
        { 
            DataSet ds = showBll.GetStudentList();
            DropDownList ddlSex;
            for (int i = 0; i < DataList1.Items.Count;i++ )
            {
                if (ds.Tables[0].Rows[i]["StuSex"].ToString() == "")//数据库性别没有设置,ddlSex控件将默认设置为“男”
                {
                    ddlSex = (DropDownList)DataList1.Items[i].FindControl("ddlSex");
                    ddlSex.SelectedValue = "0";
                }
                if (ds.Tables[0].Rows[i]["StuSex"].ToString() == "")
                {
                    ddlSex = (DropDownList)DataList1.Items[i].FindControl("ddlSex");
                    ddlSex.SelectedValue="1";
                }
            }


        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName) {
                case "singleDelete":
                int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                showBll.DeleteStudent(id);
                Response.Write("<script>alert('删除成功!')</script>");
                BindDataList();
                break;
                case "mutlDelete":
                   
                    DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
                    for (int i = 0; i < dlic.Count; i++)
                    {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showBll.DeleteStudent(id1);      
                              }
                     
                    }
                    BindDataList();//重新绑定数据库
                    break;
                    
                
        
        }
        }
    }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值