GridView控件修改示例(修改含有DropDownList控件)

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "curr=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=curr");
        }
        DropDownList ddl1 = e.Row.FindControl("DropDownList3") as DropDownList;
        DropDownList ddl2 = e.Row.FindControl("DropDownList2") as DropDownList;
        HiddenField hf2 = e.Row.FindControl("HiddenField2") as HiddenField;
        HiddenField hf1 = e.Row.FindControl("HiddenField1") as HiddenField;
        if (ddl1 != null)
        {
            ddl1.SelectedValue = hf1.Value.Trim();
            ddl2.SelectedValue = hf2.Value.Trim();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values["Id"].ToString().Trim();
        string userRoleId = (GridView1.Rows[e.RowIndex].FindControl("DropDownList2") as DropDownList).SelectedValue.ToString();
        string userStateId = (GridView1.Rows[e.RowIndex].FindControl("DropDownList3") as DropDownList).SelectedValue.ToString();
        User user = UserManager.GetUserById(Convert.ToInt32(id));
        user.LoginId = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text.Trim();
        user.Mail = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text.Trim();
        user.Phone = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text.Trim();
        user.UserRole = UserRoleManager.GetUserRoleById(Convert.ToInt32(userRoleId));
        user.UserState = UserStateManager.GetUserStateById(Convert.ToInt32(userStateId));
        UserManager.ModifyUser(user);
        Response.Redirect("ManagerAllUsers.aspx");
    }

你可能感兴趣的:(GridView)