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();
    }
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页