
119 P.3d 1016 (2005)
Bradley P. GRASSER, Appellant,
v.
STATE of Alaska, Appellee.
No. A-8867.
Court of Appeals of Alaska.
August 26, 2005.
*1017 Samuel A. McQuerry, Assistant Public Defender, Ketchikan, and Barbara K. Brink, Public Defender, Anchorage, for the Appellant.
Diane L. Wendlandt, Assistant Attorney General, Office of Special Prosecutions and Appeals, Anchorage, and David W. Márquez, Attorney General, Juneau, for the Appellee.
Before: COATS, Chief Judge, and MANNHEIMER and STEWART, Judges.

OPINION
MANNHEIMER, Judge.
Bradley P. Grasser was charged with nine crimes, all related to domestic violence against his girlfriend  assault, unlawful contact, violating a protective order, and violating the conditions of bail release. He ultimately reached a plea agreement with the State in which he pleaded guilty to two counts of fourth-degree assault, one count of first-degree unlawful contact, and one count of violating a protective order.[1] (The remaining charges were dismissed.)
Grasser's plea agreement called for him to receive a specified amount of jail time, but another significant aspect of Grasser's sentence was left unresolved: whether the district court should order Grasser to participate in a domestic violence rehabilitation program (the "batterers intervention program") run by the Ketchikan Indian Community.
This program is located in Ketchikan, and Grasser lives in Craig. Grasser contended that the court should not order him to participate in this rehabilitative program because the costs of traveling back and forth between Craig and Ketchikan would impose an unfair financial burden on him. The State, for its part, believed that the costs were reasonable, and that Grasser was in need of the rehabilitative treatment offered at the program. In their plea bargain, Grasser and the State agreed that they would litigate this issue at sentencing, and that the sentencing judge would make the final decision.
After hearing the parties' arguments on this issue, Magistrate Christine Ellis found that Grasser was in need of the rehabilitative treatment offered by the program, and she rejected Grasser's contention that the costs of attending this program would be unreasonable. The magistrate found that Grasser could afford ferry transportation between Craig and Ketchikan. She further found that the directors of the program were willing to schedule their sessions to accommodate Grasser and other defendants who lived outside of Ketchikan. In addition, Magistrate Ellis declared that she would impose no fine on Grasser for any of his four convictions, so that Grasser's money could be devoted to the rehabilitative program.
(The magistrate structured Grasser's judgements so that he could begin the rehabilitative program while he was serving his jail sentence, if the Department of Corrections offered him this opportunity. Otherwise, Grasser was ordered to begin the program upon his release, as a condition of his probation.)
Grasser raises two arguments in this appeal. First, he contends that Magistrate Ellis was clearly erroneous when she concluded that participation in the Ketchikan program would not impose an unreasonable financial burden on Grasser. We reject this contention. Magistrate Ellis explained the basis of her decision in some detail, and she refrained from imposing any fine on Grasser, so that he would be better able to pay the cost of transportation and the other expenses involved. We find no error in the magistrate's resolution of this issue.
Next, Grasser contends that, leaving aside any issue of financial burden, Magistrate Ellis had no authority to order him to *1018 participate in the Ketchikan batterers program. Grasser argues that the pertinent provisions of Title 12 of the Alaska Statutes do not give sentencing judges the authority to order a defendant to participate in a rehabilitative program located outside the defendant's place of residence, either as a direct component of his sentence or as a condition of his probation.
Grasser did not make this argument in the district court. Instead, as explained above, Grasser's plea agreement with the State expressly provided that the district court could order Grasser to participate in this program if the court rejected Grasser's contention that participation in the program would put him to an unreasonable expense.
In other words, Grasser's position in this appeal is a repudiation of the position that he took when he negotiated his plea agreement with the State. An attorney has a duty to deal fairly with judges and opposing counsel. We question whether this duty is met when an attorney expressly agrees to a procedure in the trial court and then, having suffered an adverse ruling, the attorney argues on appeal that this procedure is illegal.
In any event, Grasser's argument was not preserved in the district court, and any error was invited. Because of this, Grasser is estopped from pursuing this argument on appeal.
If Grasser now believes that he agreed to an illegal procedure or an illegal sentence when he negotiated his plea bargain with the State, his proper course of action is to ask the district court to allow him to withdraw his pleas (i.e., rescind the plea bargain). Because Grasser negotiated a plea agreement with the government, and because he was sentenced under the terms of that agreement, Grasser can not now claim the benefit of the portions of the agreement that he likes while, at the same time, mounting an appellate attack on the portions that he does not like.
The judgement of the district court is AFFIRMED.
NOTES
[1]  AS 11.41.230(a), AS 11.56.750(a), and AS 11.56.740(a), respectively.
