日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

GridView更新代碼

 昵稱14676 2007-12-15
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)//頁面第依次顯示時,執(zhí)行數(shù)據(jù)綁定
            databind();
    }

    protected void databind()
    {
        SqlConnection sqlcon = new SqlConnection("Data Source=KEN;Initial Catalog=aspnettest;Persist Security Info=True;User ID=hyslove;Password=hyslove");
        SqlCommand sqlcom = new SqlCommand("select * from UserTable", sqlcon);
        sqlcon.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        da.Fill(ds, "UserTable");
        GridView1.DataSource = ds.Tables["UserTable"].DefaultView;
        GridView1.DataBind();
        sqlcon.Close();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        databind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string username = GridView1.DataKeys[GridView1.EditIndex][0].ToString();

//這個是根據(jù)前面的Datekeynames設(shè)置而得到的。
            //如果多個鍵值,那么用GridView1.DataKeys[GridView1.EditIndex].Value三[0];
            string passwordnew = ((TextBox)(GridView1.Rows[GridView1.EditIndex].Cells[3].Controls[0])).Text.ToString().Trim();
          
            string sql = "update UserTable set Password='"+passwordnew+
                "' where Username='" + username + "'";
            SqlConnection sqlcon = new SqlConnection("Data Source=KEN;Initial Catalog=aspnettest;Persist Security Info=True;User ID=hyslove;Password=hyslove");
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;//返回正常狀態(tài)
            databind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        databind
();
    }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約