DataList数据绑定时DataList.DataKeyField和DataList.DataKey的用法

private void DataListToBing()
    {
        int curPage = Convert.ToInt32(this.Label2.Text);
        SqlConnection con = DB.createCon();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("select * from employees", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "emp");
        PagedDataSource ps = new PagedDataSource();
        ps.AllowPaging = true;
        ps.PageSize = 3;
        this.Label4.Text = ps.PageCount.ToString();
        ps.CurrentPageIndex = curPage - 1;
        ps.DataSource = ds.Tables["emp"].DefaultView;
        this.DataList1.DataKeyField = "EmployeeID";-----//这个时设置数据库中的主键字段.
        this.Button1.Enabled = true;
        this.Button2.Enabled = true;
        if (curPage == 1)
        {
            this.Button1.Enabled = false;
        }
        if (curPage == ps.PageCount)
        {
            this.Button2.Enabled = false;
        }
        this.DataList1.DataSource = ps;
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
            this.DataList1.SelectedIndex = e.Item.ItemIndex;
            this.DataListToBing();
        }
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.DataList1.SelectedIndex = -1;
        this.DataListToBing();
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = -1;
        this.DataList1.SelectedIndex = -1;
        this.DataListToBing();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        string empID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();---//DataList1.DataKeys里是一个集
                                                                                                        //和 注意:里面是按照索引的顺序存储了
                                                                                                      //DataKeyField设置的主键,里面的集合的索引
                                                                                                     //设置成DataList中对应的索引,巧妙
        string City = ((TextBox)e.Item.FindControl("txtCity")).Text;
        SqlConnection con = DB.createCon();
        con.Open();
        SqlCommand cmd = new SqlCommand("update employees set City='" + City + "' where EmployeeID='" + empID + "'", con);
        cmd.ExecuteNonQuery();
        this.DataListToBing();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text) - 1);
        this.DataListToBing();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Label2.Text = Convert.ToString(Convert.ToInt32(this.Label2.Text) + 1);
        this.DataListToBing();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值