2012-09-07 72 views
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(); 
} 

回傳的值是原始值(而非更改的值)。如果我註釋出裝入文本框的行,它可以正常工作。

回答

3

這是因爲您沒有將數據綁定的東西包裝在IsPostBack中,請檢查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"]; 
    } 
} 
+0

傳奇 - 徹底忘了! – Ben

相關問題