4

ページが読み込まれると、データベースにいくつかの値がクエリされ、いくつかのテキスト ボックスに値が入力されます。

protected void Page_Load(object sender, EventArgs e)
{
    tadbDataContext tadb = new tadbDataContext();
    Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);

    tbTextColor.Text = hexColors["textColor"];
    tbAltColor.Text = hexColors["altColor"];
    tbBackgroundColor.Text = hexColors["backgroundColor"];
}

次に、値を変更し、次のことを行うボタンをクリックして、データベースに再送信しようとします。

using (tadbDataContext tadb = new tadbDataContext())
{
    var textColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "textColor");

    var altColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "altColor");
    var backgroundColor = tadb.msp_silentAuctionColors.Single(x => x.colorDescription == "backgroundColor");

    textColor.colorValue = tbTextColor.Text;
    altColor.colorValue = tbAltColor.Text;
    backgroundColor.colorValue = tbBackgroundColor.Text;

    tadb.SubmitChanges();
}

ポストバックされる値は、元の (変更されていない) 値です。ロード時にテキスト ボックスに入力する行をコメント アウトすると、正常に動作します。

4

1 に答える 1

3

IsPostBackこれは、データバインディングを-check inでラップしなかったためですPage_Load。したがって、変更された値を常にデータベースからの古い値で上書きしています。

したがって、次のようにするだけです。

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        tadbDataContext tadb = new tadbDataContext();
        Dictionary<string, string> hexColors = tadb.msp_silentAuctionColors.ToDictionary(t => t.colorDescription, t => t.colorValue);

        tbTextColor.Text = hexColors["textColor"];
        tbAltColor.Text = hexColors["altColor"];
        tbBackgroundColor.Text = hexColors["backgroundColor"];
    }
}
于 2012-09-07T23:32:27.300 に答える